C++将无现有元素从一个向量复制到另一个矢量

发布于 2025-01-27 07:55:06 字数 724 浏览 2 评论 0原文

我有这样的课程:

struct DataElement {
    std::string key;
    std::string value;
    std::string placeholder;
    // some other data members
}

我将它们存储在矢量中。 现在,我有了一个功能,该函数采用了这些dataElementvector,并且还创建了vector dataElement的。

std::vector<DataElement> doAction(std::vector<DataElement>& data) {
    auto additional_data = create_additional_data() //returns std::vector<DataElement>
    //merge data and additional_data
    return additional_data
}

现在,我想将所有元素从vector数据复制到vector afred_data(如果尚不存在的密钥)。

我正在考虑使用copy_if,但是如何检查当前元素是否已经在目标中?

I have a class like this:

struct DataElement {
    std::string key;
    std::string value;
    std::string placeholder;
    // some other data members
}

I store them inside a Vector.
Now i have a function which takes a vector of these DataElement and also creates a vector of the DataElement.

std::vector<DataElement> doAction(std::vector<DataElement>& data) {
    auto additional_data = create_additional_data() //returns std::vector<DataElement>
    //merge data and additional_data
    return additional_data
}

Now i want to copy all the Elements from the vector data into the vector additional_data if there key is not already there.

I was thinking of using copy_if but how do i check if the current element is already in the Destination?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半窗疏影 2025-02-03 07:55:07

我更喜欢检查当前元素是否已经在目的地中使用附加unordered_map

std::vector<DataElement> doAction(std::vector<DataElement>& data) {
    std::vector<DataElement> result = data;
    std::unordered_map<std::string, DataElement> merged;
    for(const auto& e: data){
     merged[e.key] = e;
    }

    auto additional_data = create_additional_data() //returns std::vector<DataElement>
    // Time complexity: O(N)
    for(const auto &e: additional_data){
    // it's O(1) complexity
    if(merged.find(e.key) == merged.end()){
        result.push_back(e);
        merged[e.key] = e;
       }
    }
    //merge data and additional_data
    return result;
}

I prefer to check if the current element is already in the Destination with an additional unordered_map

std::vector<DataElement> doAction(std::vector<DataElement>& data) {
    std::vector<DataElement> result = data;
    std::unordered_map<std::string, DataElement> merged;
    for(const auto& e: data){
     merged[e.key] = e;
    }

    auto additional_data = create_additional_data() //returns std::vector<DataElement>
    // Time complexity: O(N)
    for(const auto &e: additional_data){
    // it's O(1) complexity
    if(merged.find(e.key) == merged.end()){
        result.push_back(e);
        merged[e.key] = e;
       }
    }
    //merge data and additional_data
    return result;
}
驱逐舰岛风号 2025-02-03 07:55:07

您可以使用std :: find_if检查给定键的元素是否已经在向量中。

但是,std :: find_ifo(n),当您为n元素进行操作时,您最终会带有o( n^2)。考虑使用std :: unordered_map而不是向量。 std :: unordered_map :: inserto(1)插入单个元素。

如果您不能切换到std :: Unordered_map,则在构建向量时仍然可以使用一个:

#include <string>
#include <vector>
#include <unordered_map>

struct DataElement {
    std::string key;
    std::string value;
    std::string placeholder;
    // some other data members
};

std::vector<DataElement> create_additional_data() { return {}; }

std::vector<DataElement> doAction(std::vector<DataElement>& data) {
    auto additional_data = create_additional_data();

    std::vector<DataElement> result;
    std::unordered_map<std::string,DataElement> temp;

    
    for (const auto& e : data) { temp.insert({e.key,e}); }
    //merge data and additional_data

    // element is not inserted when key is already present !
    for (const auto& e : additional_data) { temp.insert({e.key,e});} 

    for (const auto& e : temp) { result.push_back(e.second); }

    return result;
}

当两个向量都包含一个用于给定键时,我不确定您要保留哪个向量。上面的代码使用数据中的一个代码,仅在data中不存在from_data中的代码。这只是两个循环切换顺序的问题。

You could use std::find_if to check if an element with given key is already in the vector.

However, std::find_if is O(N) and when you do it for N elements you end up with a O(N^2). Consider to use std::unordered_map instead of vectors. std::unordered_map::insert is O(1) on average for inserting a single element.

If you cannot switch to std::unordered_map, you can still use one while building the vector:

#include <string>
#include <vector>
#include <unordered_map>

struct DataElement {
    std::string key;
    std::string value;
    std::string placeholder;
    // some other data members
};

std::vector<DataElement> create_additional_data() { return {}; }

std::vector<DataElement> doAction(std::vector<DataElement>& data) {
    auto additional_data = create_additional_data();

    std::vector<DataElement> result;
    std::unordered_map<std::string,DataElement> temp;

    
    for (const auto& e : data) { temp.insert({e.key,e}); }
    //merge data and additional_data

    // element is not inserted when key is already present !
    for (const auto& e : additional_data) { temp.insert({e.key,e});} 

    for (const auto& e : temp) { result.push_back(e.second); }

    return result;
}

I wasnt sure from which vector you want to keep the element when both vectors contain one for a given key. The above code uses the one from data and the one from additional_data is only inserted when it wasnt present in data. Its just a matter of switching order of the two loops.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文