Examples of stl containers and stl containers in c ++
1. set (set) -- contains sorted data. The values of these data must be unique.
That is to say, after the set container is input, the data will be de-duplicated and sorted.
S. insert () insert an element
S. begin () s. end () returns the first and last pointers respectively.
S. clear () clears the set
The iterator set <type>: iterator variable name must be used for traversal;
2. stringstream (container)-You can separate string strings into words by spaces (it feels like a filter)
3. vector (uncertain length array) -- an array with no initial length needs to be defined
V. push_back () insert an element to the end
V. size () returns the array size.
V. pop_back () Delete the last element
V. insert (v [0] + I, a) insert element a after the I + 1 Element
Common member functions are as follows:
Basic operations
(1) header file # include <vector>.
(2) create a vector object, vector <int> vec;
(3) Insert numbers at the end: vec. push_back ();
(4) use a subscript to access the element. cout <vec [0] <endl; remember that the subscript starts from 0.
(5) use an iterator to access elements.
Vector <int>: iterator it; for (it = vec. begin (); it! = Vec. end (); it ++) cout <* it <endl;
(6) insert element: vec. insert (vec. begin () + I, a); insert a before the I + 1 element;
(7) delete element: vec. erase (vec. begin () + 2); Delete 3rd Elements
Vec. erase (vec. begin () + I, vec. end () + j); Delete interval [I, J-1]; interval starts from 0
(8) vector size: vec. size ();
(9) clear: vec. clear ();
4. map-map is the ing from key to value. For example:
First define map <string, int> m; Assign m ["green"] = 8. It looks like the advanced version of the array. You can change the representation of the lower mark.
When we are asked to calculate the number of times a word appears in an article (if it only asks one question about the number of times word a appears, we traverse it once directly, however, if it asks many times, it cannot be traversed over and over again, and Will time out.) ing is used at this time.
For example:
5. The queue (queue) is a data structure of "first-in-first-out". It seems that these containers are transforming the array and adding some member functions to make it more convenient to use, if you use arrays to implement such a function, it may be difficult to understand.
This operation:
1. Definition: queue <int> q;
2. q. push (a) adds Element a to the queue.
Q. front () access to the first element
Q. back () access the End Element
Q. pop () deletes the first element.
Q. size () returns the number of elements
Q. empty () determines whether the queue is empty.
6. stack: a data structure of "advanced and later", which is similar to that of a queue.
This operation:
1. Definition: stack <int> s;
2. s. push (a) pushes Element a to the stack.
S. top () Access stack top Element
S. pop () deletes the top element of the stack.
S. size () returns the number of elements
S. empty () determines whether the stack is empty
To be continued ......