Analysis of C++ vector 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 adds a data at the end of the array

2.pop_back removes the last data of the array

3.at get the data of the number position

4.begin to get the pointer of the array header

5.end get the pointer of the last cell + 1 of the array

6. front get the reference of array header

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

8.max_size gets the maximum size of the vector

9.capacity the size allocated by the current vector

10.size size of currently used data

11.resize changes the size of the currently used data. If it is larger than the currently used data, the user fills in the default value

12.reserve changes the size of the space allocated by the current vecotr

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

14.clear clear the current vector

15.rbegin returns the start pointer after reversing the vector (in fact, it is the original end-1)

16.rend returns the end pointer of the vector inversion construct (in fact, it is the original begin-1)

17.empty judge whether the vector is empty

18.swap exchanges data with another vector

4, Basic usage

#include < vector> 
using namespace std;

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

example

1. Pop_back() & push_back (elem) instances remove and insert 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 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;
}

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

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 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 big 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)

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<<"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 

Original address: http://blog.csdn.net/w_linux/article/details/71600574

Tags: C++ Cpp

Posted on Tue, 19 Oct 2021 03:06:03 -0400 by tpearce2