C++ std::list的merge()使用方式與分析
看到《C++標準庫第2版》對list::merge()的相關(guān)介紹,令我有點迷糊,特意敲代碼驗了一下不同情況的調(diào)用結(jié)果。
《C++標準庫第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默認排序,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(); //默認升序排序 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(); //默認升序排序 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(); //默認升序排序 c2.sort(); //默認升序排序 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; c1.sort(); //默認升序排序 c2 = c1; myPrinter(c1, c2); cout << "-----------合并后數(shù)據(jù)-----------" << endl; c1.merge(c2); myPrinter(c1, c2);
結(jié)果:可以看到,c1賦值給c2,使得c2也具有了與c1一樣的默認排序,兩者合并后,仍能按默認升序排序,結(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默認排序,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的默認排序,與情況二相似
情況九: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é)論
因為合并后的順序情況多變,所以如果希望合并后結(jié)果按某種規(guī)則排序,建議使用c1.merge(c2, op),指明合并后的排序規(guī)則。
當然,如果c1,c2都是默認排序,則可以直接使用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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- C++中std::tuple和std::pair的高級用法
- C++20中std::format的示例代碼
- c++之std::get_time和std::put_time
- C++中std::ios_base::floatfield報錯已解決
- C++中std::invalid_argument報錯解決
- C++中std::ifstream::readsome和std::ifstream::read的區(qū)別解析
- C++中std::ifstream的使用方法介紹
- C++中std::transform的使用小結(jié)
- C++中的std::format?如何實現(xiàn)編譯期格式檢查
- c++中std::placeholders的使用方法
相關(guān)文章
C語言數(shù)組按協(xié)議存儲與按協(xié)議解析數(shù)據(jù)的實現(xiàn)
今天小編就為大家分享一篇關(guān)于C語言數(shù)組按協(xié)議存儲與按協(xié)議解析數(shù)據(jù)的實現(xiàn),小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12深入理解C++中的new/delete和malloc/free動態(tài)內(nèi)存管理及區(qū)別介紹
這篇文章主要介紹了深入理解C++中的new/delete和malloc/free動態(tài)內(nèi)存管理,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09C++數(shù)據(jù)結(jié)構(gòu)與算法之反轉(zhuǎn)鏈表的方法詳解
這篇文章主要介紹了C++數(shù)據(jù)結(jié)構(gòu)與算法之反轉(zhuǎn)鏈表的方法,結(jié)合實例形式分析了C++反轉(zhuǎn)鏈表的原理、實現(xiàn)方法及相關(guān)注意事項,需要的朋友可以參考下2017-08-08C++將音頻PCM數(shù)據(jù)封裝成wav文件的方法
這篇文章主要為大家詳細介紹了C++將音頻PCM數(shù)據(jù)封裝成wav文件的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01