I. What is vector?
A vector is a sequence container that encapsulates a dynamically sized array. Like any other type of container, it can store objects of various types. It can simply be thought of as a dynamic array that can hold elements of any type.
II. Container Characteristics
1. Ordered sequence
Elements in a sequence container are arranged in a strict linear order. The corresponding element can be accessed by its position in the sequence.
2. Dynamic array
Supports fast direct access to any element in the sequence, and this can even be done through pointer arithmetic. It provides relatively fast addition/deletion of elements at the end of the sequence.
3. Allocator-aware
The container uses an allocator object to dynamically handle its storage needs.
III. Basic Function Implementation
1. Constructors- vector(): creates an empty vector
- vector(int nSize): creates a vector with nSize elements
- vector(int nSize, const t& t): creates a vector with nSize elements, all with value t
- vector(const vector&): copy constructor
- vector(begin,end): copies elements from another array in the range [begin,end) into the vector
2. Adding functions
- void push_back(const T& x): adds an element X to the end of the vector
- iterator insert(iterator it, const T& x): inserts an element x before the element pointed to by the iterator in the vector
- iterator insert(iterator it, int n, const T& x): inserts n identical elements x before the element pointed to by the iterator in the vector
- iterator insert(iterator it, const_iterator first, const_iterator last): inserts data from another vector of the same type in the range [first,last) before the element pointed to by the iterator in the vector
3. Deleting functions
- iterator erase(iterator it): deletes the element pointed to by the iterator in the vector
- iterator erase(iterator first, iterator last): deletes elements in the vector in the range [first,last)
- void pop_back(): deletes the last element in the vector
- void clear(): clears all elements in the vector
4. Traversal functions
- reference at(int pos): returns a reference to the element at position pos
- reference front(): returns a reference to the first element
- reference back(): returns a reference to the last element
- iterator begin(): returns the head pointer of the vector, pointing to the first element
- iterator end(): returns the tail pointer of the vector, pointing to the position after the last element
- reverse_iterator rbegin(): reverse iterator, pointing to the last element
- reverse_iterator rend(): reverse iterator, pointing to the position before the first element
5. Judgment functions
- bool empty() const: determines whether the vector is empty; if empty, the vector has no elements
6. Size functions
- int size() const: returns the number of elements in the vector
- int capacity() const: returns the maximum number of elements the current vector can hold
- int max_size() const: returns the maximum allowable number of elements in the vector
7. Other functions
- void swap(vector&): swaps the data of two vectors of the same type
- void assign(int n, const T& x): sets the values of the first n elements in the vector to x
- void assign(const_iterator first, const_iterator last): sets the elements in the vector to the elements in [first,last)
8. For clarity
1. push_back: adds a data item at the end of the array
2. pop_back: removes the last data item from the array
3. at: gets the data at the numbered position
4. begin: gets the pointer to the head of the array
5. end: gets the pointer to one past the last element of the array
6. front: gets a reference to the head of the array
7. back: gets a reference to the last element of the array
8. max_size: gets the maximum possible size of the vector
9. capacity: the current allocated size of the vector
10. size: the size of the data currently in use
11. resize: changes the size of the currently used data; if it is larger than the current usage, it fills in default values
12. reserve: changes the size of the space currently allocated for the vector
13. erase: deletes the data item pointed to by the pointer
14. clear: clears the current vector
15. rbegin: returns the start pointer of the reversed vector (in fact, the original end - 1)
16. rend: returns the end pointer of the reversed vector (in fact, the original begin - 1)
17. empty: determines whether the vector is empty
18. swap: exchanges data with another vector
IV. Basic Usage
#include < vector> using namespace std;
V. Brief Introduction
- Vector<type> identifier
- Vector<type> identifier(maximum capacity)
- Vector<type> identifier(maximum capacity, initial all values)
- Int i[5]={1,2,3,4,5}
Vector<type> vi(I, i+2); // get the value after the index value i is 3 - Vector< vector< int> >v; // two-dimensional vector. Note: the outer <> must have a space; otherwise it will not compile on older compilers.
Example
1. pop_back() & push_back(elem) example: removing and inserting data at the end of the container
Example
The output result is:
0,1,2,3,4,5,6,7,8,9, 0,1,2,3,4,
2. clear() clears all data in the container
Example
The output result is:
0,1,2,3,4,5,6,7,8,9,
3. Sorting
Example
The output result is:
从小到大: 0,1,3, 从大到小: 3,1,0,
1. Note that sort requires a header file#include <algorithm>
2. If you want sort to sort in descending order, you can rewrite sort
bool compare(int a,int b)
{
return a< b; //升序排列,如果改为return a>b,则为降序
}
int a[20]={2,4,1,23,5,76,0,43,24,65},i;
for(i=0;i<20;i++)
cout<< a[i]<< endl;
sort(a,a+20,compare);
4. Access (direct array access & iterator access)
Example
The output result is:
直接利用数组:0 1 2 3 4 5 6 7 8 9 利用迭代器:0 1 2 3 4 5 6 7 8 9
5. Two methods for defining a two-dimensional array (same result)
Method 1
Method 2
The output result is:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Original address: http://blog.csdn.net/w_linux/article/details/71600574