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

  1. Vector<type> identifier
  2. Vector<type> identifier(maximum capacity)
  3. Vector<type> identifier(maximum capacity, initial all values)
  4. Int i[5]={1,2,3,4,5}
    Vector<type> vi(I, i+2); // get the value after the index value i is 3
  5. 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

#include <string.h> #include <vector> #include <iostream> using namespace std; int main() { vector<int>obj;// create a vector storage container int for(int i=0;i<10;i++) // push_back(elem) adds data at the end of the array { obj.push_back(i); cout<<obj[i]<<","; } for(int i=0;i<5;i++)// remove the last data item from the array { obj.pop_back(); } cout<<"\n"<<endl; for(int i=0;i<obj.size();i++)// size(): the actual number of data items in the container { cout<<obj[i]<<","; } return 0; }

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

#include <string.h> #include <vector> #include <iostream> using namespace std; int main() { vector<int>obj; for(int i=0;i<10;i++)// push_back(elem) adds data at the end of the array { obj.push_back(i); cout<<obj[i]<<","; } obj.clear();// clear all data in the container for(int i=0;i<obj.size();i++) { cout<<obj[i]<<endl; } return 0; }

The output result is:

0,1,2,3,4,5,6,7,8,9,

3. Sorting

Example

#include <string.h> #include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { vector<int>obj; obj.push_back(1); obj.push_back(3); obj.push_back(0); sort(obj.begin(),obj.end());//From small to large cout<<"From small to large:"<<endl; for(int i=0;i<obj.size();i++) { cout<<obj[i]<<","; } cout<<"\n"<<endl; cout<<"From large to small:"<<endl; reverse(obj.begin(),obj.end());//From large to small for(int i=0;i<obj.size();i++) { cout<<obj[i]<<","; } return 0; }

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

#include <string.h> #include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { //Sequential access vector<int>obj; for(int i=0;i<10;i++) { obj.push_back(i); } cout<<"Directly using arrays:"; for(int i=0;i<10;i++)//Method 1 { cout<<obj[i]<<" "; } cout<<endl; cout<<"Using iterators:" ; //Method 2, using iterators to output the data in the container vector<int>::iterator it;//Declare an iterator to access the vector container, used to: traverse or point to elements of the vector container for(it=obj.begin();it!=obj.end();it++) { cout<<*it<<" "; } return 0; }

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

#include <string.h> #include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { int N=5, M=6; vector<vector<int> > obj(N); //Define a two-dimensional dynamic array with 5 rows for(int i =0; i< obj.size(); i++)//Dynamic two-dimensional array with 5 rows and 6 columns, all values are 0 { obj[i].resize(M); } for(int i=0; i< obj.size(); i++)//Output the two-dimensional dynamic array { for(int j=0;j<obj[i].size();j++) { cout<<obj[i][j]<<" "; } cout<<"\n"; } return 0; }

Method 2

#include <string.h> #include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { int N=5, M=6; vector<vector<int> > obj(N, vector<int>(M)); //Define a two-dimensional dynamic array with 5 rows and 6 columns for(int i=0; i< obj.size(); i++)//Output the two-dimensional dynamic array { for(int j=0;j<obj[i].size();j++) { cout<<obj[i][j]<<" "; } cout<<"\n"; } return 0; }

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