Concatenating two std::vectors

后端 未结 25 2322
予麋鹿
予麋鹿 2020-11-22 12:00

How do I concatenate two std::vectors?

相关标签:
25条回答
  • 2020-11-22 12:14

    To be honest, you could fast concatenate two vectors by copy elements from two vectors into the other one or just only append one of two vectors!. It depends on your aim.

    Method 1: Assign new vector with its size is the sum of two original vectors' size.

    vector<int> concat_vector = vector<int>();
    concat_vector.setcapacity(vector_A.size() + vector_B.size());
    // Loop for copy elements in two vectors into concat_vector
    

    Method 2: Append vector A by adding/inserting elements of vector B.

    // Loop for insert elements of vector_B into vector_A with insert() 
    function: vector_A.insert(vector_A .end(), vector_B.cbegin(), vector_B.cend());
    
    0 讨论(0)
  • 2020-11-22 12:17

    I prefer one that is already mentioned:

    a.insert(a.end(), b.begin(), b.end());
    

    But if you use C++11, there is one more generic way:

    a.insert(std::end(a), std::begin(b), std::end(b));
    

    Also, not part of a question, but it is advisable to use reserve before appending for better performance. And if you are concatenating vector with itself, without reserving it fails, so you always should reserve.


    So basically what you need:

    template <typename T>
    void Append(std::vector<T>& a, const std::vector<T>& b)
    {
        a.reserve(a.size() + b.size());
        a.insert(a.end(), b.begin(), b.end());
    }
    
    0 讨论(0)
  • 2020-11-22 12:17

    Add this one to your header file:

    template <typename T> vector<T> concat(vector<T> &a, vector<T> &b) {
        vector<T> ret = vector<T>();
        copy(a.begin(), a.end(), back_inserter(ret));
        copy(b.begin(), b.end(), back_inserter(ret));
        return ret;
    }
    

    and use it this way:

    vector<int> a = vector<int>();
    vector<int> b = vector<int>();
    
    a.push_back(1);
    a.push_back(2);
    b.push_back(62);
    
    vector<int> r = concat(a, b);
    

    r will contain [1,2,62]

    0 讨论(0)
  • 2020-11-22 12:17

    There is an algorithm std::merge from C++17, which is very easy to use when the input vectors are sorted,

    Below is the example:

    #include <iostream>
    #include <vector>
    #include <algorithm>
    
    int main()
    {
        //DATA
        std::vector<int> v1{2,4,6,8};
        std::vector<int> v2{12,14,16,18};
    
        //MERGE
        std::vector<int> dst;
        std::merge(v1.begin(), v1.end(), v2.begin(), v2.end(), std::back_inserter(dst));
    
        //PRINT
        for(auto item:dst)
            std::cout<<item<<" ";
    
        return 0;
    }
    
    0 讨论(0)
  • 2020-11-22 12:18

    I would use the insert function, something like:

    vector<int> a, b;
    //fill with data
    b.insert(b.end(), a.begin(), a.end());
    
    0 讨论(0)
  • 2020-11-22 12:18

    Or you could use:

    std::copy(source.begin(), source.end(), std::back_inserter(destination));
    

    This pattern is useful if the two vectors don't contain exactly the same type of thing, because you can use something instead of std::back_inserter to convert from one type to the other.

    0 讨论(0)
提交回复
热议问题