How to use vector Standard template Library in C++
Today, I would like to share with you how to use the vector standard template library in C++ related knowledge points, the content is detailed, the logic is clear, I believe most people are too aware of this knowledge, so share this article for your reference, I hope you can get something after reading this article, let's take a look at it.
One: introduction
Vector is the C++ standard template library, is a container, the bottom is an array, for continuous memory.
The namespace is std, and the header file is note: no
When vector stores data, it allocates a storage space. If it continues to store, the allocated space is full, it will allocate a larger piece of memory, copy the original data, and continue to store it. This performance will also be lost to some extent.
Second, common operations
Capacity:
A.vector size: vector.size ()
Actual amount of memory occupied by b.vector: vector.capacity ()
Modify:
a. Add element to the tail: vector.push_back ()
b. Tail delete element: vector.pop_back ()
c. Exchange two vector elements: vector.swap ()
d. Clear the vector element: vector.clear ()
e. Delete the specified element: vector.erase (it)
Iterator:
A.vector start pointer: vector.begin ()
B.vector tail pointer: vector.end () Note: the next position of the last element, similar to NULL, is not the last element of the container
Access elements:
a. Subscript access: vector [1] / / does not check whether it is out of bounds
B.at method access: vector.at (1) / / automatically checks whether it is out of bounds, and throws an exception if it is out of bounds
c. Access the first element: vector.front ()
d. Access the last element: vector.back ()
Three: storage
Simple storage
/ / Storage mode 1 vector v1 (10); for (int item0; I