STL notes (1): vector, list, deque

Source: Internet
Author: User
STL container Class C ++ STL embodies the idea of generic programming. It is broadly divided into container, iterator, and algorithm ). A container class can contain a group of objects of the same type or different types. An object of the same type is called a similar container class. An object of different types is called a heterogeneous container class. The container class library contains 10 types of containers, which are classified into three types:
  • Ordered containers: vector, deque, and list );
  • Associated containers: Set, multi-set, map, and multimap );
  • Container adapter: Stack, queue, and priority queue );
Comparison of the features of STL three sequential containers:
  • Vector variable array, the memory space is continuous, and the capacity will not be reduced. Supports efficient random access, that is, supports [] and at () operations. Insert/delete at the end is highly efficient, and insert/delete at other locations is less efficient;
  • List two-way linked list. The memory space is discontinuous and random access is not supported. Insert and delete operations are efficient;
  • Deque dual-end queue. The memory space is multiple consecutive memory blocks. The trace of these blocks and their sequence is saved in a ing structure. The memory available is larger, the memory size can be automatically reduced. Supports random access, but the random access performance is not good at vector. High insert efficiency at the beginning and end, and low insert and delete efficiency at other locations;
Note:
  • For vector and deque, do not cross-border when using random access;
  • The non-tail insertion and deletion of vectors and the non-beginning and end insertion and deletion of deque will lead to the movement of some elements. This requires you to consider whether the iterator, pointer, or index you are using needs to be adjusted;
Common functions of vector containers:

# Structure: vector (): # create an empty vectorvector (INT nsize): # create a vector with the number of elements nsizevector (INT nsize, const T & T ): # create a vector with the number of elements being nsize and the values being tvector (const vector &): # copy constructor vector (begin, end): # copy [begin, end) element of another array in the interval to the vector # add and delete: void push_back (const T & X): # Add an element xiterator insert (iterator it, const T & X) at the end of the vector ): # Add an element xiterator insert (iterator it, int N, const T & X) before the iterator points to the element in the vector ): # add n identical xiterator insert (iterator it, const_iterator first, const_iterator last) before the iterator points to the element in the vector ): # insert another iterator erase (iterator it) data between [first, last) of the same type vector to the iterator before the element ): # deleting the iterator in the vector pointing to the element iterator erase (iterator first, iterator last): # deleting the element void pop_back () in the vector [first, last (): # Delete void clear (): # Clear all elements in the vector # traverse: Reference at (int pos): # Return reference Reference Front () of the POs position element (): # Return reference back () of the first element: # Return the reference iterator begin (): # Return the vector header pointer, pointing to the first element iterator end (): # returns the vector tail pointer, pointing to the next position of the last element of the vector reverse_iterator rbegin (): # reverse iterator, pointing to the last element reverse_iterator rend (): # reverse iterator, point to the position before the first element # function: bool empty () const: # judge whether the vector is null. If it is null, no element int size () const in the vector: # Return the number of elements in the vector int capacity () const: # Return the maximum element value int max_size () const that can be accommodated by the current vector zhanghong: # Return the maximum allowed vector element quantity value void swap (vector &): # exchange the data void assign (int n, const T & X) of two vectors of the same type ): # Set the value of the nth element in the vector to xvoid assign (const_iterator first, const_iterator last): # Set the element in the [first, last) of the vector to the current vector element.

List container common functions:

# Structure: List <ELEM> C: # create an empty listlist <ELEM> C1 (C2): # copy the listlist of another element of the same type <ELEM> C (n ): # create a list of n elements. Each element value is determined by the default constructor. List <ELEM> C (n, ELEM): # create a list of n elements, the value of each element is elemlist <ELEM> C (begin, end): # The list is created by the iterator, And the iteration interval is [begin, end) # add or delete: void push_back (const T & X): # Add an element xvoid push_front (const T & X) at the end: # Add an element xvoid pop_back () in the header: # Delete the tail element of the container, if and only when the container is not empty void pop_front (): # Delete the first element of the container, and only when the container is not empty void remove (const T & X ): # Delete the void clear () element whose element value is equal to X in the container: # delete all elements in the container iterator insert (iterator it, const T & X ): # insert element x before the iterator pointer it, and return the X iterator pointer void insert (iterator it, size_type N, const T & X ): # insert n identical elements xvoid insert (iterator it, const_iterator first, const_iteratorlast) before the iterator pointer it: # Put [first, last) insert the iterator pointer between the iterator erase (iterator it): # Delete the iterator erase (iterator first, iterator last) element of the iterator pointer it ): # Delete the elements between the iterator pointer [first, last) # traverse: iterator begin (): # Return the iterator pointer to the first element iterator end (): # returns the iterator pointer reverse_iterator rbegin (): # returns the reverse iterator pointer of the last element, used to reverse traverse the container reverse_iterator rend (): # Return the iterator pointer reference Front (): # Return the reference Reference reference back () of the first element: # Return the reference of the last element # function: void sort (): # Sort all elements in the container. The default value is the ascending template <class Pred> void sort (PRED Pr ): # All elements in the container sort void swap (List & Str) according to the pre-determined function PR: # void unique (): # If there are duplicate Adjacent Elements in the container, only one void splice (iterator it, list & Li) is retained: # The queue merging function. Before all functions of queue Li are inserted into the iteration pointer it, X becomes the void splice (iterator it, list & Li, iterator first): # The queue Li moves the elements between [first, end) and inserts the pre-it void splice (iterator it, list & Li, iterator first, iterator last): # x move in between [first, last) element insert iterator pointer it prior void reverse (): # reverse the element order in the container

Deque container common functions
# Construct deque (): # create an empty dequedeque (INT nsize): # create a deque with the number of elements nsizedeque (INT nsize, const T & T): # create a deque, the number of elements is nsize and the values are tdeque (const deque &): # copy constructor # add or delete: void push_front (const T & X ): # Add an element xvoid push_back (const T & X) to the end of the dual-end queue: # Add an element xiterator insert (iterator it, const T & X) to the end of the dual-end queue ): # Add an element xvoid insert (iterator it, int N, const T & X) before an element in the double-end queue ): # add n identical elements xvoid insert (iterator it, const_iterator first, const_iteratorlast) before an element in the double-end queue ): # data iterator erase (iterator it) between [Forst, last) of another same type vector is inserted before an element in the double-end queue ): # delete an iterator erase (iterator first, iterator last) element in the double-end queue: # Delete the void pop_front () element in [first, last (): # Delete the first element void pop_back () in the double-ended queue: # Delete the last element void clear () in the double-ended queue: # Clear the last element in the double-ended queue # traverse: reference at (int pos): # Return reference Reference Front () of the POs position element: # Return Reference reference back of the hand element (): # Return reference of the End Element iterator begin (): # returns the vector header pointer, pointing to the first element iterator end (): # returns the pointer to the next element of the last element in the vector (not included in the vector) reverse_iterator rbegin (): # reverse iterator pointing to the last element reverse_iterator rend (): # reverse iterator pointing to the previous element of the first element # function: bool empty () const: # Whether the vector is null. If true, no element in the vector int size () const: # returns the number of elements in the vector int max_size () const: # Return the maximum number of allowed double-ended pair elements void swap (deque &): # Switch the data void assign (int n, const T & X) of two queues of the same type ): # Set the value of the nth element in the vector to X
Small Example: a small example of Vector
# Include <iostream> # include <algorithm> # include <vector> using namespace STD; void display (INT Val) {cout <Val <'';} int main () {vector <int> V; For (INT I = 0; I <10; ++ I) {v. push_back (I);} for_each (v. begin (), V. end (), display); // for_each function template cout <"\ nuse iterator \ n"; vector <int >:: iterator ITER; For (iter = v. begin (); iter! = V. end (); ++ ITER) {cout <* ITER <"" ;}cout <"---" <v. at (9); cout <"\ nthis is an two-dimen1_array \ n"; vector <int> array; vector <int> line; for (INT I = 0; I <5; ++ I) {array. push_back (line); For (Int J = 0; j <9; ++ J) {array [I]. push_back (j) ;}}for (INT I = 0; I <5; ++ I) {for (Int J = 0; j <array [I]. size (); ++ J) {cout <array [I] [J] <";}cout <Endl ;}return 0 ;}/ * result: 0 1 2 3 4 5 6 7 8 9use iterator0 1 2 3 4 5 6 7 8 9 --- 9 This Is An two-dimen1_array0 1 1 2 3 5 6 7 80 1 2 3 4 5 6 7 80 1 2 3 4 5 6 7 80 1 2 4 5 6 7 80 1 2 4 5 6 7 8 */


STL notes (1): vector, list, deque

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.