C++11 List Container
Introduction to List
List is a doubly linked list container
You can efficiently insert and delete elements.
The list is not allowed to randomly access elements, so at (POS) functions and [] operators are not supported.
#include <list>
Default construct for list object
List is implemented using template class
The default constructor for the object:
List<t> LSTT
Example:
List<int> Lstint; Defines a list container that holds an int.
List<float> lstfloat; Defines a list container that holds float.
List<string> lststring; Defines a list container that holds a string.
...
You can also set pointer types or custom types within angle brackets.
Parameterized construction of a list object
Theoretical knowledge:
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)
A constructor copies n Elem to itself.
List (n)
Creates a container of n elements with an initial value of 0
List (const list &LST)
Copy constructor.
Example:
List<int> Lstinta;
Lstinta.push_back (1);
Lstinta.push_back (3);
Lstinta.push_back (5);
Lstinta.push_back (7);
Lstinta.push_back (9);
List<int> Lstintb (Lstinta.begin (), Lstinta.end ()); 1 3 5) 7 9
List<int> LSTINTC (5,8); 8 8 8) 8 8
List<int> Lstintd (Lstinta); 1 3 5) 7 9
Add remove operation for list Kinsoku
Theoretical knowledge:
List.push_back (Elem)
Add an element at the end of the container
List.pop_back ()
Delete the last element in a container
List.push_front (Elem)
Inserts an element at the beginning of the container
List.pop_front ()
Remove the first element from the beginning of the container
Example:
List<int> 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}
Data access for List
Theoretical knowledge:
List.front ();
Returns a reference to the first element.
List.back ();
Returns a reference to the last element.
Example:
List<int> Lstint;
Lstint.push_back (1);
Lstint.push_back (3);
Lstint.push_back (5);
Lstint.push_back (7);
Lstint.push_back (9);
int ifront = Lstint.front (); 1
int iback = Lstint.back (); 9
Lstint.front () = 11; 11
Lstint.back () = 19; 19
The size of the list
Theoretical knowledge:
Ls.size ()
Returns the number of elements in a container
Ls.empty ()
Determine if the container is empty
Ls.resize (num)
The container is re-specified as NUM, and if the container is longer, the new position is populated with the default value. If the container is shorter, the element at the end of the container length is removed.
Ls.resize (num, elem)
Reassign the container's length to num, and if the container is longer, fill the new position with the Elem value. If the container is shorter, the element at the end of the container length is removed.
Example:
List<int> Lstinta;
Lstinta.push_back (1);
Lstinta.push_back (3);
Lstinta.push_back (5);
if (!lstinta.empty ())
{
int isize = Lstinta.size (); 3
Lstinta.resize (5); 1 3 5) 0 0
Lstinta.resize (7,1); 1 3 5 0 0 1 1
Lstinta.resize (2); 1 3
}
List and iterator
Theoretical knowledge:
List.begin ()
Returns an iterator to the first element in a container.
List.end ()
Returns an iterator after the last element in the container.
List.rbegin ()
Returns an iterator to the first element in the container.
List.rend ()
Returns an iterator to the back of the last element in the container.
Example:
List<int> Lstint;
Lstint.push_back (1);
Lstint.push_back (3);
Lstint.push_back (5);
Lstint.push_back (7);
Lstint.push_back (9);
For (List<int>::iterator It=lstint.begin ();
It!=lstint.end (); ++it)
{
cout << *it; cout << "";
}
For (List<int>::reverse_iterator Rit=lstint.rbegin ();
Rit!=lstint.rend (); ++rit)
{
cout << *rit;
cout << "";
}
Assignment of List
Theoretical knowledge:
List.assign (Beg,end)
Assigns a copy of the data in the [Beg, end] interval to itself. Note that the interval is left-closed and right-open .
List.assign (N,elem)
Assigns an n elem copy to itself.
list& operator= (const list &LST)
Overloaded equals operator
List.swap (LST)
The LST is interchanged with its own elements.
Example:
List<int> Lstinta,lstintb,lstintc,lstintd;
Lstinta.push_back (1);
Lstinta.push_back (3);
Lstinta.push_back (5);
Lstinta.push_back (7);
Lstinta.push_back (9);
Lstintb.assign (Lstinta.begin (), Lstinta.end ()); 1 3 5) 7 9
Lstintc.assign (5,8); 8 8 8) 8 8
Lstintd = Lstinta; 1 3 5) 7 9
Lstintc.swap (LSTINTD); Swap
Insert of List
Theoretical knowledge:
Ls.insert (Pos,elem)
Inserts a copy of the Elem element at the POS location, returning the location of the new data.
Ls.insert (Pos,n,elem)
Insert n elem data at POS location, no return value.
Ls.insert (Pos,beg,end)
The data in the [Beg,end] interval is inserted at the POS location, with no return value.
Example:
List<int> Lsta;
List<int> Lstb;
Lsta.push_back (1);
Lsta.push_back (3);
Lsta.push_back (5);
Lsta.push_back (7);
Lsta.push_back (9);
Lstb.push_back (2);
Lstb.push_back (4);
Lstb.push_back (6);
Lstb.push_back (8);
Lsta.insert (Lsta.begin (), 11); {11, 1, 3, 5, 7, 9}
Lsta.insert (++lsta.begin (), 2,33); {11,33,33,1,3,5,7,9}
Lsta.insert (Lsta.begin (), Lstb.begin (), Lstb.end ()); {2,4,6,8,11,33,33,1,3,5,7,9}
Deletion of List
Theoretical knowledge:
Ls.clear ()
Remove all data from a container
Ls.erase (Beg,end)
Deletes the data for the [beg,end] interval, returning the position of the next data.
Ls.erase (POS)
Deletes the data at the POS location and returns the location of the next data.
Ls.remove (Elem)
Removes all elements in the container that match the Elem value.
Example:
Delete elements within a range
Lstint is a container declared with list<int>, and now contains an ordered 1,3,5,6,9 element.
List<int>::iterator Itbegin=lstint.begin ();
+ + Itbegin;
List<int>::iterator Itend=lstint.begin ();
+ + Itend;
+ + Itend;
+ + Itend;
Lstint.erase (Itbegin,itend);
At this point the container lstint contains three elements in 1,6,9 order.
Assuming that lstint contains 1,3,2,3,3,3,4,3,5,3, remove the method one of the elements in the container equal to 3
For (List<int>::iterator it=lstint.being (); It!=lstint.end ();)//parentheses do not need to write ++it
{
if (*it = = 3)
{
it = lstint.erase (it); The iterator is the parameter, the element 3 is removed, and the next element position after the data is deleted is returned to the iterator.
At this time, do not execute ++it;
}
Else
{
++it;
}
}
Method two for removing elements equal to 3 in a container
Lstint.remove (3);
Delete all elements of lstint
Lstint.clear (); Container is empty
Reverse Order of List
Theoretical knowledge:
Lst.reverse () Reverse linked list,
For example, LST contains the 1,3,5 element, and when this method is run, LST contains the 5,3,1 element.
Example:
List<int> 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
C++11 List Container