list的构造函数

来源:互联网 发布:路灯照明计算软件 编辑:程序博客网 时间:2024/05/20 05:11
[cpp] view plaincopyprint?
  1. explicit list(  
  2.    const Allocator& _Al  
  3. );  
  4. explicit list(  
  5.    size_type _Count  
  6. );  
  7. list(  
  8.    size_type _Count,  
  9.    const Type& _Val  
  10. );  
  11. list(  
  12.    size_type _Count,  
  13.    const Type& _Val,  
  14.    const Allocator& _Al  
  15. );  
  16. list(  
  17.    const list& _Right  
  18. );  
  19. template<class InputIterator>  
  20.    list(  
  21.       InputIterator _First,  
  22.       InputIterator _Last  
  23.    );  
  24. template<class InputIterator >  
  25.    list(  
  26.       InputIterator _First,  
  27.       InputIterator _Last,  
  28.       const Allocator& _Al  
  29.    );  
  30. list(  
  31.    list&& _Right  
  32. );  
例子:
[cpp] view plaincopyprint?
  1. // list的构造函数  
  2. void test_list_constructor()  
  3. {  
  4.     std::list<int>::iterator c4_Iter, c5_Iter;  
  5.   
  6.     // 0. Create an empty list c0  
  7.     std::list<int> c0;  
  8.   
  9.     // 1. Create a list c1 with 3 elements of default value 0  
  10.     std::list<int> c1(3);  
  11.   
  12.     // 2. Create a list c2 with 5 elements of value 2  
  13.     std::list<int> c2(5, 2);  
  14.   
  15.     // 3. Create a list c3 with 3 elements of value 1 and with the   
  16.     // allocator of list c2  
  17.     std::list<int> c3(3, 1, c2.get_allocator());  
  18.   
  19.     // 4. Create a copy, list c4, of list c2  
  20.     std::list<int> c4(c2);  
  21.   
  22.     // 5. Create a list c5 by copying the range c4[_First, _Last)  
  23.     c4_Iter = c4.begin();  
  24.     c4_Iter++;  
  25.     c4_Iter++;  
  26.     std::list<int> c5(c4.begin(), c4_Iter);  
  27.   
  28.     // 6. Create a list c6 by copying the range c4[_First, _Last) and with   
  29.     // the allocator of list c2  
  30.     c4_Iter = c4.begin();  
  31.     c4_Iter++;  
  32.     c4_Iter++;  
  33.     c4_Iter++;  
  34.     std::list<int> c6(c4.begin(), c4_Iter, c2.get_allocator());  
  35.   
  36.     std::cout << "c1 = ";  
  37.     std::copy(c1.begin(), c1.end(), std::ostream_iterator<int>(std::cout, " "));  
  38.     std::cout << std::endl;  
  39.   
  40.     std::cout << "c2 = ";  
  41.     std::copy(c2.begin(), c2.end(), std::ostream_iterator<int>(std::cout, " "));  
  42.     std::cout << std::endl;  
  43.   
  44.     std::cout << "c3 = ";  
  45.     std::copy(c3.begin(), c3.end(), std::ostream_iterator<int>(std::cout, " "));  
  46.     std::cout << std::endl;  
  47.   
  48.     std::cout << "c4 = ";  
  49.     std::copy(c4.begin(), c4.end(), std::ostream_iterator<int>(std::cout, " "));  
  50.     std::cout << std::endl;  
  51.   
  52.     std::cout << "c5 = ";  
  53.     std::copy(c5.begin(), c5.end(), std::ostream_iterator<int>(std::cout, " "));  
  54.     std::cout << std::endl;  
  55.   
  56.     std::cout << "c6 = ";  
  57.     std::copy(c6.begin(), c6.end(), std::ostream_iterator<int>(std::cout, " "));  
  58.     std::cout << std::endl;  
  59. }  
0 0
原创粉丝点击