Concatenating two std::vectors

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

How do I concatenate two std::vectors?

25条回答
  •  死守一世寂寞
    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 
    #include 
    #include 
    
    int main()
    {
        //DATA
        std::vector v1{2,4,6,8};
        std::vector v2{12,14,16,18};
    
        //MERGE
        std::vector dst;
        std::merge(v1.begin(), v1.end(), v2.begin(), v2.end(), std::back_inserter(dst));
    
        //PRINT
        for(auto item:dst)
            std::cout<

提交回复
热议问题