Geek Learning Note--stl Container list

Source: Internet
Author: User

Introduction

Defined in header file <list>

namespacestd {    template <typename T,            typename Allocator = allocator<T> >classlist;}

A list is a container that supports fast insertion and deletion of elements in any location and does not support fast random access. It is implemented as a two-way linked list.
Compared to Forward_list, it provides two-way iteration capability, but less space efficiency.
Insert and delete operations do not invalidate pointers, references, and iterators that point to other elements.

List Operation Construction, replication and destruction
 list<Elem>C///default constructor; Create an empty list list<Elem>C (C2)//copy constructor; Create a new list as a copy of the C2 (all elements are copied) list<Elem>c = C2//copy constructor; Create a new list as a copy of the C2 (all elements are copied) list<Elem>C (RV)//Move the constructor; Use the right-value object RV to create a new list list<Elem>c = RV//Move the constructor; Use the right-value object RV to create a new list list<Elem>C (N)//Use the default constructor to create a list with n elements list<Elem>C (N,elem)//Create a list and initialize with n elem list<Elem>C (Beg,end)//Create a list and initialize using beg to the value in the end range list<Elem>C (initlist)//Create a list and initialize it with the initialization list list<Elem>c = initlist//Create a list and initialize it with the initialization listc.~List()//Destroy all elements and free up memory
Non-volatile operation
c.empty//判断容器是否为空,与size()==0相同,但可能更快//返回当前元素数量//返回可容纳的元素最大数量//判断c1与c2是否相等//判断c1与c2是否不相等,等同于!(c1==c2)//判断c1是否小于c2//判断c1是否大于c2//判断c1是否小于等于c2//判断c1是否大于等于c2
Assign Value
//将c2所有元素赋值给c//将右值对象rv的所有元素移动赋值给c//使用初始化列表进行赋值//使用初始化列表进行赋值//使用n个elem元素进行赋值c.assign(beg,end//使用beg到end范围内的元素进行赋值//交换c1和c2的数//交换c1和c2的数
element Access
//返回第一个元素,不检查第一个元素是否存在//返回最后一个元素,不检查最后一个元素是否存在
iterator-related functions
c.begin//返回一个随机存取迭代器,指向第一个元素c.end//返回一个随机存取迭代器,指向最后一个元素//返回一个随机存取常迭代器,指向第一个元素//返回一个随机存取常迭代器,指向最后一个元素//返回一个逆向迭代器,指向逆向迭代的第一个元素//返回一个逆向迭代器,指向逆向迭代的最后一个元素//返回一个逆向常迭代器,指向逆向迭代的第一个元素//返回一个逆向常迭代器,指向逆向迭代的最后一个元素
inserting and removing elements
C.push_back (Elem)//Add a elem copy at the endC.pop_back ()//Remove end element (but not callback)C.push_front (Elem)//Add a elem copy to the headC.pop_front ()//Remove head element (but not callback)C.insert (Pos,elem)//Insert a elem copy in front of the iterator position and return the position of the new elementC.insert (Pos,n,elem)Insert n elem copy in front of the iterator position and return the position of the first new element; return to the original position if no new value is insertedC.insert (Pos,beg, End)//Insert a copy of all elements of the range beg through end in front of the iterator position, and return the position of the first new element; return to the original position if no new value is insertedC.insert (Pos,initlist)Inserts a copy of all the elements of the initialization list before the position of the iterator, and returns the position of the first new element; Returns the original position if no new values are insertedC.emplace (Pos,args ...)//Insert a copy of the element initialized with args in front of the iterator position and return the position of the new elementC.emplace_back (args ...)//Add a copy of the element initialized with args at the end, no return valueC.emplace_front (args ...)//Add a copy of the element initialized with args on the head with no return valueC.erase (POS)//Remove the element of the iterator position and return the position of the next elementC.erase (Beg, End)//Remove all elements in the beg to end range and return the position of the next elementC.remove (Val)//Remove all elements with a value of ValC.remove_if (OP)//Remove all elements that satisfy the OP conditionC.resize (Num)//Set the number of elements to num (if size () increases, the extra elements are created using the default constructor)C.resize (Num, Elem)//Set the number of elements to num (if size () increases, the extra element is a copy of Elem)C.Clear()//Remove so element, empty container
Special Modification Operations
c.unique () //if there are adjacent elements with equal values, remove the repeating element  C.unique ( OP)  c.splice (POS,C2) If there are adjacent elements with equal values and if the OP condition is met //Transfer all elements within the C2 to the location indicated by POS in C1  c.splice (pos,c2,c2pos)  c.splice (pos,c2,c2beg,c2end) // Transfers all elements within the C2 from C2beg to c2end interval to the position indicated by POS within C1  c.sort  () //with operator< as the guideline, sort all elements  c.sort  (OP) //with OP as the guideline, sort all elements  c.merge  (C2) //assuming that both C1 and C2 contain ordered elements, transfer all elements of C2 to C1. and ensure that the merged list is still ordered  c. (c2,op) //assumes that both C1 and C2 contain ordered elements under the OP principle, transferring all elements of C2 to C1. and ensure that the combined list is still ordered under OP principle  C.reverse () //reverse all elements  
Chestnut
#include <list>#include <iostream>#include <algorithm>#include <iterator>using namespace STD;voidPrintlists (Const  list<int>& L1,Const  list<int>& L2) {cout<<"List1:"; Copy (L1.cbegin (), L1.cend (), ostream_iterator<int> (cout," "));cout<< Endl <<"List2:"; Copy (L2.cbegin (), L2.cend (), ostream_iterator<int> (cout," "));cout<< Endl << Endl;}intMain () {//Create two empty lists     list<int>List1, List2;//Fill two list     for(intI=0; i<6;        ++i) {list1.push_back (i);    List2.push_front (i); } printlists (List1, List2);Insert all elements of List1 before list2 the first element with a value of 3List2.splice (Find (List2.begin (), List2.end (),//target location                      3), List1);//Source listPrintlists (List1, List2);//Move the List2 head element to the tailList2.splice (List2.end (),//target locationList2,//Source listList2.begin ());//Source locationPrintlists (List1, List2);//Sort List2, assign List2 to List1, remove duplicates for List2List2.sort ();    List1 = List2;    List2.unique (); Printlists (List1, List2);//Merge the ordered List2 into the ordered List1List1.merge (LIST2); Printlists (List1, List2);return 0;}
Output
List1:0 1 2 3 4 5List2:5 4 3 2 1 0List1:List2: 5 4 0 1 2 3 4 5 3 2 1 0List1:List2: 4 0 1 2 3 4 5 3 2 1 0 5List1: 0 0 1 1 2 2 3 3 4 4 5 5List2: 0 1 2 3 4 5List1: 0 0 0 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5List2:

Geek Learning Note--stl Container list

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.