Let's take a brief look at vector in C + +

Keywords: C++ Container

1, What is a vector?

A Vector is a Sequence container that encapsulates a dynamic size array. Like any other type of container, it can hold various types of objects. It can be simply considered that a Vector is a dynamic array that can store any type.

2, Container characteristics

1. Sequence

The elements in the order container are sorted in strict linear order. The corresponding element can be accessed through its position in the sequence.

2. Dynamic array

It supports fast and direct access to any element in the sequence, and even through pointer calculation. Provides operations to add / remove elements relatively quickly at the end of a sequence.

3. Be able to sense the of memory allocator aware

The container uses a memory allocator object to dynamically handle its storage requirements.

3, Basic function implementation

1. Constructor

vector(): create an empty vector
vector(int nSize): create a vector with the number of elements being nSize
Vector (int nSize, const t& T): create a vector with the number of elements nSize and the values t
Vector (const > vector &): copy constructor
vector(begin,end): copy the elements of another array in the [begin,end) interval to the vector

2. Add function

Void push_back (const T & x): add an element X to the tail of the vector
Iterator insert (iterator it, const T & x): adds an element x before the iterator points to the element in the vector
Iterator insert (iterator it, int n, const T & x): n identical elements x are added before the iterator points to the element in the vector
iterator insert(iterator it,const_iterator first,const_iterator last): insert data between [first, last] vectors of the same type before the iterator in the vector points to the element

3. Delete function

iterator erase(iterator it): deletes the iterator pointing element in the vector
iterator erase(iterator first,iterator last): deletes the element in [first, last] in the vector
void pop_back(): deletes the last element in the vector
void clear(): clears all elements in the vector

4. Traversal function

reference at(int pos): returns the reference of the pos position element
reference front(): returns the reference of the first element
reference back(): returns the reference of the tail element
iterator begin(): returns a pointer to the vector header, pointing to the first element
iterator end(): returns the pointer to the end of the vector, pointing to the next position of the last element of the vector
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 function

bool empty() const: judge whether the vector is empty. If it is empty, there are no elements in the vector

6. Size function

int size() const: returns the number of elements in the vector
int capacity() const: returns the maximum element value that the current vector can hold
int max_size() const: returns the maximum allowed number of vector elements

7. Other functions

Void swap (vector &): exchange data of two vectors of the same type
Void assign (int n, const T & x): sets the value of the first n elements in the vector to X
void assign(const_iterator first,const_iterator last): the element in [first, last] of the vector is set as the current vector element

8. Look clearly
1.push_back Add a data at the end of the array

2.pop_back Remove the last data of the array

3.at Get the data of the number position

4.begin Get the pointer to the array header

5.end Get the last cell of the array+1 Pointer to

6.front Get a reference to the array header

7.back Get the reference of the last cell of the array

8.max_size obtain vector How big can the maximum be

9.capacity current vector Allocated size

10.size Size of data currently in use

11.resize Change the size of the currently used data, and fill in the default value if it is larger than the currently used data

12.reserve Change current vecotr Size of allocated space

13.erase Delete the data item pointed to by the pointer

14.clear Clear current vector

15.rbegin take vector The inverted start pointer returns(It's actually the original end-1)

16.rend take vector Returns the end pointer of the inversion construct(It's actually the original begin-1)

17.empty judge vector Is it empty

18.swap With another vector Exchange data

4, Basic usage

#include < vector> 
using namespace std;

5, 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 I index value is 3
  5. Vector < vector < int > > V; two-dimensional vector / / the outermost < > here must have a space. Otherwise, it cannot pass under the older compiler

6, Instance

1. Pop_back() & push_back (elem) instances remove and insert data at the end of the container

#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 to the end of the array 
    {
        obj.push_back(i);
        cout<<obj[i]<<",";    
    }
 
    for(int i=0;i<5;i++)//Remove the last data of the array 
    {
        obj.pop_back();
    }
 
    cout<<"\n"<<endl;
 
    for(int i=0;i<obj.size();i++)//size() number of actual data in the container 
    {
        cout<<obj[i]<<",";
    }
 
    return 0;
}

Output results:

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

2.clear() clears all data in the container

#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 to 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;
}

Output results:

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

3. Sorting

#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 big to small:"<<endl;
    reverse(obj.begin(),obj.end());//From big to small 
    for(int i=0;i<obj.size();i++)
    {
        cout<<obj[i]<<",";
    }
    return 0;
}

The output result is:

from small to large:
0,1,3,
From large to small:
3,1,0,

1. Note that the header file #include is required for sort
2. If you want to sort in descending order, you can override sort

bool compare(int a,int b) 
{ 
    return a< b; //In ascending order, if it is changed to return a > b, it is in descending order 
} 
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)

#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<<"Use arrays directly:"; 
    for(int i=0;i<10;i++)//Method 1 
    {
        cout<<obj[i]<<" ";
    }
 
    cout<<endl; 
    cout<<"Using iterators:" ;
    //Method 2: use an iterator to output the data in the container 
    vector<int>::iterator it;//Declare an iterator to access the vector container. Its function is to traverse or point to the elements of the vector container 
    for(it=obj.begin();it!=obj.end();it++)
    {
        cout<<*it<<" ";
    }
    return 0;
}

The output result is:

Direct use of array: 0 1 2 3 4 5 6 7 8 9
Using iterators: 0 1 2 3 4 5 6 7 8 9

5. Two methods for defining two-dimensional arrays (the results are the same)

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 the size of two-dimensional dynamic array 5 lines 
    for(int i =0; i< obj.size(); i++)//The dynamic two-dimensional array has 5 rows and 6 columns, and all values are 0 
    { 
        obj[i].resize(M); 
    } 
 
    for(int i=0; i< obj.size(); i++)//Output 2D 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 two-dimensional dynamic array 5 rows and 6 columns 
 
    for(int i=0; i< obj.size(); i++)//Output 2D 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

I wanted to summarize it myself, but I found that this article was completely written, so I took it directly and arranged version 0.0,

Posted by daimoore on Fri, 03 Dec 2021 00:00:38 -0800