C++ std::list的merge()使用方式與分析
看到《C++標(biāo)準(zhǔn)庫第2版》對list::merge()的相關(guān)介紹,令我有點(diǎn)迷糊,特意敲代碼驗(yàn)了一下不同情況的調(diào)用結(jié)果。
《C++標(biāo)準(zhǔn)庫第2版》對list::merge()的相關(guān)介紹


list::merge()定義
merge()的作用就是將兩個list合并在一起,函數(shù)有2個版本:
c1.merge(c2)------------->這個版本含糊,將c2合入c1中,但合并后元素是怎么排序的呢?下文主要分析這個版本的不同調(diào)用結(jié)果
c1.merge(c2, op)--------->這個版本比較簡單,就是將c2的內(nèi)容合入到c1中,然后按op()排序
c1.merge(c2)調(diào)用情況分析
前提:有兩個list,內(nèi)容分別如下:

情況一:c1默認(rèn)排序,c2不排序,c2合入c1中
list<int> c1{ 0,1,2,88,3,4 };
list<int> c2{ 10,11,99,13,14,15 };
cout << "-----------原始數(shù)據(jù)-----------" << endl;
myPrinter(c1, c2);
cout << "-----------排序后數(shù)據(jù)-----------" << endl;
c1.sort(); //默認(rèn)升序排序
myPrinter(c1, c2);
cout << "-----------合并后數(shù)據(jù)-----------" << endl;
c1.merge(c2);
myPrinter(c1, c2);
結(jié)果:合并后沒有按c1的默認(rèn)升序排序
情況二:c1不排序,c2默認(rèn)排序,c2合入c1中
list<int> c1{ 0,1,2,88,3,4 };
list<int> c2{ 10,11,99,13,14,15 };
cout << "-----------原始數(shù)據(jù)-----------" << endl;
myPrinter(c1, c2);
cout << "-----------排序后數(shù)據(jù)-----------" << endl;
c2.sort(); //默認(rèn)升序排序
myPrinter(c1, c2);
cout << "-----------合并后數(shù)據(jù)-----------" << endl;
c1.merge(c2);
myPrinter(c1, c2);
結(jié)果:合并后沒有按c2的默認(rèn)升序排序
情況三:c1默認(rèn)排序,c2默認(rèn)排序,c2合入c1中
list<int> c1{ 0,1,2,88,3,4 };
list<int> c2{ 10,11,99,13,14,15 };
cout << "-----------原始數(shù)據(jù)-----------" << endl;
myPrinter(c1, c2);
cout << "-----------排序后數(shù)據(jù)-----------" << endl;
c1.sort(); //默認(rèn)升序排序
c2.sort(); //默認(rèn)升序排序
myPrinter(c1, c2);
cout << "-----------合并后數(shù)據(jù)-----------" << endl;
c1.merge(c2);
myPrinter(c1, c2);
結(jié)果:合并后也能按默認(rèn)升序排序
情況四:c1默認(rèn)排序,將c1賦值給c2,c2合入c1中
list<int> c1{ 0,1,2,88,3,4 };
list<int> c2{ 10,11,99,13,14,15 };
cout << "-----------原始數(shù)據(jù)-----------" << endl;
myPrinter(c1, c2);
cout << "-----------排序后數(shù)據(jù)-----------" << endl;
c1.sort(); //默認(rèn)升序排序
c2 = c1;
myPrinter(c1, c2);
cout << "-----------合并后數(shù)據(jù)-----------" << endl;
c1.merge(c2);
myPrinter(c1, c2);
結(jié)果:可以看到,c1賦值給c2,使得c2也具有了與c1一樣的默認(rèn)排序,兩者合并后,仍能按默認(rèn)升序排序,結(jié)果與情況三結(jié)果相似。
下面使用自定義的降序規(guī)則(op())來排序
//降序比較
auto op = [](int first, int second) {
return first > second;
};情況五:c1自定義降序排序,c2不排序,c2合入c1中
list<int> c1{ 0,1,2,88,3,4 };
list<int> c2{ 10,11,99,13,14,15 };
cout << "-----------原始數(shù)據(jù)-----------" << endl;
myPrinter(c1, c2);
cout << "-----------排序后數(shù)據(jù)-----------" << endl;
c1.sort(op);
myPrinter(c1, c2);
cout << "-----------合并后數(shù)據(jù)-----------" << endl;
c1.merge(c2);
myPrinter(c1, c2);
結(jié)果:合并后沒有按c1的自定義降序排序,與情況一相似
情況六:c1不排序,c2自定義降序排序,c2合入c1中
list<int> c1{ 0,1,2,88,3,4 };
list<int> c2{ 10,11,99,13,14,15 };
cout << "-----------原始數(shù)據(jù)-----------" << endl;
myPrinter(c1, c2);
cout << "-----------排序后數(shù)據(jù)-----------" << endl;
c2.sort(op);
myPrinter(c1, c2);
cout << "-----------合并后數(shù)據(jù)-----------" << endl;
c1.merge(c2);
myPrinter(c1, c2);
結(jié)果:合并后沒有按c2的自定義降序排序,與情況二相似
情況七:c1自定義降序排序,c2自定義降序排序,c2合入c1中
list<int> c1{ 0,1,2,88,3,4 };
list<int> c2{ 10,11,99,13,14,15 };
cout << "-----------原始數(shù)據(jù)-----------" << endl;
myPrinter(c1, c2);
cout << "-----------排序后數(shù)據(jù)-----------" << endl;
c1.sort(op);
c2.sort(op);
myPrinter(c1, c2);
cout << "-----------合并后數(shù)據(jù)-----------" << endl;
c1.merge(c2);
myPrinter(c1, c2);
結(jié)果:合并后,其結(jié)果僅僅是將c2放到了c1的末端,c1段、c2段數(shù)據(jù)仍是合并前的順序,這與情況三有差異
情況八:c1自定義降序排序,c2默認(rèn)排序,c2合入c1中
list<int> c1{ 0,1,2,88,3,4 };
list<int> c2{ 10,11,99,13,14,15 };
cout << "-----------原始數(shù)據(jù)-----------" << endl;
myPrinter(c1, c2);
cout << "-----------排序后數(shù)據(jù)-----------" << endl;
c1.sort(op);
c2.sort();
myPrinter(c1, c2);
cout << "-----------合并后數(shù)據(jù)-----------" << endl;
c1.merge(c2);
myPrinter(c1, c2);
結(jié)果:合并后,沒有按c1的自定義降序排序,也沒有按c2的默認(rèn)排序,與情況二相似
情況九:c1自定義降序排序,將c1賦值給c2,c2合入c1中
list<int> c1{ 0,1,2,88,3,4 };
list<int> c2{ 10,11,99,13,14,15 };
cout << "-----------原始數(shù)據(jù)-----------" << endl;
myPrinter(c1, c2);
cout << "-----------排序后數(shù)據(jù)-----------" << endl;
c1.sort(op);
c2 = c1;
myPrinter(c1, c2);
cout << "-----------合并后數(shù)據(jù)-----------" << endl;
c1.merge(c2);
myPrinter(c1, c2); 
結(jié)果:合并后,其結(jié)果僅僅是將c2放到了c1的末端,c1段、c2段數(shù)據(jù)仍是合并前的順序,這與情況七相同,但與情況三有差異
結(jié)論
因?yàn)楹喜⒑蟮捻樞蚯闆r多變,所以如果希望合并后結(jié)果按某種規(guī)則排序,建議使用c1.merge(c2, op),指明合并后的排序規(guī)則。
當(dāng)然,如果c1,c2都是默認(rèn)排序,則可以直接使用c1.merge(c2),即上文提到的情況三。
附:示例的輔助函數(shù)
template <class T>
void printfList(const T& _Container, const char* _Delim)
{
std::copy(_Container.cbegin(), _Container.cend(), std::ostream_iterator<T::value_type>(cout, _Delim));
cout << endl;
}
void myPrinter(const list<int>& c1, const list<int>& c2)
{
cout << "c1:";
printfList(c1, " ");
cout << "c2:";
printfList(c2, " ");
cout << "----------------------" << endl << endl;
}以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
- C++中std::tuple和std::pair的高級用法
- C++20中std::format的示例代碼
- c++之std::get_time和std::put_time
- C++中std::ios_base::floatfield報(bào)錯已解決
- C++中std::invalid_argument報(bào)錯解決
- C++中std::ifstream::readsome和std::ifstream::read的區(qū)別解析
- C++中std::ifstream的使用方法介紹
- C++中std::transform的使用小結(jié)
- C++中的std::format?如何實(shí)現(xiàn)編譯期格式檢查
- c++中std::placeholders的使用方法
相關(guān)文章
C/C++中OpenCV 矩陣運(yùn)算的實(shí)現(xiàn)
本文主要介紹了C/C++中OpenCV 矩陣運(yùn)算的實(shí)現(xiàn),包括基本算術(shù)運(yùn)算(標(biāo)量與矩陣)、矩陣乘法、轉(zhuǎn)置、逆矩陣、行列式、跡、范數(shù)等操作,感興趣的可以了解一下2025-05-05
Matlab實(shí)現(xiàn)將圖像序列合并為視頻的方法詳解
MATLAB是一種高性能語言,用于操縱矩陣、執(zhí)行技術(shù)計(jì)算、繪圖等。它代表矩陣實(shí)驗(yàn)室。借助這個軟件,我們可以從圖像中創(chuàng)建視頻。這篇文章主要介紹了Matlab實(shí)現(xiàn)將圖像序列合并為視頻的四個方法,希望對大家有所幫助2023-03-03
Linux下C語言的幾道經(jīng)典面試題小結(jié)(分享)
下面小編就為大家?guī)硪黄狶inux下C語言的幾道經(jīng)典面試題小結(jié)(分享)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05
C++的類型轉(zhuǎn)換(強(qiáng)轉(zhuǎn))你了解嗎
這篇文章主要為大家詳細(xì)介紹了C++的類型轉(zhuǎn)換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-02-02

