C++boost库之assign使用(一)

来源:互联网 发布:南风知我意2七微 编辑:程序博客网 时间:2024/05/22 22:04

头文件位于:boost/assign/list_of.hpp,boost::assign是boost中一个对容器进行赋值的库,各种方法非常灵活使用。使用示例如下:

[cpp] view plain copy
 print?

  1. // operator+=  
  2. // 优点: 可应用与stl中定义的标准容器(vector, list, set, map等)  
  3. // 缺点: 对于其他类型的容器(如boost新容器)则无能为力  
  4. void test_assign_plus()  
  5. {  
  6.     using namespace boost::assign;  
  7.   
  8.     // 1. vector  
  9.     std::vector<int> values;  
  10.     values += 1, 2, 3, 4, 5, 6, 7, 8, 9; // 插入值到容器的末尾  
  11.   
  12.     BOOST_ASSERT(values.size() == 9);  
  13.     BOOST_ASSERT(values[0] == 1);  
  14.     BOOST_ASSERT(values[8] == 9);  
  15.   
  16.     // 2. set   
  17.     std::set<std::string> s;    //标准集合容器  
  18.     s += "cpp""java""c#""python";  
  19.     BOOST_ASSERT(s.size() == 4);  
  20.   
  21.     // 3. map  
  22.     std::map<int, std::string> m;  
  23.     m += std::make_pair(1, "one"),   
  24.          std::make_pair(2, "two"),   
  25.          std::make_pair(3, "three"),   
  26.          std::make_pair(4, "four");  
  27.     BOOST_ASSERT(m[1] == "one");  
  28.     BOOST_ASSERT(m[2] == "two");  
  29.     BOOST_ASSERT(m[3] == "three");  
  30.     BOOST_ASSERT(m[4] == "four");  
  31. }  
  32.   
  33. // operator()  
  34. // 优点: operator+=使用上有些小的限制,而且在处理map容器也显麻烦,操作符operator()更通用.  
  35. //       不能直接使用operator(), 而应当使用assign库提供三个辅助函数insert(),push_front(),push_back().  
  36. //       这些函数可作用于拥有同名成员函数的容器,接受容器变量作为参数  
  37. //       返回一个代理对象list_inserter,它重载了operator(),=等操作符用来实现向容器填入数据的功能。  
  38. void test_assign_bracket()  
  39. {  
  40.     using namespace boost::assign;  
  41.   
  42.     std::vector<int> v;  
  43.     push_back(v), 1, 2, 3, 4, 5;  
  44.     push_back(v), 6, 7, 8;  
  45.     std::copy(v.cbegin(), v.cend(), std::ostream_iterator<int>(std::cout, " "));  
  46.     std::cout << std::endl;  
  47.   
  48.     std::vector<int> l;  
  49.     push_back(l), 1, 2, 3, 4, 5;  
  50.     push_back(l), 6, 7, 8;  
  51.     std::copy(l.cbegin(), l.cend(), std::ostream_iterator<int>(std::cout, " "));  
  52.     std::cout << std::endl;  
  53.   
  54.     std::deque<int> d;  
  55.     push_back(d), 1, 2, 3, 4, 5;  
  56.     push_back(d), 6, 7, 8;  
  57.     push_front(d), 0, -1, -2;  
  58.     std::copy(d.cbegin(), d.cend(), std::ostream_iterator<int>(std::cout, " "));  
  59.     std::cout << std::endl;  
  60.   
  61.     std::map<std::string, int> months;  
  62.     insert(months)  
  63.         ( "january",   31 )( "february", 28 )  
  64.         ( "march",     31 )( "april",    30 )  
  65.         ( "may",       31 )( "june",     30 )  
  66.         ( "july",      31 )( "august",   31 )  
  67.         ( "september", 30 )( "october",  31 )  
  68.         ( "november",  30 )( "december", 31 );  
  69.     BOOST_ASSERT(months.size() == 12);      
  70.     BOOST_ASSERT(months["january"] == 31);  
  71. }  
  72.   
  73. // 初始化容器元素的函数: list_of(), map_list_of(), tuple_list_of()  
  74. // 操作符+=和()解决了对容器的赋值问题,但有的时候需要在容器构造的时候就完成数据的填充,这种方式较赋值更为高效。  
  75. // c++内建的数组和标准字符串类string支持这样做,但stl容器则不行。  
  76.   
  77. // list_of()函数的用法与之前的insert(),push_back()等函数很相似, 也重载了括号, 逗号操作符。  
  78. // 它很智能, 返回一个匿名的列表, 可以赋值给任意容器。  
  79. void test_assign_list_of()  
  80. {  
  81.     using namespace boost::assign;  
  82.   
  83.     const std::list<int> l = list_of(1)(2)(3)(4)(5)(6)(7)(8)(9)(10)(11);  
  84.     BOOST_ASSERT( l.size() == 11 );  
  85.     BOOST_ASSERT( l.back() == 11 );  
  86.     BOOST_ASSERT( l.front() == 1 );  
  87.   
  88.     // 二维数组  
  89.     std::vector<std::vector<int>> v = list_of(list_of(1)(2)) (list_of(3)(4));  
  90.     v += list_of(5)(6), list_of(7)(8);  
  91.   
  92.     // 两个类似功能的ref_list_of()和cref_list_of(),   
  93.     // 这两个函数接受变量的引用作为参数来创建初始化匿名列表,较list_of()的效率更高  
  94.     int a = 1, b = 2, c = 3;  
  95.     std::vector<int> v = ref_list_of<3>(a)(b)(c);  
  96.     assert(v.size() == 3);  
  97. }  
  98.   
  99. void test_assign_map_list_of()  
  100. {  
  101.     using namespace boost::assign;  
  102.   
  103.     std::map<intint> m1 = map_list_of(1, 2)(3, 4)(5, 6)(7, 8)(9, 10);  
  104.     std::map<int, std::string> m2 = map_list_of(1, "one")(2, "two")(3, "three")(4, "four");  
  105.   
  106. }  
  107.   
  108. // 减少重复输入:  
  109. // assign库提供repeat(),repeat_fun()和range()三个函数来减少重复的输入  
  110.   
  111. void test_assign_repeat()  
  112. {  
  113.     using namespace boost::assign;  
  114.   
  115.     std::vector<int> v = list_of(1).repeat(3, 2)(3)(4)(5);  
  116.     // 1, 2, 2, 2, 3, 4, 5  
  117.     std::copy(v.cbegin(), v.cend(), std::ostream_iterator<int>(std::cout, " "));  
  118.     std::cout << std::endl;     
  119.   
  120.     std::deque<int> d;  
  121.     push_front(d).range(v.begin(), v.begin() + 5);  
  122.     // 3, 2, 2, 2, 1  
  123.     std::copy(d.cbegin(), d.cend(), std::ostream_iterator<int>(std::cout, " "));  
  124.     st
原创粉丝点击