list of STL containers

Posted by PHPMagician on Sun, 20 Feb 2022 15:38:29 +0100

Traversal of container:

void printlist(list<int>& L) {
	for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}

I Constructor

List < int LST > / / list Cao Yong template class try next year, the default construction form of the object

list(beg,end);// The constructor copies the elements in the [beg, end) interval to itself

list(n,elme);// The constructor copies n elem s to itself

list(const list &lst);// copy constructor

//list container constructor
void test01() {
	//Create a list container
	list<int> L1;

	//Add data
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);

	//Traversal container
	printlist(L1);

	//Interval method construction
	list<int>L2(L1.begin(), L1.end());
	printlist(L2);

	//copy construction 
	list<int>L3(L2);
	printlist(L3);

	//n elem s
	list<int>L4(10, 1000);
	printlist(L4);
}

II Assignment and exchange

assign(beg,end);// Assign the data copy in the [beg,end) interval to itself

assign(n,elem);// Assign n elem copies to itself

list& operator=(const list &list);// Overload the equal sign operator and use the equal sign directly when using it

swap(lst);// Exchange LST with its own element

void test01() {
	//Create a list container
	list<int> L1;

	//Add data
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);

	//Traversal container
	printlist(L1);

	list<int>L2;
	L2 = L1;
	printlist(L2);
	list<int>L3;
	L3.assign(L2.begin(), L2.end());
	printlist(L3);

	list<int>L4;
	L4.assign(10, 100);//a thousand 

    //Similar to the previous container operation, if you don't understand, you can take a look at the previous functions
}

III Size operation

size();// Returns the number of elements in the container

empty();// Determine whether the container is empty

resize();// Reassign the length of the container to num. if the container becomes longer, the new position will be filled with the default value. If the container becomes shorter, the elements beyond the length of the container at the end will be deleted

resize(num,elem); Reassign the length of the container to num. if the container becomes longer, fill the new position with elem. If the container becomes shorter, the elements beyond the length of the container at the end will be deleted

The operation is the same as the previous containers. Go directly to the code of the previous containers

Code slightly

IV Insert and delete

push_back(elem);// Add an element at the end of the container

pop_back();// Delete the last element of the container

push_from(elem);// Insert an element at the beginning of the container

pop_front();// Remove an element from the beginning of the container

insert(pos,elem);// Insert a copy of the elem element in the POS position and return the location of the new data

insert(pos,n,elem);// Insert a copy of N elem elements in the POS position, and there is no return value

insert(pos,beg,end);// Insert the data of [beg, end) interval in POS position

clear();// Remove all elements

erase(beg,end);// Delete the data in the [beg, end) interval and return the position of the next data

erase(pos);// Delete the data in POS position and return to the position of the next data

remove(elem);// Delete all elements matching elem in the container

void test01() {
	list<int> L;

	L.push_back(10);
	L.push_back(20);
	L.push_back(30);
	
	L.push_front(100);
	L.push_front(200);
	L.push_front(300);
	printlist(L);

	//Tail deletion
	L.pop_back();
	printlist(L);//300 200 100 10 20

	//Header deletion
	L.pop_front();//200 100 10 20

	//Insert insert
	L.insert(L.begin(), 1000);
	printlist(L);//1000 200 100 10 20
	
	//delete
	L.erase(L.begin());
	printlist(L); //200 100 10 20

	//remove
	L.remove(10);//More than 10 pages will be deleted
	printlist(L);//200 100 20
}

V data access

front();// Returns the first element

back();// Returns the second element

It does not support [] access to container elements, nor does it support at access to container elements. The reason is that the list is essentially a linked list, which does not store data in a continuous space, and the iterator does not support random access

Code slightly

Vi Reverse and sort

reverse();// Reverse linked list

sort();// Linked list sorting

void test01() {
	list<int> L;

	L.push_back(10);
	L.push_back(20);
	L.push_back(30);
	printlist(L);//10 20 30

	L.reverse();
	printlist(L);//	30 20 10
	
	//All containers that do not support random access iterators cannot use standard algorithms
	//Containers that do not support random access iterators will provide corresponding algorithms internally
	//sort(L.begin(), L.end());// An error will be reported during operation,
	L.sort();//Just write it like this
	printlist(L);
}

Topics: Operation & Maintenance Container list