A method,
function | instructions |
---|---|
pop_front() | Deletes an element at the top of the List |
pop_back() | Deletes an element at the end of the List |
erase() | This member function can delete either an element at a specified location in a List container or multiple elements within a region of the container. |
clear() | Delete all elements from the List container |
remove(val) | Deletes all elements in the container that are equal to val |
unique() | Deletes adjacent duplicate elements from the container, keeping only one copy |
remove_if() | Deletes the element in the container that meets the criteria |
1, erase,
format | instructions |
---|---|
iterator erase (iterator position); | Deletes the element in the list at the position referenced by the position iterator |
iterator erase (iterator first, iterator last); | Deletes all elements in the list that are bounded by the first and last iterators (including the elements to which first refers, but not the elements to which last refers) |
2, the unique
format | instructions |
---|---|
void unique() | Remove adjacent duplicate elements from the List |
Void Unique (BinaryPredicate) | Remove adjacent duplicate elements from a list, leaving only one copy |
Second, the instance
This is mainly the use of unique() and remove_if()
1, unique ()
STD: : list < int > listInt1,72,72,100,72,109,203,671,109,192,671 {16}, listInt2 {9201,94,43,67,81,901}; **listInt1.unique(); ** int i = 0; std::list<int>::iterator listIter1 = listInt1.begin(); for (; listIter1 ! = listInt1.end(); listIter1++) { std::cout << " unique listInt1[" << i++ << "]=" << *listIter1 << std::endl; }
The results are as follows:
According to the results:
① Only two adjacent elements can be compared. It is also applicable if three or more consecutive elements have the same value
bool test(int first, int second) { return (first <= second); } STD: : list < int > listInt1 {72,73,100,72,44,48,109,92,671,15,192,671}; listInt1.unique(test); int i = 0; std::list<int>::iterator listIter1 = listInt1.begin(); for (; listIter1 ! = listInt1.end(); listIter1++) { std::cout << " unique listInt1[" << i++ << "]=" << *listIter1 << std::endl; }
The results are as follows:
From the results we can see that
When test returns true, the element is deleted
When false is returned, the values of the two elements to be compared are retained
Such as: step is,73,100,72,44,48,109,92,671,15,192,671 {72}
①72,73 return true, delete 73; Return true, delete 100; Returns true and removes 72
Return false; leave 72,44
Return true; drop 48; Return true, delete 109; Return true; delete ’92’; Returns true, delete 671
(4, 4,15) return false
⑤ return true, delete 192; Return true delete 671
Therefore, the data in the final list is {72.44.15}
Note:
In addition to the way predicate functions are defined above, you can also use LAMBA expressions and function objects.
2, remove_if
STD: : list < int > listInt1 {72,73,100,72,44,48,109,92,671,109,15,671}; listInt1.remove_if([](int nValue) {return nValue < 100; }); int i = 0; std::list<int>::iterator listIter1 = listInt1.begin(); for (; listIter1 ! = listInt1.end(); listIter1++) { std::cout << " unique listInt1[" << i++ << "]=" << *listIter1 << std::endl; }
or
Bool test(int nValue) {bool bRet = (nValue < 100); return bRet; } STD: : list < int > listInt1 {72,73,100,72,44,48,109,92,671,109,15,671}; listInt1.remove_if(test); int i = 0; std::list<int>::iterator listIter1 = listInt1.begin(); for (; listIter1 ! = listInt1.end(); listIter1++) { std::cout << " unique listInt1[" << i++ << "]=" << *listIter1 << std::endl; }
The results are as follows:
Elements with values greater than or equal to 100 are preserved