STL_ List container

Time:2021-4-13

1、 Introduction to list

Linked list is a kind of non continuous and non sequential storage structure on physical storage unit. The logical order of data elements is realized by the pointer link order in linked list.

The linked list is composed of a series of nodes (each element in the linked list is called a node), which can be generated dynamically at runtime. Each node includes two parts: one is the data field to store the data element, the other is the pointer field to store the address of the next node.

Compared with vector’s continuous linear space, the advantage of list is to insert or delete one element at a time, that is, to configure or release the space of one element. Therefore, list is absolutely accurate in the use of space, and it is not a waste at all. Moreover, for any element insertion or removal at any location, list is always a constant time.

List and vector are the two most commonly used containers.

The list container is aTwo way linked list containerTo insert and delete elements efficiently.

List can’t access elements randomly, so it doesn’t support at. (POS) function and [] operator.

#include

2、 The default construction of the list object

List is implemented by template class, and the default construction form of the object is: listList; e.g

List lstint; // define a list container for holding ints.
List lstfloat; // define a list container to store floats.
List lststring; // define a list container for storing strings.              
//You can also set pointer type or custom type in angle brackets.

3、 The operation of adding and removing the head and tail of a list

list.push_ Back (elem); / / add an element at the end of the container

list.pop_ Back(); / / delete the last element in the container

list.push_ Front (elem); / / insert an element at the beginning of the container

list.pop_ Front(); / / removes the first element from the beginning of the container

list lstInt;
	lstInt.push_back(1);
	lstInt.push_back(3);
	lstInt.push_back(5);
	lstInt.push_back(7);
	lstInt.push_back(9);
	lstInt.pop_front();
	lstInt.pop_front();
	lstInt.push_front(11);
	lstInt.push_front(13);
	lstInt.pop_back();
	lstInt.pop_back();
// lstInt    {13,11,5}

4、 Data access of list

list.front (); / / returns the first element.

list.back (); / / returns the last element.

list lstInt;
	lstInt.push_back(1);
	lstInt.push_back(5);
	lstInt.push_back(9);

	int iFront = lstInt.front();	//1
	int iBack = lstInt.back();		//9

5、 List and iterator

list.begin (); / / returns the iterator of the first element in the container.

list.end (); / / returns the iterator after the last element in the container.

list.rbegin (); / / returns the iterator of the penultimate element in the container.

list.rend (); / / returns the iterator after the last element in the container.

for (list::iterator it=lstInt.begin(); it!=lstInt.end(); ++it)	{
		cout << *it<

6、 Construction of list object with parameters

List (beg, end); / / the constructor copies the elements in the [beg, end) interval to itself. Note that the interval is left closed and right open.

List (n, elem); / / the constructor copies n elems to itself.

List (const list & LST); / / copy constructor.

list mlist1;
	List mlist2 (10, 10); // parametric construction
	List mlist3 (mlist2); // copy construction
	list mlist4(mlist2.begin(), mlist2.end());

	for (list::iterator it = mlist4.begin(); it != mlist4.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
/*
result:
10 10 10 10 10 10 10 10 10 10
*/

7、 Assignment of list

list.assign (beg, end); / / assign the data copy in the [beg, end] interval to itself. Note that the interval is left closed and right open.

list.assign (n, elem); / / assign n elem copies to itself.

List & operator = (const list & LST); / / overload equal sign operator

list.swap (LST); / / exchange LST with its own elements.

list lstIntA,lstIntB,lstIntC,lstIntD;
	lstIntA.push_back(1);
	lstIntA.push_back(5);
	lstIntA.push_back(9);

	lstIntB.assign(lstIntA.begin(),lstIntA.end());		//1 5 9
	lstIntC.assign(5,8);							//8 8 8 8 8
	lstIntD = lstIntA;							//1 5 9
	lstIntC.swap (lstintd); // interchange

8、 The size of the list

list.size (); / / returns the number of elements in the container

list.empty (); / / judge whether the container is empty

list.resize (Num); / / re specify the length of the container to num. if the container becomes longer, the new location will be filled with the default value. If the container becomes shorter, the element whose end exceeds the length of the container is removed.

list.resize (Num, elem); / / re specify the length of the container to num. if the container becomes longer, fill in the new position with the value of elem. If the container becomes shorter, the element whose end exceeds the length of the container is removed.

list lstIntA;
	lstIntA.push_back(11);
	lstIntA.push_back(33);
	lstIntA.push_back(55);

	if (!lstIntA.empty())
	{
		int iSize = lstIntA.size();		//3
		lstIntA.resize(5);			//11 33 55 0 0
		lstIntA.resize(7,1);			//11 33 55 0 0 1 1
		lstIntA.resize(2);			//11 33
	}

9、 Insertion of list

list.insert (POS, elem); / / insert a copy of elem element at POS to return the location of new data.

list.insert (POS, N, elem); / / insert n elem data at POS, no return value.

list.insert (POS, beg, end); / / insert the data in the [beg, end] interval at the POS position, and there is no return value.

list lstA;
	list lstB;

	lstA.push_back(1);
	lstA.push_back(5);
	lstA.push_back(9);

	lstB.push_back(2);
	lstB.push_back(6);

	lstA.insert(lstA.begin(), 11);		//{11, 1, 5, 9}
	lstA.insert(++lstA.begin(),2,33);		//{11,33,33,1,5,9}
	lstA.insert(lstA.begin() , lstB.begin() , lstB.end() );	//{2,6,11,33,33,1,5,9}

10、 Deletion of list

list.clear (); / / remove all data from the container

list.erase(beg,end); //Delete [beg, end]Interval data, return the position of the next data.

list.erase (POS); / / delete the data at POS and return to the location of the next data.

lst.remove (elem); / / delete all the elements in the container that match the elem value.

//Delete the elements in the interval
//Lstint is a container declared with a list and now contains elements 1, 3, 5, 6, 9 in order.
list::iterator itBegin=lstInt.begin();
++ itBegin;
list::iterator itEnd=lstInt.begin();
++ itEnd;
++ itEnd;
++ itEnd;
lstInt.erase(itBegin,itEnd);
//At this point, the container lstint contains three elements in order: 1, 6, and 9.

//Suppose lstint contains 1,3,2,3,3,3,4,3,5,3, method 1 of deleting elements equal to 3 in the container
for(list::iterator it= lstInt.being (); it!= lstInt.end ();) // there is no need to write + + it in parentheses
{
   if(*it == 3){
        it  =   lstInt.erase (it); // take the iterator as the parameter, delete element 3, and return the position of the next element after data deletion to the iterator.
         //At this time, + + it is not executed;  
   }
   else{
       ++it;
   }
}

//Method 2 of deleting element equal to 3 in container
lstInt.remove(3);

//Delete all elements of lstint
lstInt.clear (); // the container is empty

11、 Reverse order of list

lst.reverse (); / / reverse linked list

list lstA;
	
	lstA.push_back(1);
	lstA.push_back(3);
	lstA.push_back(5);
	lstA.push_back(7);
	lstA.push_back(9);

	lstA.reverse();			//9 7 5 3 1

Recommended Today

Review of SQL Sever basic command

catalogue preface Installation of virtual machine Commands and operations Basic command syntax Case sensitive SQL keyword and function name Column and Index Names alias Too long to see? Space Database connection Connection of SSMS Connection of command line Database operation establish delete constraint integrity constraint Common constraints NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY DEFAULT […]