The vector class provides an alternative representation of the built-in array, which is part of the standard library introduced with standard C + +, as with the string class. In order to use vectors we must include the relevant header files:
#include <vector>
There are two different forms of using vectors. The so-called array habits and stl habits .
first, the use of the array habit method
1. Define a vector of a known length:
vector< int > Ivec(Ten); An array-like definition of int ia[ten];
Ability to access elements via ivec[ index ]
Use if (Ivec.empty ()) to infer whether it is empty. ivec.size () infers the number of elements.
2. The element of the vector is initialized to the default value associated with its type: the default value for arithmetic and pointer types is 0, and for class type, the default value can be obtained by invoking the default constructor for this class , and we can also provide an explicit initial value for each element to complete the initialization, such as
vector< int > Ivec (10,-1);
Defines Ivec it consists of 10 elements of type int each element is initialized to-1
For built-in arrays we can explicitly initialize the elements of an array to a set of constant values, such as:
int ia[6] = {-2,-1, 0, 1, 2, 1024};
we cannot explicitly initialize vectors in the same way . However, it is possible to initialize a vector to all or part of an existing array. You only need to specify the start address of the array you want to initialize the vector to and the next position of the array's last element, such as:
Copy the 6 elements of IA into Ivec
vector< int > Ivec (ia, ia+6);
The two pointers passed to Ivec mark the range of values used to initialize the object, and the second pointer always points to the next position of the last element to be copied , and the range of the marked elements can be a subset of the array. Like what:
Copy 3 elements ia[2], ia[3], ia[4]
vector< int > Ivec (&ia[2], &ia[5]);
3. Unlike a built-in array vectors can be initialized or assigned to another vector, such as
vector< string > Svec;
void Init_and_assign ()
{
Initializes a vector with a vector.
vector< string > User_names (SVEC);
// ...
To copy a vector and a vector.
Svec = User_names;
}
Second, STL customary use method
The customary use of vectors in STL9 is completely different. Instead of defining a vector of a known size, we define an empty vector
vector< string > text;
< Span style= "font-family: Song body; font-size:14px ">
1. We insert an element into a vector, instead of an indexed element, and assign a value to an element, such as a push_back () operation, which is A while loop that follows a vector inserts a sequence of strings from the standard input and inserts a string into the vector each time
string word;
while (cin >> word) {
Text.push_back (word);
//...
} /p>
< Span style= "font-family: Song body; font-size:14px "> Although we can still iterate through the element with subscript operators;
cout << words read is: \ n ";
for (int ix = 0; IX < text.size (); ++ix)
cout &L t;< text[IX] << "&NBSP;
cout << endl;
But it is more typical to use the vector operations set begin () and End () iterator
Yes:
cout << "Words read is: \ n";
for (Vector<string>::iterator it = Text.begin ();
It! = Text.end (); ++it)
cout << *it << ';
cout << endl
Iterator is a class in the standard library that has pointer functions
*it;
Dereference an iterator and access the actual object it points to
++it;
Move the iterator forward to point it to the next element
< Span style= "font-family: Song body; Font-size:14px ">2. Be careful not to mix these two habits, for example, the following definitions
vector< int > ivec;
Defines an empty vector to write this statement
ivec[0] = 1024;
is wrong. Because Ivec does not yet have the first element . We can only index elements that already exist in the vector the size () operation returns the number of elements included in the vector.
3. Similarly, when we define a vector with a given size, for example:
vector<int> IA (ten);
No matter what an insert operation will add the size of the vector, rather than overwriting an existing element, it may seem obvious. But the following errors are not uncommon among those who have just started to learn:
const int size = 7;
int ia[Size] = {0, 1, 1, 2, 3, 5, 8};
vector< int > Ivec (size);
for (int ix = 0; ix < size; ++ix)
Ivec.push_back (ia[IX]);
At the end of the program Ivec includes 14 Vegetarian, IA the element is inserted by the beginning of the eighth element.
Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.
C + + vector