Stack,queue,vector in C + + is a common data structure that is encapsulated in the <stack>,<queue>,<vector> header file.
Stack,queue,vector is defined as follows:
Stack<class t> S;
Queue<class t> Q;
Vector<class T> v;
- Stack Common methods:
- Push () Inserts an element into the top of the container;
- Pop () is the element that deletes the top of the container;
- Top () returns the element at the top of the container;
- Size () returns the number of elements in the container;
- Empty () is a method that checks if it is empty.
Note
- The end and begin methods are not methods in the stack;
- The size of the data can be obtained directly with the size method.
- Common methods for queue:
- Back () returns the last element reference of the queue;
- Empty () is the method of checking whether it is empty;
- Front () Gets the first element reference in the queue;
- Push () Adds a data at the end of the queue;
- Pop () deletes a data from the queue header;
- The number of elements in the size () queue.
Note
- Push_back and Push_front in the VC6.0 are compiled do not pass;
- The size of the data can be obtained directly by using the size method;
- End and begin are also compiled in VC6.0 that cannot be passed.
a 1.vector container is a template class that can hold objects of any type (but must be of the same class object). The vector object can add elements efficiently at run time, and the elements in the vector are stored continuously; the vector's constructor:
vector<string> v1;//Create an empty container whose object type is a string class;
vector<string> v2 (10);//Create a container of 10 string class objects with an initial value (that is, an empty string);
vector<string> v3 (5, "Hello");//Create a container of 5 string class objects with a value of "Hello";
Vector<string> v4 (V3.begin (), V3.end ()); The V4 is the same container as the V3 (full replication).
2.void push_back (x);//Add an element to the end of the container;
3.void pop_back ();//Popup The last element in the container (the container must be non-empty);
4.vector<int>::iterator Iter=v.begin ();//Initialize the iterator so that he points to the beginning of V;
5.iterator Erase (iterator it); Delete the specified element and return the position of an element after the element is deleted (if no element, return end ());
6.void Erase (iterator first, iterator last);//NOTE: After deleting an element, the iterator corresponding to the element after the delete point is no longer valid;
7.void Clear ();//empty container, equivalent to call erase (begin (), end ());
8.void Reserve (size_type n);//Ensure capacity () >= N.
Note
Iterator Erase (iterator it); The return value of the method is the address of the next element of it, and the address is usually changed to the IT variable;
The Bengin and end methods are vector methods and must not be confused with stack and queue;
the way to add deleted elements is push_back and pop_back, not with stack and queue confusion;
iterator is a member variable of a vector not with stack and queue are confused.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Simple analysis of common methods of easy confusion in C + + Stack,queue,vector