当前位置:   article > 正文

C++/C++11中std::list双向链表的使用_c++ std 双向链表

c++ std 双向链表

std::list是双向链表,是一个允许在序列中任何一处位置以常量耗时插入或删除元素且可以双向迭代的顺序容器。std::list中的每个元素保存了定位前一个元素及后一个元素的信息,允许在任何一处位置以常量耗时进行插入或删除操作,但不能进行直接随机访问。

std::list和std::forward_list两个容器的设计目的是令容器任何位置的添加和删除操作都很快速。作为代价,这两个容器不支持元素的随机访问:为了访问一个元素,我们只能遍历整个容器。而且与vector、deque和array相比,这两个容器的额外内存开销也很大。

std::list与std::forward_list相比,它提供双向迭代的能力,但具有更低的空间效率。

Lists are sequence containers that allow constant time insert and erase operations anywhere within the sequence, and iteration in both directions.

List containers are implemented as doubly-linked lists; Doubly linked lists can store each of the elements they contain in different and unrelated storage locations. The ordering is kept internally by the association to each element of a link to the element preceding it and a link to the element following it.

They are very similar to forward_list: The main difference being that forward_list objects are single-linked lists, and thus they can only be iterated forwards, in exchange for being somewhat smaller and more efficient.

Compared to other base standard sequence containers (array, vector and deque), lists perform generally better in inserting, extracting and moving elements in any position within the container for which an iterator has already been obtained,and therefore also in algorithms that make intensive use of these, like sorting algorithms.

The main drawback of lists and forward_lists compared to these other sequence containers is that they lack direct access to the elements by their position.  

一个容器就是一些特定类型对象的集合。顺序容器(sequential container)为程序员提供了控制元素存储和访问顺序的能力。这种顺序不依赖于元素的值,而是与元素加入容器时的位置相对应。

        标准库中的顺序容器包括:

        (1)、vector:可变大小数组。支持快速随机访问。在尾部之外的位置插入或删除元素可能很慢。

        (2)、deque:双端队列。支持快速随机访问。在头尾位置插入/删除速度很快。

        (3)、list:双向链表。只支持双向顺序访问。在list中任何位置进行插入/删除操作速度都很快。

        (4)、forward_list:单向链表。只支持单向顺序访问。在链表任何位置进行插入/删除操作速度都很快。

        (5)、array:固定大小数组。支持快速随机访问。不能添加或删除元素。

        (6)、string:与vector相似的容器,但专门用于保存字符。随机访问快。在尾部插入/删除速度快。

        除了固定大小的array外,其它容器都提供高效、灵活的内存管理。我们可以添加和删除元素,扩张和收缩容器的大小。容器保存元素的策略对容器操作的效率有着固定的,有时是重大的影响。在某些情况下,存储策略还会影响特定容器是否支持特定操作。

        例如,string和vector将元素保存在连续的内存空间中。由于元素是连续存储的,由元素的下标来计算其地址是非常快速的。但是,在这两种容器的中间位置添加或删除元素就会非常耗时:在一次插入或删除操作后,需要移动插入/删除位置之后的所有元素,来保持连续存储。而且,添加一个元素有时可能还需要分配额外的存储空间。在这种情况下,每个元素都必须移动到新的存储空间中。

        list和forward_list两个容器的设计目的是令容器任何位置的添加和删除操作都很快速。作为代价,这两个容器不支持元素的随机访问:为了访问一个元素,我们只能遍历整个容器。而且,与vector、deque和array相比,这两个容器的额外内存开销也很大。

        deque是一个更为复杂的数据结构。与string和vector类似,deque支持快速的随机访问。与string和vector一样,在deque的中间位置添加或删除元素的代价(可能)很高。但是,在deque的两端添加或删除元素都是很快的,与list或forward_list添加删除元素的速度相当。

        forward_list和array是新C++标准增加的类型。与内置数组相比,array是一个种更安全、更容易使用的数组类型。与内置数组类似,array对象的大小是固定的。因此,array不支持添加和删除元素以及改变容器大小的操作。forward_list的设计目标是达到与最好的手写的单向链表数据结构相当的性能。因此,forward_list没有size操作,因为保存或计算其大小就会比手写链表多出额外的开销。对其他容器而言,size保证是一个快速的常量时间的操作。

        通常,使用vector是最好的选择,除法你有很好的理由选择其他容器。

        以下是一些选择容器的基本原则:

        (1)、除法你有很好的理由选择其他容器,否则应该使用vector;

        (2)、如果你的程序有很多小的元素,且空间的额外开销很重要,则不要使用list或forward_list;

        (3)、如果程序要求随机访问元素,应使用vector或deque;

(4)、如果程序要求在容器的中间插入或删除元素,应使用list或forward_list;

(5)、如果程序需要在头尾位置插入或删除元素,但不会在中间位置进行插入或删除操作,则使用deque;

(6)、如果程序只有在读取输入时才需要在容器中间位置插入元素,随后需要随机访问元素,则:首先,确定是否真的需要在容器中间位置添加元素。当处理输入数据时,通常可以很容器地向vector追加数据,然后再调用标准库的sort函数来重排容器中的元素,从而避免在中间位置添加元素。如果必须在中间位置插入元素,考虑在输入阶段使用list,一旦输入完成,将list中的内容拷贝到一个vector中。

如果你不确定应该使用哪种容器,那么可以在程序中只使用vector和list公共的操作:使用迭代器,不使用下标操作,避免随机访问。这样,在必要时选择使用vector或list都很方便。

一般来说,每个容器都定义在一个头文件中,文件名与类型名相同。即,deque定义在头文件deque中,list定义在头文件list中,以此类推。容器均定义为模板类。

顺序容器几乎可以保存任意类型的元素。特别是,我们可以定义一个容器,其元素的类型是另一个容器。这种容器的定义与任何其他容器类型完全一样:在尖括号中指定元素类型(此种情况下,是另一种容器类型)。

除了顺序容器外,标准库还定义了三个顺序容器适配器:stack、queue和priority_queue。适配器(adaptor)是标准库中的一个通用概念。容器、迭代器和函数都有适配器。本质上,一个适配器是一种机制,能使某种事物的行为看起来像另外一种事物一样。一个容器适配器接受一种已有的容器类型,使其行为看起来像一种不同的类型。

下面是从其他文章中copy的std::list测试代码,详细内容介绍可以参考对应的reference:

  1. #include "list.hpp"
  2. #include <iostream>
  3. #include <list>
  4. #include <vector>
  5. #include <string>
  6. #include <cmath>
  7. //
  8. // reference: http://www.cplusplus.com/reference/list/list/
  9. // compare only integral part:
  10. static bool mycomparison(double first, double second)
  11. {
  12. return (int(first)<int(second));
  13. }
  14. // comparison, not case sensitive.
  15. static bool compare_nocase(const std::string& first, const std::string& second)
  16. {
  17. unsigned int i = 0;
  18. while ((i<first.length()) && (i<second.length())) {
  19. if (tolower(first[i])<tolower(second[i])) return true;
  20. else if (tolower(first[i])>tolower(second[i])) return false;
  21. ++i;
  22. }
  23. return (first.length() < second.length());
  24. }
  25. // a predicate implemented as a function:
  26. static bool single_digit(const int& value) { return (value<10); }
  27. // a predicate implemented as a class:
  28. struct is_odd {
  29. bool operator() (const int& value) { return (value % 2) == 1; }
  30. };
  31. // a binary predicate implemented as a function:
  32. static bool same_integral_part(double first, double second)
  33. {
  34. return (int(first) == int(second));
  35. }
  36. // a binary predicate implemented as a class:
  37. struct is_near {
  38. bool operator() (double first, double second)
  39. {
  40. return (fabs(first - second)<5.0);
  41. }
  42. };
  43. int test_list_1()
  44. {
  45. { // list::list: Constructs a list container object, initializing its contents depending on the constructor version used
  46. std::list<int> first; // empty list of ints
  47. std::list<int> second(4, 100); // four ints with value 100
  48. std::list<int> third(second.begin(), second.end()); // iterating through second
  49. std::list<int> fourth(third); // a copy of third
  50. // the iterator constructor can also be used to construct from arrays:
  51. int myints[] = { 16, 2, 77, 29 };
  52. std::list<int> fifth(myints, myints + sizeof(myints) / sizeof(int));
  53. std::cout << "The contents of fifth are: ";
  54. for (std::list<int>::iterator it = fifth.begin(); it != fifth.end(); it++)
  55. std::cout << *it << ' ';
  56. std::cout << '\n';
  57. }
  58. { // list::assign: Assigns new contents to the list container, replacing its current contents, and modifying its size accordingly
  59. // list::size: Returns the number of elements in the list container.
  60. std::list<int> first;
  61. std::list<int> second;
  62. first.assign(7, 100); // 7 ints with value 100
  63. second.assign(first.begin(), first.end()); // a copy of first
  64. int myints[] = { 1776, 7, 4 };
  65. first.assign(myints, myints + 3); // assigning from array
  66. std::cout << "Size of first: " << int(first.size()) << '\n';
  67. std::cout << "Size of second: " << int(second.size()) << '\n';
  68. }
  69. { // list::back: Returns a reference to the last element in the list container
  70. // list::front: Returns a reference to the first element in the list container.
  71. std::list<int> mylist;
  72. mylist.push_back(10);
  73. while (mylist.back() != 0) {
  74. mylist.push_back(mylist.back() - 1);
  75. }
  76. std::cout << "mylist contains:";
  77. for (std::list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)
  78. std::cout << ' ' << *it;
  79. std::cout << '\n';
  80. mylist.push_back(77);
  81. mylist.push_back(22);
  82. mylist.front() -= mylist.back();
  83. std::cout << "mylist.front() is now " << mylist.front() << '\n';
  84. }
  85. { // list::begin: Returns an iterator pointing to the first element in the list container
  86. // list::cbegin: C++11, Returns a const_iterator pointing to the first element in the container
  87. // list::crbegin: C++11, Returns a const_reverse_iterator pointing to the last element in the container (i.e., its reverse beginning)
  88. // list::rbegin: Returns a reverse iterator pointing to the last element in the container (i.e., its reverse beginning).
  89. // list::end: Returns an iterator referring to the past-the-end element in the list container
  90. // list::cend: C++11, Returns a const_iterator pointing to the past-the-end element in the container
  91. // list::crend: C++11, Returns a const_reverse_iterator pointing to the theoretical element
  92. // preceding the first element in the container (which is considered its reverse end)
  93. // list::rend: Returns a reverse iterator pointing to the theoretical element preceding the first element
  94. // in the list container (which is considered its reverse end).
  95. int myints[] = { 75, 23, 65, 42, 13 };
  96. std::list<int> mylist(myints, myints + 5);
  97. std::cout << "mylist contains:";
  98. for (std::list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)
  99. std::cout << ' ' << *it;
  100. std::cout << '\n';
  101. for (auto it = mylist.cbegin(); it != mylist.cend(); ++it)
  102. std::cout << ' ' << *it;
  103. std::cout << '\n';
  104. for (auto rit = mylist.crbegin(); rit != mylist.crend(); ++rit)
  105. std::cout << ' ' << *rit;
  106. std::cout << '\n';
  107. std::cout << "mylist backwards:";
  108. for (std::list<int>::reverse_iterator rit = mylist.rbegin(); rit != mylist.rend(); ++rit)
  109. std::cout << ' ' << *rit;
  110. std::cout << '\n';
  111. }
  112. { // list::clear: Removes all elements from the list container (which are destroyed),
  113. // and leaving the container with a size of 0
  114. std::list<int> mylist;
  115. std::list<int>::iterator it;
  116. mylist.push_back(100);
  117. mylist.push_back(200);
  118. mylist.push_back(300);
  119. std::cout << "mylist contains:";
  120. for (it = mylist.begin(); it != mylist.end(); ++it)
  121. std::cout << ' ' << *it;
  122. std::cout << '\n';
  123. mylist.clear();
  124. mylist.push_back(1101);
  125. mylist.push_back(2202);
  126. std::cout << "mylist contains:";
  127. for (it = mylist.begin(); it != mylist.end(); ++it)
  128. std::cout << ' ' << *it;
  129. std::cout << '\n';
  130. }
  131. { // list::emplace: C++11, The container is extended by inserting a new element at position.
  132. // This new element is constructed in place using args as the arguments for its construction.
  133. // list::emplace_back: C++11, Inserts a new element at the end of the list, right after its current last element.
  134. // This new element is constructed in place using args as the arguments for its construction.
  135. // list::emplace_front: Inserts a new element at the beginning of the list, right before its current first element.
  136. // This new element is constructed in place using args as the arguments for its construction.
  137. std::list< std::pair<int, char> > mylist;
  138. mylist.emplace(mylist.begin(), 100, 'x');
  139. mylist.emplace(mylist.begin(), 200, 'y');
  140. std::cout << "mylist contains:";
  141. for (auto& x : mylist)
  142. std::cout << " (" << x.first << "," << x.second << ")";
  143. std::cout << '\n';
  144. mylist.emplace_back(10, 'a');
  145. mylist.emplace_back(20, 'b');
  146. mylist.emplace_back(30, 'c');
  147. std::cout << "mylist contains:";
  148. for (auto& x : mylist)
  149. std::cout << " (" << x.first << "," << x.second << ")";
  150. std::cout << std::endl;
  151. mylist.emplace_front(10, 'a');
  152. mylist.emplace_front(20, 'b');
  153. mylist.emplace_front(30, 'c');
  154. std::cout << "mylist contains:";
  155. for (auto& x : mylist)
  156. std::cout << " (" << x.first << "," << x.second << ")";
  157. std::cout << std::endl;
  158. }
  159. { // list::empty: Returns whether the list container is empty (i.e. whether its size is 0).
  160. std::list<int> mylist;
  161. int sum(0);
  162. for (int i = 1; i <= 10; ++i) mylist.push_back(i);
  163. while (!mylist.empty()) {
  164. sum += mylist.front();
  165. mylist.pop_front();
  166. }
  167. std::cout << "total: " << sum << '\n';
  168. }
  169. { // list::erase: Removes from the list container either a single element (position) or a range of elements ([first,last)).
  170. std::list<int> mylist;
  171. std::list<int>::iterator it1, it2;
  172. // set some values:
  173. for (int i = 1; i<10; ++i) mylist.push_back(i * 10);
  174. // 10 20 30 40 50 60 70 80 90
  175. it1 = it2 = mylist.begin(); // ^^
  176. std::advance(it2, 6); // ^ ^
  177. ++it1; // ^ ^
  178. it1 = mylist.erase(it1); // 10 30 40 50 60 70 80 90
  179. // ^ ^
  180. it2 = mylist.erase(it2); // 10 30 40 50 60 80 90
  181. // ^ ^
  182. ++it1; // ^ ^
  183. --it2; // ^ ^
  184. mylist.erase(it1, it2); // 10 30 60 80 90
  185. // ^
  186. std::cout << "mylist contains:";
  187. for (it1 = mylist.begin(); it1 != mylist.end(); ++it1)
  188. std::cout << ' ' << *it1;
  189. std::cout << '\n';
  190. }
  191. { // list::get_allocator: Returns a copy of the allocator object associated with the list container
  192. std::list<int> mylist;
  193. int * p;
  194. // allocate an array of 5 elements using mylist's allocator:
  195. p = mylist.get_allocator().allocate(5);
  196. // assign some values to array
  197. for (int i = 0; i<5; ++i) p[i] = i;
  198. std::cout << "The allocated array contains:";
  199. for (int i = 0; i<5; ++i) std::cout << ' ' << p[i];
  200. std::cout << '\n';
  201. mylist.get_allocator().deallocate(p, 5);
  202. }
  203. { // list::insert: The container is extended by inserting new elements before the element at the specified position
  204. std::list<int> mylist;
  205. std::list<int>::iterator it;
  206. // set some initial values:
  207. for (int i = 1; i <= 5; ++i) mylist.push_back(i); // 1 2 3 4 5
  208. it = mylist.begin();
  209. ++it; // it points now to number 2 ^
  210. mylist.insert(it, 10); // 1 10 2 3 4 5
  211. // "it" still points to number 2 ^
  212. mylist.insert(it, 2, 20); // 1 10 20 20 2 3 4 5
  213. --it; // it points now to the second 20 ^
  214. std::vector<int> myvector(2, 30);
  215. mylist.insert(it, myvector.begin(), myvector.end());
  216. // 1 10 20 30 30 20 2 3 4 5
  217. // ^
  218. std::cout << "mylist contains:";
  219. for (it = mylist.begin(); it != mylist.end(); ++it)
  220. std::cout << ' ' << *it;
  221. std::cout << '\n';
  222. }
  223. { // list::max_size: Returns the maximum number of elements that the list container can hold.
  224. // list::resize: Resizes the container so that it contains n elements
  225. // list::size_type: A type that counts the number of elements in a list.
  226. unsigned int i;
  227. std::list<int> mylist;
  228. i = 111111;
  229. if (i<mylist.max_size()) mylist.resize(i);
  230. else std::cout << "That size exceeds the limit.\n";
  231. std::list <int>::size_type ii;
  232. ii = mylist.max_size();
  233. std::cout << "Maximum possible length of the list is " << ii << "." << std::endl;
  234. }
  235. { // list::merge: Merges x into the list by transferring all of its elements at their respective ordered positions
  236. // into the container (both containers shall already be ordered).
  237. std::list<double> first, second;
  238. first.push_back(3.1);
  239. first.push_back(2.2);
  240. first.push_back(2.9);
  241. second.push_back(3.7);
  242. second.push_back(7.1);
  243. second.push_back(1.4);
  244. first.sort();
  245. second.sort();
  246. first.merge(second);
  247. // (second is now empty)
  248. second.push_back(2.1);
  249. first.merge(second, mycomparison);
  250. std::cout << "first contains:";
  251. for (std::list<double>::iterator it = first.begin(); it != first.end(); ++it)
  252. std::cout << ' ' << *it;
  253. std::cout << '\n';
  254. }
  255. { // list::sort: Sorts the elements in the list, altering their position within the container
  256. std::list<std::string> mylist;
  257. std::list<std::string>::iterator it;
  258. mylist.push_back("one");
  259. mylist.push_back("two");
  260. mylist.push_back("Three");
  261. mylist.sort();
  262. std::cout << "mylist contains:";
  263. for (it = mylist.begin(); it != mylist.end(); ++it)
  264. std::cout << ' ' << *it;
  265. std::cout << '\n';
  266. mylist.sort(compare_nocase);
  267. std::cout << "mylist contains:";
  268. for (it = mylist.begin(); it != mylist.end(); ++it)
  269. std::cout << ' ' << *it;
  270. std::cout << '\n';
  271. }
  272. { // list::operator=: Assigns new contents to the container, replacing its current contents, and modifying its size accordingly.
  273. std::list<int> first(3); // list of 3 zero-initialized ints
  274. std::list<int> second(5); // list of 5 zero-initialized ints
  275. second = first;
  276. first = std::list<int>();
  277. std::cout << "Size of first: " << int(first.size()) << '\n';
  278. std::cout << "Size of second: " << int(second.size()) << '\n';
  279. }
  280. { // list::push_back: Adds a new element at the end of the list container, after its current last element.
  281. // list::push_front: Inserts a new element at the beginning of the list, right before its current first element.
  282. // list::pop_back: Removes the last element in the list container, effectively reducing the container size by one.
  283. // list::pop_front: Removes the first element in the list container, effectively reducing its size by on
  284. std::list<int> mylist;
  285. int sum(0);
  286. mylist.push_back(100);
  287. mylist.push_back(200);
  288. mylist.push_back(300);
  289. while (!mylist.empty()) {
  290. sum += mylist.back();
  291. mylist.pop_back();
  292. }
  293. std::cout << "The elements of mylist summed " << sum << '\n';
  294. mylist.push_back(100);
  295. mylist.push_back(200);
  296. mylist.push_back(300);
  297. mylist.push_front(200);
  298. mylist.push_front(300);
  299. std::cout << "Popping out the elements in mylist:";
  300. while (!mylist.empty()) {
  301. std::cout << ' ' << mylist.front();
  302. mylist.pop_front();
  303. }
  304. std::cout << "\nFinal size of mylist is " << mylist.size() << '\n';
  305. }
  306. { // list::remove: Removes from the container all the elements that compare equal to val.
  307. // This calls the destructor of these objects and reduces the container size by the number of elements removed.
  308. // list::remove_if: Removes from the container all the elements for which Predicate pred returns true.
  309. // This calls the destructor of these objects and reduces the container size by the number of elements removed.
  310. int myints[] = { 89, 15, 36, 7, 17, 20, 39, 4, 1 };
  311. std::list<int> mylist(myints, myints + 9);
  312. mylist.remove(89);
  313. std::cout << "mylist contains:";
  314. for (std::list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)
  315. std::cout << ' ' << *it;
  316. std::cout << '\n';
  317. mylist.remove_if(single_digit); // 15 36 17 20 39
  318. mylist.remove_if(is_odd()); // 36 20
  319. std::cout << "mylist contains:";
  320. for (std::list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)
  321. std::cout << ' ' << *it;
  322. std::cout << '\n';
  323. }
  324. { // list::reverse: reverses the order of the elements in the list container.
  325. std::list<int> mylist;
  326. for (int i = 1; i<10; ++i) mylist.push_back(i);
  327. mylist.reverse();
  328. std::cout << "mylist contains:";
  329. for (std::list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)
  330. std::cout << ' ' << *it;
  331. std::cout << '\n';
  332. }
  333. { // list::splice: Transfers elements from x into the container, inserting them at position
  334. std::list<int> mylist1, mylist2;
  335. std::list<int>::iterator it;
  336. // set some initial values:
  337. for (int i = 1; i <= 4; ++i)
  338. mylist1.push_back(i); // mylist1: 1 2 3 4
  339. for (int i = 1; i <= 3; ++i)
  340. mylist2.push_back(i * 10); // mylist2: 10 20 30
  341. it = mylist1.begin();
  342. ++it; // points to 2
  343. mylist1.splice(it, mylist2); // mylist1: 1 10 20 30 2 3 4
  344. // mylist2 (empty)
  345. // "it" still points to 2 (the 5th element)
  346. mylist2.splice(mylist2.begin(), mylist1, it);
  347. // mylist1: 1 10 20 30 3 4
  348. // mylist2: 2
  349. // "it" is now invalid.
  350. it = mylist1.begin();
  351. std::advance(it, 3); // "it" points now to 30
  352. mylist1.splice(mylist1.begin(), mylist1, it, mylist1.end());
  353. // mylist1: 30 3 4 1 10 20
  354. std::cout << "mylist1 contains:";
  355. for (it = mylist1.begin(); it != mylist1.end(); ++it)
  356. std::cout << ' ' << *it;
  357. std::cout << '\n';
  358. std::cout << "mylist2 contains:";
  359. for (it = mylist2.begin(); it != mylist2.end(); ++it)
  360. std::cout << ' ' << *it;
  361. std::cout << '\n';
  362. }
  363. { // list::swap: Exchanges the content of the container by the content of x,
  364. // which is another list of the same type. Sizes may differ.
  365. std::list<int> first(3, 100); // three ints with a value of 100
  366. std::list<int> second(5, 200); // five ints with a value of 200
  367. first.swap(second);
  368. std::swap(first, second);
  369. std::cout << "first contains:";
  370. for (std::list<int>::iterator it = first.begin(); it != first.end(); it++)
  371. std::cout << ' ' << *it;
  372. std::cout << '\n';
  373. std::cout << "second contains:";
  374. for (std::list<int>::iterator it = second.begin(); it != second.end(); it++)
  375. std::cout << ' ' << *it;
  376. std::cout << '\n';
  377. }
  378. { // list::unique: Remove duplicate values
  379. double mydoubles[] = { 12.15, 2.72, 73.0, 12.77, 3.14,
  380. 12.77, 73.35, 72.25, 15.3, 72.25 };
  381. std::list<double> mylist(mydoubles, mydoubles + 10);
  382. mylist.sort(); // 2.72, 3.14, 12.15, 12.77, 12.77,
  383. // 15.3, 72.25, 72.25, 73.0, 73.35
  384. mylist.unique(); // 2.72, 3.14, 12.15, 12.77
  385. // 15.3, 72.25, 73.0, 73.35
  386. mylist.unique(same_integral_part); // 2.72, 3.14, 12.15
  387. // 15.3, 72.25, 73.0
  388. mylist.unique(is_near()); // 2.72, 12.15, 72.25
  389. std::cout << "mylist contains:";
  390. for (std::list<double>::iterator it = mylist.begin(); it != mylist.end(); ++it)
  391. std::cout << ' ' << *it;
  392. std::cout << '\n';
  393. }
  394. { // Performs the appropriate comparison operation between the list containers lhs and rhs
  395. std::list<int> a = { 10, 20, 30 };
  396. std::list<int> b = { 10, 20, 30 };
  397. std::list<int> c = { 30, 20, 10 };
  398. if (a == b) std::cout << "a and b are equal\n";
  399. if (b != c) std::cout << "b and c are not equal\n";
  400. if (b<c) std::cout << "b is less than c\n";
  401. if (c>b) std::cout << "c is greater than b\n";
  402. if (a <= b) std::cout << "a is less than or equal to b\n";
  403. if (a >= b) std::cout << "a is greater than or equal to b\n";
  404. }
  405. return 0;
  406. }
  407. //
  408. // reference: https://msdn.microsoft.com/en-us/library/802d66bt.aspx
  409. int test_list_2()
  410. {
  411. using namespace std;
  412. // Create an empty list c0
  413. list <int> c0;
  414. // Create a list c1 with 3 elements of default value 0
  415. list <int> c1(3);
  416. // Create a list c2 with 5 elements of value 2
  417. list <int> c2(5, 2);
  418. // Create a list c3 with 3 elements of value 1 and with the
  419. // allocator of list c2
  420. list <int> c3(3, 1, c2.get_allocator());
  421. // Create a copy, list c4, of list c2
  422. list <int> c4(c2);
  423. // Create a list c5 by copying the range c4[ first, last)
  424. list <int>::iterator c4_Iter = c4.begin();
  425. c4_Iter++;
  426. c4_Iter++;
  427. list <int> c5(c4.begin(), c4_Iter);
  428. // Create a list c6 by copying the range c4[ first, last) and with
  429. // the allocator of list c2
  430. c4_Iter = c4.begin();
  431. c4_Iter++;
  432. c4_Iter++;
  433. c4_Iter++;
  434. list <int> c6(c4.begin(), c4_Iter, c2.get_allocator());
  435. cout << "c1 =";
  436. for (auto c : c1)
  437. cout << " " << c;
  438. cout << endl;
  439. cout << "c2 =";
  440. for (auto c : c2)
  441. cout << " " << c;
  442. cout << endl;
  443. cout << "c3 =";
  444. for (auto c : c3)
  445. cout << " " << c;
  446. cout << endl;
  447. cout << "c4 =";
  448. for (auto c : c4)
  449. cout << " " << c;
  450. cout << endl;
  451. cout << "c5 =";
  452. for (auto c : c5)
  453. cout << " " << c;
  454. cout << endl;
  455. cout << "c6 =";
  456. for (auto c : c6)
  457. cout << " " << c;
  458. cout << endl;
  459. // Move list c6 to list c7
  460. list <int> c7(move(c6));
  461. cout << "c7 =";
  462. for (auto c : c7)
  463. cout << " " << c;
  464. cout << endl;
  465. // Construct with initializer_list
  466. list<int> c8({ 1, 2, 3, 4 });
  467. cout << "c8 =";
  468. for (auto c : c8)
  469. cout << " " << c;
  470. cout << endl;
  471. return 0;
  472. }
GitHubhttps://github.com/fengbingchun/Messy_Test

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/516287
推荐阅读
相关标签
  

闽ICP备14008679号