当前位置:网站首页>C++ STL list

C++ STL list

2021-01-23 16:43:54 itread01

##`std::list`` `### List nature   1、 Two way concatenation   2、 Only bidirectional sequential access is supported , Subscript access is not supported ( Random access iterators )( Element random access )  3、 Because random access iterators are not supported , So it can't be used `std::sort` Sort , You need to call the member function `list::sort`  4、 stay `list` Anywhere in the world ` Insert / Delete ` The operation speed is very fast ### Container operation ####1、 Type alias | Type | Explain || ------------ | ------ || `iterator` | The iterator type of this container type || `const_iterator` | You can read elements , But you can't change the iterator type of an element || `size_type` | Unsigned integer type , Enough to store the maximum possible container size for this container type || `difference_type` | Signed integer type , Enough to store the distance between two iterators || `value_type` | Element type || `reference` | The lvalue type of the element ; And `value_type&` It means the same thing || `const_reference` | The lvalue type of the element ,( namely `const value_type&`) |####2、 Construction list ##### 1)、 Presupposition construction > Construct an empty container without any elements ```  list mylist;```##### 2)、 Fill construct construct a structure containing n A container of elements , Each element is assigned a value of val( If there is val)> `list mylist( size_type n, const value_type &val );````  list mylist (5); // contain 5 Elements , Each element is initialized by default (0) list mylist ( 5, 1314 ); // contain 5 Elements , The value of each element is 1314```##### 3)、 Scope construction constructs a container , Have and range **[** first, last **)** Just as many elements , And the order of the elements in the container corresponds to the order of the elements in the range >`list mylist( Iterator first, Iterator last );```` int myints[5] = { 75, 23, 65, 42, 13 }; list mylist ( myints, myints + 5 ); // Construct a list Containers , The element is myints A copy of the elements in ```##### 4)、 Copy construct construct a structure with `mylist` The elements in are in the same order as the elements in the container >`list yourlist( const list &mylist );````  list mylist (5, 1314); list yourlist ( mylist ); // Will mylist Copy to yourlist```##### 5)、 List construction ( List element initialization ) Construct a relationship with `il` The elements in are in the same order as the elements in the container ,`initializer_list` namely `{ }`>`list mylist( initializer_list il );```` /* The three ways of writing are the same */ list mylist ( {1, 3, 5} ); //mylist The element in is 1, 3, 5 list mylist {1, 3, 5}; list mylist = {1, 3, 5}; // Below ` Assignment ` It will be explained in ‘=’ The role of ```####3、 Assignment  (`=`)##### 1) The copy will list `x` Copy to list `mylist`>`mylist = const list &x;```` list mylist ( {1, 3, 5} ); // There's no need to give... In advance x Allocation size Because ‘=’ Will bring mylist Copy all the information of elements in list x; x = mylist;```##### 2) List copy through list copy `initializer_list` namely `{ }`>`mylist = initializer_list il;```` list mylist(3); // Pay attention to mylist Allocation size mylist = {1, 2, 3};```#### 4、 iterator ##### 1)`begin`> Returns the iterator pointing to the first element of the container ##### 2)`end`> Returns a trailing iterator , Point to the next position of the tail element :```#include #include using namespace std;int main ( void ){ list lk; for ( int i = 1; i <= 5; i++ ) // 1, 2, 3, 4, 5 lk.push_back(i); for ( auto i = lk.begin(); i != lk.end(); i++ ) cout << *i << " "; cout << endl; return 0;}```![](https://img2020.cnblogs.com/blog/2283267/202101/2283267-20210122123720327-1086126948.png)##### 4)`cbegin `> The iterator type returned is `const_iterator` So you can't dereference and modify iterators ##### 5)`cend `> The iterator type returned is `const_iterator` So you can't dereference and modify iterators ##### 5)`rbegin`> Returns the iterator pointing to the position before the tail element of the container ##### 6)`rend`> Returns the iterator pointing to the position before the first element of the container ##### 7)`crbegin `> return `const_iterator`##### 8)`crend `> return `const_iterator`#### 5、 Space ##### 1)`empty`> Returns whether the current container is empty ##### 2)`size`> The number of elements in the container ##### 3)`max_size`> The maximum number of elements that the container can store #### 6、 Access elements ##### 1)`front`> Return to `list` Reference to the first element of the container ##### 2)`back`> Return to `list` Reference to the last element of the container #### 7、 modify ##### 1)`assign``assign` It's also a way of assigning values , and ` initialization ` And ` Assignment ` The difference is :`assign` Allows us to assign values from a different but compatible type , Or from a subsequence of the container .`assign` Manipulate the element specified by the argument ( A copy of ) Replace all elements in the container on the left . for example , We can use `assign` Implementation will be a `vector` One of the paragraphs `char*` Value is given to a `list` Medium `string`:```#include #include #include using namespace std;int main ( void ){ list myString; vector myChar; myChar.push_back ( "1 2 3 4 5 6" ); myChar.push_back ( "7 8 9 10" ); myChar.push_back ( "11 12 13 14" ); myString.assign ( myChar.begin(), myChar.end() ); // You can char* Convert to string for ( auto i = myString.begin(); i != myString.end(); i++ ) { cout << *i << ' '; } cout << endl; return 0;}``` In this code, there is an error in `assign` Your call will `names` Replace the element in with a copy of the element in the scope specified by the iterator .`assign` The arguments to determine how many elements are in the container and what their values are . Output ![](https://img2020.cnblogs.com/blog/2283267/202101/2283267-20210123075756890-1014079877.png)| Type | Explain || ---- | ---- || `val` | The value of the element to be inserted | | `n` | New container size | | `first, last` | Iterators that specify the range of elements , Will [first,last) All elements in the scope are copied to list In the container | | `il` | A list element , Equivalent to list initialization |###### (1) Scope endows > `void assign ( InputIterator first, InputIterator last );`###### (2) The fill assignment accepts an integer value and an element value . It replaces the original elements in the container with a specified number of elements with the same given value > `void assign ( size_type n, const value_type &val );`###### (3) List elements are given to accept a list of elements , Will `list` The elements and sizes in the container are replaced by the values and numbers in the element list in order > `void assign ( initializer_list il );`##### 2)`emplace `##### 3)`emplace_front `> `void emplace_front ( Args&&... args );`##### 4)`emplace_back `> `void emplace_back ( Args&&... args );` Come and save konjac ,`emplace` I didn't learn to understand , Welcome to leave a message ##### 5)`push_front` stay `list` The head of the container creates a value of `val` The elements of , Accordingly make `size` Increasing the `1`> `void push_front ( const value_type &val );`##### 6)`pop_front` Delete `list` The first element of the container > `void pop_front ( void );`##### 7)`push_back` stay `list` The tail of the container creates a value of `val` The elements of , Accordingly make `size` Increasing the `1`> `void push_back ( const value_type &val );`##### 8)`pop_back` Delete `list` The tail element of the container > `void pop_back ( void );`##### 9)`insert`| Type | Explain || ---- | ---- || `position` | Where to insert the new element in the container , The new element will be inserted in `position` In front of || `val` | The value of the element to be inserted | | `n` | Number of elements to insert , The value of each number is `val` | | `first, last` | Iterators that specify the range of elements , Will [first,last) All copies of elements in the scope are inserted into `position` In front of | | `il` | List `{ }` Insert the value in `position` In front of |######(1) Insert a single element in the iterator `position` Point to the element before creating a value of `val` The elements of , Returns the iterator pointing to the newly added element >`iterator insert ( const_iterator position, const value_type& val );`######(2) Insert n The same elements are in the iterator `position` Insert before the element pointed to `n` The value is `val` The elements of , Returns an iterator pointing to a newly added element ; if `n` For `0`, Then return to `p`>`iterator insert ( const_iterator position, size_type n, const value_type& val );`######(3) Insert a list of element values `{ a, b, c, ... }``il` Is a list of element values surrounded by curly braces . Insert these given values into the iterator `position` Before pointing to the element . Returns the iterator pointing to the first newly added element ; If the list is empty , Then return to `p`>iterator insert (const_iterator position, initializer_list il);##### 10)`erase`###### a. Delete a single element delete a single element specified by an iterator return an iterator pointing to the position after the deleted element > `iterator erase ( const_iterator position );`###### b. Delete multiple elements and accept a pair of iterators , Allows us to delete an element iterator within a scope `first` Point to the first element to delete ,`last` Point to the position after the last element we want to delete > `iterator erase ( const_iterator first, const_iterator last );`##### 11)`swap` Used to exchange container contents ,`swap` Exchange two `list` The data structure inside the container ( The elements themselves are not exchanged ), So very fast , It can be done in a constant time .> `void swap ( list &x );`##### 12)`resize` Change the size of the container to `n`1、 If `n` Smaller than the size of the current container , Reduce the number of elements to the first n Elements , And delete elements that are out of range .2、 If `n` Larger than the size of the current container , Then insert any number of elements at the end to achieve n Elements . If you specify `val`, Then initialize the new element to `val` Copy of , Otherwise , They will be initialized with default values .> `void resize ( size_type n, value_type val = value_type() );`##### 13)`clear` Delete list All the elements in the container `void clear ( void )`#### 8、 operation ##### 1)`splice` For example, there are `list`a,`list`b Then `splice` The role of will be `b` Some element of ` Move ` To `a` Somewhere in the middle
###### a. Moving all elements will `x` All elements in the container are moved to the iterator of the specified container `position` The point is . The first element inserted is placed in the iterator `position` The element in the original position is placed after the last element inserted .( The size of both containers has changed ) Pay attention to and `insert` The difference between ,`splice` It's equivalent to inserting elements into `position` In front of > `void splice ( iterator position, list &x );`###### c. Single element movement will `x` In the container , iterator `i` The pointed element moves to `list` In the container , iterator `position` Before the point .> `void splice (iterator position, list& x, iterator i);`###### c. The range element moves the range `[first,last)` The elements inside come from `x` Move to `list` In the container `position` Before the point .> `void splice (iterator position, list& x, iterator first, iterator last);` About `splice` Please refer to the code for details :```// splicing lists#include #include int main (){ std::list mylist1, mylist2; std::list ::iterator it; // set some initial values: for (int i=1; i<=4; ++i) mylist1.push_back(i); // mylist1: 1 2 3 4 for (int i=1; i<=3; ++i) mylist2.push_back(i*10); // mylist2: 10 20 30 it = mylist1.begin(); ++it; // points to 2 mylist1.splice (it, mylist2); // mylist1: 1 10 20 30 2 3 4 // mylist2 (empty) // "it" still points to 2 (the 5th element) mylist2.splice (mylist2.begin(),mylist1, it); // mylist1: 1 10 20 30 3 4 // mylist2: 2 // "it" is now invalid. it = mylist1.begin(); std::advance(it,3); // "it" points now to 30 mylist1.splice ( mylist1.begin(), mylist1, it, mylist1.end()); // mylist1: 30 3 4 1 10 20 std::cout << "mylist1 contains:"; for (it=mylist1.begin(); it!=mylist1.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; std::cout << "mylist2 contains:"; for (it=mylist2.begin(); it!=mylist2.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0;}```OutPut:```mylist1 contains: 30 3 4 1 10 20mylist2 contains: 2```> The above code comes from http://www.cplusplus.com/reference/list/list/splice/##### 2)`remove` Delete with `val` The elements of > `void remove ( const value_type &val );`##### 3)`remove_if` Delete elements that meet the conditions > `void remove_if ( Predicate pred );` The code says everything ```// list::remove_if#include #include // a predicate implemented as a function:bool single_digit (const int& value) { return (value<10); }// a predicate implemented as a class:struct is_odd { bool operator() (const int& value) { return (value%2)==1; }};int main (){ int myints[]= {15,36,7,17,20,39,4,1}; std::list mylist (myints,myints+8); // 15 36 7 17 20 39 4 1 mylist.remove_if (single_digit); // 15 36 17 20 39 mylist.remove_if (is_odd()); // 36 20 std::cout << "mylist contains:"; for (std::list ::iterator it=mylist.begin(); it!=mylist.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0;}```OutPut:```mylist contains: 36 20```> The above code comes from http://www.cplusplus.com/reference/list/list/remove_if/##### 4)`unique` Delete adjacent elements , It's really destroyed .> `void unique ( void );`##### 5)`merge` Splicing `list` Containers ###### a. Splicing > `void merge ( list &x );`###### b. Stitching with comparison templates > `void merge ( list& x, Compare comp );`##### 6)`sort` Yes `list` Sort containers ###### a. Ascending sort // 0, 1, 2, 3, 4> `void sort ( void );`###### b.`cmp` Template sorting > `void sort ( Compare comp );`##### 7)`reverse` Reverse `list` The order of the elements in the container > `void reverse (

版权声明
本文为[itread01]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/01/20210123164135056h.html