C++ 如何合并两个不同大小的向量对

C++ 如何合并两个不同大小的向量对,c++,vector,std-pair,C++,Vector,Std Pair,我有两个向量,我想合并成一个。两者都是嵌入式对向量(能够在一对中保存3个int值),它们的大小彼此不同 两对向量和合并向量的代码为: vector < pair<int, pair<int,int> > > parentVect; vector < pair<int, pair<int,int> > > childVect; vector < pair<int, pair<int,int> >

我有两个向量,我想合并成一个。两者都是嵌入式对向量(能够在一对中保存3个int值),它们的大小彼此不同

两对向量和合并向量的代码为:

vector < pair<int, pair<int,int> > > parentVect;
vector < pair<int, pair<int,int> > > childVect;
vector < pair<int, pair<int,int> > > mergedVect;
我的输出是
mergedVect={(019)、(039)、(139)、(2215)}

如果要将两个已排序的序列合并为一个序列,应该使用的算法函数是

以下是一个使用您的数据的示例:

#include <vector>
#include <utility>
#include <iostream>
#include <algorithm>
#include <iterator>

typedef std::pair<int, int> PairInt;
typedef std::pair<int, PairInt> PairPairInt;
typedef std::vector<PairPairInt> PairVect;

// lambda that compares the pairs on the last value in the pair sequence
auto comparer = [](const PairPairInt& p1, const PairPairInt& p2) {return p1.second.second < p2.second.second; };

int main()
{
    PairVect parentVect = { { 0,{ 3, 9 } },{ 1,{ 3, 9 } },{ 2,{ 2, 15 } } };
    PairVect childVect = { { 0,{ 1, 9 } } };
    PairVect mergedVect;

    // First, sort the sequences based on the criteria that the
    // last number in the pairs is sorted in ascending order
    std::sort(parentVect.begin(), parentVect.end(), comparer);
    std::sort(childVect.begin(), childVect.end(), comparer);

    // Now merge the two sequences above, using the same sorting criteria
    std::merge(parentVect.begin(), parentVect.end(), 
               childVect.begin(), childVect.end(), 
               std::back_inserter(mergedVect), comparer);

    for (auto& p : mergedVect)
        std::cout << "{" << p.first << " " << p.second.first << " " << p.second.second << "}\n";
}


注意
std::sort
的用法,因为
std::merge
需要排序的范围。

我不确定这有什么帮助@Paul McKenzie好的,我把它改成了“你想合并两个排序的序列,对吗?然后算法是
inplace\u merge
,或者在主要部分阅读我的评论。您很可能是从items开始的
mergedVect
——它并不像上面的代码那样是空的。更重要的是
vectormergedVect(sizeOfFinalVect)--该行将
sizeOfFinalVect
对添加到向量中。如果您想要证明,请在该行代码之后立即调用
mergedVect.size()
。好的,我已经修复了这些错误。我不再得到额外的4对0。但是,当它合并childVect时,它仍然合并到不合适的位置。我看了你的代码,但是parentVect和childVect的对都被读入了,所以我不能像你那样分配它们。不知道该怎么处理that@Pvelez_3--将我发布的内容作为一个函数,只进行排序和合并。关注点分离。然后分别输入数据,因为这与实现合并两个排序序列的目标无关这是lambda捕获语法。这在任何关于C++ 11的教程中都有更好的解释。
merge(parentVect.begin(), parentVect.end(), childVect.begin(), childVect.end(), std::back_inserter(mergedVect));
#include <vector>
#include <utility>
#include <iostream>
#include <algorithm>
#include <iterator>

typedef std::pair<int, int> PairInt;
typedef std::pair<int, PairInt> PairPairInt;
typedef std::vector<PairPairInt> PairVect;

// lambda that compares the pairs on the last value in the pair sequence
auto comparer = [](const PairPairInt& p1, const PairPairInt& p2) {return p1.second.second < p2.second.second; };

int main()
{
    PairVect parentVect = { { 0,{ 3, 9 } },{ 1,{ 3, 9 } },{ 2,{ 2, 15 } } };
    PairVect childVect = { { 0,{ 1, 9 } } };
    PairVect mergedVect;

    // First, sort the sequences based on the criteria that the
    // last number in the pairs is sorted in ascending order
    std::sort(parentVect.begin(), parentVect.end(), comparer);
    std::sort(childVect.begin(), childVect.end(), comparer);

    // Now merge the two sequences above, using the same sorting criteria
    std::merge(parentVect.begin(), parentVect.end(), 
               childVect.begin(), childVect.end(), 
               std::back_inserter(mergedVect), comparer);

    for (auto& p : mergedVect)
        std::cout << "{" << p.first << " " << p.second.first << " " << p.second.second << "}\n";
}
{0 3 9}
{1 3 9}
{0 1 9}
{2 2 15}