boost实用工具:assign库了解学习
生活随笔
收集整理的這篇文章主要介紹了
boost实用工具:assign库了解学习
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
許多時候,我們需要為容器初始化或者賦值,填入大量的數據;
STL容器僅提供了容納這些數據的方法,但是填充的步驟是相當地麻煩(insert、push_back);
于是,boost::assign出現了,其重載了+= ,()等運算符,用難以想象的簡潔對STL容器初始化或賦值.
+=很好用,但是僅適用于STL的標準容器,對于boost新容器卻無能為力;
()可以更靈活實現對容器的初始化;
容器構造的時候就對數據進行填充list_of等.
?
C++ Code?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | ? | /* ????許多時候,我們需要為容器初始化或者賦值,填入大量的數據; ????STL容器僅提供了容納這些數據的方法,但是填充的步驟是相當地麻煩(insert、push_back); ????于是,boost::assign出現了,其重載了+=?,()等運算符,用難以想象的簡潔對STL容器初始化或賦值. ????+=很好用,但是僅適用于STL的標準容器,對于boost新容器卻無能為力; ????()可以更靈活實現對容器的初始化; ????容器構造的時候就對數據進行填充list_of等. */ /************************************************************************/ /*?C++?stl?Library????????????????????????????????????????????????????????*/ /************************************************************************/ #include?<iostream> #include?<string> #include?<vector> #include?<set> #include?<map> /************************************************************************/ /*?C++?boost?Library???????????????????????????????????????????????????*/ /************************************************************************/ #include?"boost/assign.hpp" using?namespace?boost::assign; using?namespace?std; int?main(void) { ????//operator+= ????vector<int>?vec; ????vec?+=?1,2,3,4,5,6*6; ????set<string>?s; ????s?+=?"c","c++","java","c#"; ????map<int,string>?mInfo; ????mInfo?+=?make_pair(1,"Michael"),make_pair(2,"James"); ????//operator() ????vector<int>?v1; ????push_back(v1)?(1)?(2)?(3)?(4)?(5); ????map<int,string>?maps; ????insert(maps)?(1,"East")?(2,"West"); ????vector<int>?v2; ????push_back(v2),?1,2,3,4,5; ???? ????//list_of?map_list_of/pair_list_of?(tuple_list_of) ????vector<int>?vec1?=?list_of(1)?(2)?(3)?(4)?(5); ????set<int>?set1?=?(list_of(10),?20,30,40,50); ????map<int,int>?map1?=?map_list_of(1,1)?(2,2)?(3,3); ????map<int,string>?map2?=?pair_list_of(1,"aaa")?(2,"bbb"); ???? ????//減少重復輸入?repeat?repeat_fun?range ????vector<int>?vv?=?list_of(1).repeat(3,2)?(4)?(5)?(6); ????//更多內容請感興趣研究 ????//list_of的嵌套使用list_of(list_of()) ????//引用初始化列表ref_list_of()??cref_list_of() ????//... ????cin.get(); ????return?0; } |
轉載于:https://www.cnblogs.com/MakeView660/p/7090323.html
總結
以上是生活随笔為你收集整理的boost实用工具:assign库了解学习的全部內容,希望文章能夠幫你解決所遇到的問題。