头文件位于:boost/assign/list_of.hpp,boost::assign是boost中一个对容器进行赋值的库,各种方法非常灵活使用。使用示例如下:
- // operator+=
- // 优点: 可应用与stl中定义的标准容器(vector, list, set, map等)
- // 缺点: 对于其他类型的容器(如boost新容器)则无能为力
- void test_assign_plus()
- {
- using namespace boost::assign;
- // 1. vector
- std::vector<int> values;
- values += 1, 2, 3, 4, 5, 6, 7, 8, 9; // 插入值到容器的末尾
- BOOST_ASSERT(values.size() == 9);
- BOOST_ASSERT(values[0] == 1);
- BOOST_ASSERT(values[8] == 9);
- // 2. set
- std::set<std::string> s; //标准集合容器
- s += "cpp", "java", "c#", "python";
- BOOST_ASSERT(s.size() == 4);
- // 3. map
- std::map<int, std::string> m;
- m += std::make_pair(1, "one"),
- std::make_pair(2, "two"),
- std::make_pair(3, "three"),
- std::make_pair(4, "four");
- BOOST_ASSERT(m[1] == "one");
- BOOST_ASSERT(m[2] == "two");
- BOOST_ASSERT(m[3] == "three");
- BOOST_ASSERT(m[4] == "four");
- }
- // operator()
- // 优点: operator+=使用上有些小的限制,而且在处理map容器也显麻烦,操作符operator()更通用.
- // 不能直接使用operator(), 而应当使用assign库提供三个辅助函数insert(),push_front(),push_back().
- // 这些函数可作用于拥有同名成员函数的容器,接受容器变量作为参数
- // 返回一个代理对象list_inserter,它重载了operator(),=等操作符用来实现向容器填入数据的功能。
- void test_assign_bracket()
- {
- using namespace boost::assign;
- std::vector<int> v;
- push_back(v), 1, 2, 3, 4, 5;
- push_back(v), 6, 7, 8;
- std::copy(v.cbegin(), v.cend(), std::ostream_iterator<int>(std::cout, " "));
- std::cout << std::endl;
- std::vector<int> l;
- push_back(l), 1, 2, 3, 4, 5;
- push_back(l), 6, 7, 8;
- std::copy(l.cbegin(), l.cend(), std::ostream_iterator<int>(std::cout, " "));
- std::cout << std::endl;
- std::deque<int> d;
- push_back(d), 1, 2, 3, 4, 5;
- push_back(d), 6, 7, 8;
- push_front(d), 0, -1, -2;
- std::copy(d.cbegin(), d.cend(), std::ostream_iterator<int>(std::cout, " "));
- std::cout << std::endl;
- std::map<std::string, int> months;
- insert(months)
- ( "january", 31 )( "february", 28 )
- ( "march", 31 )( "april", 30 )
- ( "may", 31 )( "june", 30 )
- ( "july", 31 )( "august", 31 )
- ( "september", 30 )( "october", 31 )
- ( "november", 30 )( "december", 31 );
- BOOST_ASSERT(months.size() == 12);
- BOOST_ASSERT(months["january"] == 31);
- }
- // 初始化容器元素的函数: list_of(), map_list_of(), tuple_list_of()
- // 操作符+=和()解决了对容器的赋值问题,但有的时候需要在容器构造的时候就完成数据的填充,这种方式较赋值更为高效。
- // c++内建的数组和标准字符串类string支持这样做,但stl容器则不行。
- // list_of()函数的用法与之前的insert(),push_back()等函数很相似, 也重载了括号, 逗号操作符。
- // 它很智能, 返回一个匿名的列表, 可以赋值给任意容器。
- void test_assign_list_of()
- {
- using namespace boost::assign;
- const std::list<int> l = list_of(1)(2)(3)(4)(5)(6)(7)(8)(9)(10)(11);
- BOOST_ASSERT( l.size() == 11 );
- BOOST_ASSERT( l.back() == 11 );
- BOOST_ASSERT( l.front() == 1 );
- // 二维数组
- std::vector<std::vector<int>> v = list_of(list_of(1)(2)) (list_of(3)(4));
- v += list_of(5)(6), list_of(7)(8);
- // 两个类似功能的ref_list_of()和cref_list_of(),
- // 这两个函数接受变量的引用作为参数来创建初始化匿名列表,较list_of()的效率更高
- int a = 1, b = 2, c = 3;
- std::vector<int> v = ref_list_of<3>(a)(b)(c);
- assert(v.size() == 3);
- }
- void test_assign_map_list_of()
- {
- using namespace boost::assign;
- std::map<int, int> m1 = map_list_of(1, 2)(3, 4)(5, 6)(7, 8)(9, 10);
- std::map<int, std::string> m2 = map_list_of(1, "one")(2, "two")(3, "three")(4, "four");
- }
- // 减少重复输入:
- // assign库提供repeat(),repeat_fun()和range()三个函数来减少重复的输入
- void test_assign_repeat()
- {
- using namespace boost::assign;
- std::vector<int> v = list_of(1).repeat(3, 2)(3)(4)(5);
- // 1, 2, 2, 2, 3, 4, 5
- std::copy(v.cbegin(), v.cend(), std::ostream_iterator<int>(std::cout, " "));
- std::cout << std::endl;
- std::deque<int> d;
- push_front(d).range(v.begin(), v.begin() + 5);
- // 3, 2, 2, 2, 1
- std::copy(d.cbegin(), d.cend(), std::ostream_iterator<int>(std::cout, " "));
- st