Recently I learned from other people's source code and came into contact with the Vector Template Class. I found it very useful. Let's record it and study it.
Let's take a look at the usage: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PC9wPgo8cHJlIGNsYXNzPQ = "brush: java;"> std: vector V;Std is the namespace
T indicates the object type. It can be an integer int, a string, a structure struct, or a custom class;
In my understanding, the program dynamically manages the memory by extending the array.
For details, click the open link (Wikipedia)
Write the following code:
If I need an object, it has four attributes, and I need to read data from the xml file, assign them to the object, add them to the array, and perform unified operations on them.
You can also use the class + array method, but it is not efficient.
Let's see how struct + vector is implemented:
. H
struct config{string name;string id;string price;string description;}
vector
item_config;
. Cpp
Config item; item. name = "bomb"; item. id = "0"; item. price = "100"; item. description = "Earth bomb, powerful but cheap"; item_config.push_back (item); // Add it to victor char name = item_config [0]. name; // obtain
Is it convenient? If the array + class code is used, it goes up and no new is required.->
- Access Element Method
vec[i]
-Element reference with an access index value of I. (The index value starts from zero, so the first element is vec [0].)
vec.at(i)
-When an element with an access index of I is referenced, array boundary check is performed for at () access. If the access is out of bounds, an exception is thrown, which is the only difference from operator.
vec.front()
-Return the reference of the first element of the vector.
vec.back()
-Return the reference of the final element of the vector.
- Method for adding or removing elements
vec.push_back()
-Add elements to the tail end of the vector and configure the memory if necessary.
vec.pop_back()
-Delete the element at the end of a vector.
vec.insert()
-Insert one or more elements to any position in the vector.
vec.erase()
-Delete one or more elements in a vector.
vec.clear()
-Clear all elements.
- Get length/Capacity
vec.size()
-Obtain the number of elements currently held by the vector.
vec.empty()
-If the vector is empty, true is returned.
vec.capacity()
-Obtain the maximum number of elements that a vector can currently accommodate. This method is related to the memory configuration. It usually only increases and does not decrease because the elements are deleted.
- Reconfigure/Reset Length
vec.reserve()
-If necessary, you can change the vector capacity (configure more memory ). In many practical STL jobs, the capacity can only be increased and cannot be reduced.
vec.resize()
-Changes the number of elements currently held by a vector.
- Iteration (Iterator)
vec.begin()
-Return an Iterator pointing to the first element of the vector.
vec.end()
-Return an Iterator pointing to the next position of the element at the end of the vector (Note: it is not the last element ).
vec.rbegin()
-Return a reverse Iterator pointing to the final element of the vector.
vec.rend()
-Return an Iterator pointing to the first element of the vector.