(1).list does not support random access
(2). Elements are inserted and removed very quickly anywhere in the list, and insertion and deletion will not affect pointing to other elementsPointer to element, reference, iterator
(3).list does not support random access and does not provide subscript operators and at() functions1. Definition and assignment of list
The list template class provides two member functions push_front() and push_back() to insert new elements into the list object.
push_front() inserts a new element at the head of the container and push_back() inserts an element at the end of the container
(1).push_front() function
list<int> ListInt; list<int>::iterator iter; //Header Insert Element ListInt.push_front(1); ListInt.push_front(2); ListInt.push_front(3); ListInt.push_front(4); ListInt.push_front(5); ListInt.push_front(3); for (iter = ListInt.begin(); iter != ListInt.end(); iter++) { cout << *iter << ","; } cout << endl;
Output results:
(2).push_back()
#include <iostream> #include <list> #include <algorithm> using namespace std; int main() { list<int> ListInt; list<int>::iterator iter; //End Insert Element ListInt.push_back(1); ListInt.push_back(2); ListInt.push_back(3); ListInt.push_back(4); ListInt.push_back(5); ListInt.push_back(3); for (iter = ListInt.begin(); iter != ListInt.end(); iter++) { cout << *iter << ","; } cout << endl; return 0; }
The output is as follows:
(3). Element reset
The member function assign(), which can reset element values, is provided in a list container. The value of any element in the container can be modified by using the function assign(), or even the number of consecutive elements can be modified. The prototype of the assign() function is as follows:
void assign(const_iterator first, const_iterator last);
void assign(size_type n, const T& x=T());
#include <iostream> #include <list> #include <algorithm> using namespace std; void Print(int &ListInt) { cout << ListInt << ","; } int main() { list<int> ListOne, ListTwo, ListThree; for (int i = 0; i < 5; i++) { ListOne.push_back(i); } ListTwo.assign(ListOne.begin(), ListOne.end()); ListThree.assign(4, 5); for_each(ListOne.begin(), ListOne.end(), Print); cout << endl; for_each(ListTwo.begin(), ListTwo.end(), Print); cout << endl; for_each(ListThree.begin(), ListThree.end(), Print); cout << endl; return 0; }
The output is as follows:
(4). Deletion of elements
A list container deletes elements at the beginning of a sequence and in the queue. Its member functions include pop_back(), pop_front(), erase, and clear
Use of pop_front() and pop_back() functions
while (!ListInt.empty()) { //Return first element cout << ListInt.front() << ","; //Delete first element ListInt.pop_front(); } while (!ListInt.empty()) { //Return trailing elements cout << ListInt.back() << ","; //Delete end-of-queue element ListInt.pop_back(); }
Use of erase() function (consistent with vector s)
(5). Use of member functions remove() and remove_if
#include <iostream> #include <list> #include <algorithm> using namespace std; bool RemoveEle(int& Ele) { return (Ele % 2); } int main() { list<int> ListInt; list<int>::iterator iter; //End Insert Element for (int i = 0; i < 10; i++) { ListInt.push_back(i); } //Delete elements with a value of 5 //ListInt.remove(5); //Delete an element whose value is odd ListInt.remove_if(RemoveEle); for (iter = ListInt.begin(); iter != ListInt.end(); iter++) { cout << *iter << ","; } cout << endl; return 0; }
The results are as follows:
(6). Function unique()
(For elements stored in list containers, there may be cases where successive elements have equal values. The use of member function unique() removes duplicate elements, leaving only one, but member function unique() does not guarantee the uniqueness of element values in the sequence, it merely keeps adjacent duplicate elements one
list<int> ListInt; list<int>::iterator iter; ListInt.push_back(1); ListInt.push_back(2); ListInt.push_back(1); ListInt.push_back(3); ListInt.push_back(2); ListInt.unique(); for (iter = ListInt.begin(); iter != ListInt.end(); iter++) { cout << *iter << ","; } cout << endl;
The results of weight removal are as follows: (No changes have been made)
After sorting:
list<int> ListInt; list<int>::iterator iter; ListInt.push_back(1); ListInt.push_back(2); ListInt.push_back(1); ListInt.push_back(3); ListInt.push_back(2); ListInt.sort(); ListInt.unique(); for (iter = ListInt.begin(); iter != ListInt.end(); iter++) { cout << *iter << ","; } cout << endl;
The results are as follows: