简体   繁体   English

将两个列表合并成一个单独的列表

[英]Merge two lists into a serparate list

I am trying to merge two lists into a seperate list. 我正在尝试将两个列表合并为一个单独的列表。 I know how to merge two lists, but I am not sure how to go about merging the values into an empty third list, without the values of the two lists changing. 我知道如何合并两个列表,但是在将两个列表的值保持不变的情况下,我不确定如何将这些值合并到一个空的第三列表中。

list <int> FirstList, SecondList, ThirdList;

FirstList.merge(SecondList);

What you want is the generic merge algorithm, which copies from the two input ranges, rather than the specialized one for lists (which transfers the nodes into the third list): 您想要的是通用合并算法,该算法从两个输入范围进行复制,而不是针对列表的专用合并算法(它将节点转移到第三个列表中):

std::merge(FirstList.begin(), FirstList.end(),
           SecondList.begin(), SecondList.end(),
           std::back_inserter(ThirdList));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM