STLYesC ++Class Library.STLThe containers in are queue containers and associated containers, container adapter congtainer adapters: stack, queue, priority queue), bit_set, and string_package.
In the series, I will introduce the list, vector, deque and other queue containers, and association containers such as set, multisets, map, and multimaps. There are a total of seven basic container classes.
Queue container ordered container): the queue container stores the set of T-type values in a linear arrangement. Each member of the queue has its own unique position. Ordered containers include vector type, double-end queue type, and list type.
Basic container -- Vector
Vector container class): # include <vector>. vector is a dynamic array and a class template for basic arrays. Many basic operations are defined internally. Since this is a class, it will have its own constructor. Four constructor types are defined in the vector class:
1) default constructor
The default constructor constructs an empty vector with an initial length of 0, for example, vector <int> v1;
2) constructor with a single integer Parameter
This parameter describes the initial vector size. This constructor also has an optional parameter, which is a T-type instance that describes the initial values of each member of each vector type, such as: vector <int> v2 (n, 0 ); if: n is defined in advance, its member values are initialized to 0;
3) copy constructor
Copy the constructor to construct a new vector and use it as the complete copy of an existing vector, for example, vector <int> v3 (v2 );
4) constructor with two constant parameters
A constructor with two constant parameters generates a vector with an initial value as an interval. The interval is specified by a semi-open interval [first, last.
For example, vector <int> v4 (first, last)
The following example uses the fourth constructor. You can try other methods on your own.
- // Program: initialization demo
- # Include <cstring>
- # Include <vector>
- # Include <iostream>
- Using namespace std;
- Int ar [10] = {12, 45,234, 64, 12, 35, 63, 23, 12, 55 };
- Char * str = "Hello World ";
- Int main ()
- {
- Vector <int> vec1 (ar, ar + 10); // first = ar, last = ar + 10, excluding ar + 10
- Vector <char> vec2 (str, str + strlen (str); // first = str, last = str + strlen (str ),
- Cout <"vec1:" <endl;
- // Print vec1 and vec2. const_iterator is an iterator.
- // Of course, you can also use for (int I = 0; I <vec1.size (); I ++) cout <vec [I]; Output
- // Size () is a member function of the vector.
- For (vector <int >:: const_iterator p = vec1.begin (); p! = Vec1.end (); ++ p)
- Cout <* p;
- Cout <'\ n' <"vec2:" <endl;
- For (vector <char >:: const_iterator p1 = vec2.begin (); p1! = Vec2.end (); ++ p1)
- Cout <* p1;
- Cout <'\ n ';
- Return 0;
- }
To help understand the concept of vectors, we have written a small example, where the member functions of vector are used: begin (), end (), push_back (), assign (), front (), back (), erase (), empty (), at (), size ().
- # Include <iostream>
- # Include <vector>
- Using namespace std;
- Typedef vector <int> INTVECTOR; // custom type INTVECTOR
- // Test the function of the vector container.
- Int main ()
- {
- // The vec1 object is initially empty.
- INTVECTOR vec1;
- // The vec2 object initially has 10 elements with a value of 6.
- INTVECTOR vec2 (10, 6 );
- // The vec3 object initially has 3 elements with a value of 6, copying the structure
- INTVECTOR vec3 (vec2.begin (), vec2.begin () + 3 );
- // Declare a bidirectional iterator named I
- INTVECTOR: iterator I;
- // Display data in vec1 from front to back
- Cout <"vec1.begin () -- vec1.end ():" <endl;
- For (I = vec1.begin (); I! = Vec1.end (); ++ I)
- Cout <* I <"";
- Cout <endl;
- // Display data in vec2 from the beginning to the back
- Cout <"vec2.begin () -- vec2.end ():" <endl;
- For (I = vec2.begin (); I! = Vec2.end (); ++ I)
- Cout <* I <"";
- Cout <endl;
- // Display data in vec3 from front to back
- Cout <"vec3.begin () -- vec3.end ():" <endl;
- For (I = vec3.begin (); I! = Vec3.end (); ++ I)
- Cout <* I <"";
- Cout <endl;
- // Test the function for adding and inserting members. The vector cannot be inserted before.
- Vec1.push _ back (2); // Add a member from the end
- Vec1.push _ back (4 );
- Vec1.insert (vec1.begin () +); // insert member 5 at the first position of vec1
- // Insert all members of vec3 from the first position of vec1
- Vec1.insert (vec1.begin () + 1, vec3.begin (), vec3.end ());
- Cout <"after push () and insert () now the vec1 is:" <endl;
- For (I = vec1.begin (); I! = Vec1.end (); ++ I)
- Cout <* I <"";
- Cout <endl;
- // Test the value assignment function
- Vec2.assign (8, 1); // assign a value to vec2 again. The initial values of the eight members are 1.
- Cout <"vec2.assign (8, 1):" <endl;
- For (I = vec2.begin (); I! = Vec2.end (); ++ I)
- Cout <* I <"";
- Cout <endl;
- // Test the reference class function
- Cout <"vec1.front () =" <vec1.front () <endl; // The 0th member of vec1
- Cout <"vec1.back () =" <vec1.back () <endl; // The last member of vec1
- Cout <"vec1.at (4) =" <vec1.at (4) <endl; // the fifth member of vec1
- Cout <"vec1 [4] =" <vec1 [4] <endl;
- // Test removal and Deletion
- Vec1.pop _ back (); // remove the last Member from vec1
- Vec1.erase (vec1.begin () + 1, vec1.end ()-2); // delete a member
- Cout <"vec1.pop _ back () and vec1.erase ():" <endl;
- For (I = vec1.begin (); I! = Vec1.end (); ++ I)
- Cout <* I <"";
- Cout <endl;
- // Display the sequence status information
- Cout <"vec1.size ():" <vec1.size () <endl; // print the number of Members
- Cout <"vec1.empty ():" <vec1.empty () <endl; // clear
- }
Push_back () is a standard function that puts data into a vector) or a deque dual-end queue. Insert () is a similar function, but it can be used in all containers, but its usage is more complex. End () is actually adding one at the end to let the loop run correctly -- the pointer it returns points to the data closest to the array boundary.
In Java, the concept of vectors also exists. A vector in Java is a collection of objects. Each element does not have to be of the same type. It can be added or deleted and cannot be directly added to the original data type.
We hope that the introduction will help you better understand vector containers. See the next article>