Some summaries of vector containers in the STL

  • 2020-04-02 01:37:31
  • OfStack

1. A brief introduction of vector

As one of the standard containers provided by STL, vector is frequently used, has a very important position, and is also very convenient to use. Vector, also known as vector, can be described graphically as an array whose length can be dynamically changed, and its function is similar to that of an array. Actually more professional described as: the vector is a versatile, able to operate a variety of data structures and algorithms of template classes and libraries, the vector is considered to be a container, because it can be like a container to store various types of objects, in a nutshell, the vector is an array to store any type of dynamic, can increase and compressed data. (note: the STL container is, from an implementation point of view, a class teplate.)

What is the main difference between a vector and an array? This is very helpful for understanding vector ~~~~

Array: Is static space distribution, usually allocated can not change, as we know the definition of an array, so the length of the array can not changed, we also can not to cross access, but it's the compiler does not cross examination, especially when it's in our programming note (many are likely to bother such mistakes!!!!!) . The array length of the general application can not meet our requirements, we have to re-apply for a larger array, and then the original array of data copied over.

Vector: What is allocated is dynamic space, that is, we found that when declaring a vector container, we can also not specify the size of the container, and the vector is automatically expanded with the addition of elements. However, we must be responsible sure vector distribution in space is continuous, also is the support of the array subscript random access, the implementation mechanism of the vector is actually: set aside part of space, and the size of the room is in a certain rate of growth, if the space is not enough to use, to ensure the continuous, must be to the new space, and then moved to the new space, the original elements and reserve a new space (and new distribution of space than the original space), the last release that part of the original space. The advantage of reserving space like this is that you don't have to reallocate space every time you add an element to the vector.

2. Functions commonly used in vecotr containers

2.1. Constructor of vector container

The declaration methods of vector container mainly include the following:

--------------------------------------------------------------------------------

The vector < Elem > v     , create an empty vector.

The vector < Elem > V1 (v)     Copy a vector.

The vector < Elem > V (n)   , create a vector with n data, all of which have been generated by default.

The vector < Elem > V (n, elem)     , create a vector with n copies of elem.

The vector < Elem > V (beg, end)     , create a vector with the [beg;end] interval.

V. ~ vector < Elem > (a)   , destroy all data and free memory.

--------------------------------------------------------------------------------

Here's a snippet of code to illustrate several common ways to declare vectors:


#include <iostream>
#include <vector>
using namespace std;
int main()
{
    vector<int>::iterator iter;
    //The first way
    vector<int> v1;
    v1.push_back(1);
    v1.push_back(2);
    v1.push_back(3);
    cout<<"The first way Output results: "<<endl;
    for(iter = v1.begin() ; iter != v1.end() ; iter++)
    {
        cout<<*iter<<" ";
    }
    cout<<endl;
    //The second way
    vector<int> v2(v1);
    cout<<"The second way Output results: "<<endl;
    for(iter = v2.begin() ; iter != v2.end() ; iter++)
    {
        cout<<*iter<<" ";
    }
    cout<<endl;
    //The third way
    vector<int> v3(3);
    cout<<"The third way Output results: "<<endl;
    for(iter = v3.begin() ; iter != v3.end() ; iter++)
    {
        cout<<*iter<<" ";
    }
    cout<<endl;
    //The fourth way
    vector<int> v4(3,4);
    cout<<"The fourth way Output results: "<<endl;
    for(iter = v4.begin() ; iter != v4.end() ; iter++)
    {
        cout<<*iter<<" ";
    }
    cout<<endl;
    //The fifth way
    vector<int> v5(v1.begin(),v1.end()-1);
    cout<<"The fifth way Output results: "<<endl;
    for(iter = v5.begin() ; iter != v5.end() ; iter++)
    {
        cout<<*iter<<" ";
    }
    cout<<endl;
    //The sixth way
    int a[] = {1,2,3,4};
    vector<int> v6(a+1,a+2);
    cout<<"The sixth way Output results: "<<endl;
    for(iter = v6.begin() ; iter != v6.end() ; iter++)
    {
        cout<<*iter<<" ";
    }
    cout<<endl;
    //
    v6.~vector<int>();
    cout<<" After the memory is freed, the result is: "<<endl;
    for(iter = v6.begin() ; iter != v6.end() ; iter++)
    {
        cout<<*iter<<" ";
    }
    cout<<endl;
    return 0;
}

Operation results:

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201309/201309251000514.jpg ">

Summary: Note this: vector < Elem > C (beg, end) declarative way, create a and [beg; the same vector end) range of elements, it is important to note is the left right open interval closed, at the same time, need to say is, whether in the STL containers and algorithms are used in the left closed right of the open interval, including v.e nd () function is returned, the position of the end of the vector is equivalent to an int a [n] a [n], and not be able to access the ~ ~ ~

2.2. Other functions commonly used in vector

--------------------------------------------------------------------------------

Says v. ssign (beg, end)   , assign the data in the [beg; end) interval to v.

Says v. ssign (n, elem)       .   Assign n copies of elem to v.

T (independence idx) says v.                               .   Returns the data referred to by the index idx, throwing out_of_range if idx crosses the line.

What exactly do v.begin ()                             .   Returns the weight of the iterator.

V.c apacity ()                     .   Returns the number of data in the container.

V.c Lear ()                               .   Removes all data from the container.

V.e mpty ()                           .   Determines if the container is empty.

V.e nd ()                                   .   Points to the last data address in the iterator.

--------------------------------------------------------------------------------

Write a program using the functions mentioned above.


#include <iostream>
#include <vector>
using namespace std;
int main()
{
    vector<int>::iterator iter;
    vector<int>v1;
    int a[] = {1,2,3,4};

    //Segment 1, exercise assign(n,t)
    v1.assign(3,2);
    cout<<"vector  Elements in: ";
    for(iter = v1.begin() ; iter != v1.end() ; ++iter)
    {
        cout<<*iter<<" ";
    }
    cout<<endl<<endl;

    //Section 2, exercise assign(beg,end)
    v1.assign(a,a+4);
    cout<<"vector  The length is: "<<v1.capacity()<<endl;
    cout<<"vector  Elements in: ";
    for(int i = 0 ; i < 4 ; ++i)
    {
        cout<<v1.at(i)<<" ";
    }
    cout<<endl<<endl;

    //Segment 3, practice the clear() function and the enpty() function
    v1.clear();
    if(v1.empty())
    {
        cout<<"vector Is empty !!!"<<endl;
    }
    return 0;
}

Operation results:

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201309/201309251000515.jpg ">

Summary: the assign function assigns values to vector variables and automatically modifies vector sizes.

--------------------------------------------------------------------------------

V.i nsert (pos, elem)     Insert a copy of elem at the pos position and return the new data location.

V. nsert(pos,n,elem) inserts data in [beg,end) interval at pos position. No return value.

V.i nsert (pos, beg, end)     Insert n elem data at the pos position. No return value.

V. pos (pos) deletes data from the pos location and returns the location of the next data.

V beg,end deletes data from the beg,end interval and returns the location of the next data.

--------------------------------------------------------------------------------

Look at the insertion and deletion of elements in vector:


#include <iostream>
#include <vector>
using namespace std;
int main()
{
    int a[] = {2,3,4};
    vector<int> v1;
    vector<int>::iterator iter;

    //Demo insert function
    v1.insert(0,1);
    v1.insert(v1.begin()+1,a,a+3);
    v1.insert(v1.begin()+4,2,5);
    cout<<"vector The data in the   : ";
    for(iter = v1.begin() ; iter != v1.end() ; ++iter)
    {
        cout<<*iter<<" ";
    }
    cout<<endl<<endl;
    //Demonstrate the erase function
    v1.erase(v1.begin(),v1.begin()+2);
    v1.erase(v1.begin()+1);
    cout<<"vector The data in the   : ";
    for(iter = v1.begin() ; iter != v1.end() ; ++iter)
    {
        cout<<*iter<<" ";
    }
    cout<<endl<<endl;
    return 0;
}

Operation results:

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201309/201309251000516.jpg ">

Summary: note that pos parameters for insert and delete operations are passed in with iterators. Note also the return values of several insert functions.

--------------------------------------------------------------------------------

V.c apacity ()   Returns the number of data in the container.

V. considering ()   Returns the number of actual data in the container.

V. eserve() reserve appropriate capacity.

V.roesize (num) respecifies the length of the queue.

V.m ax_size ()         Returns the maximum amount of data in the container.

--------------------------------------------------------------------------------


#include <iostream>
#include <vector>
using namespace std;
int main()
{
    vector<int> v1(4,1);
    vector<int>::iterator iter;
    cout<<"vector the size The value of the  : "<<v1.size()<<endl;
    cout<<"vector the capacity value  : "<<v1.capacity()<<endl;
    cout<<"vector the max_size The value of the  : "<<v1.max_size()<<endl;

    //Using the reserve function
    v1.reserve(6);
    cout<<endl;
    cout<<"vector the size The value of the  : "<<v1.size()<<endl;
    cout<<"vector the capacity value  : "<<v1.capacity()<<endl;
    cout<<"vector the max_size The value of the  : "<<v1.max_size()<<endl;
    cout<<"vector The element in  : ";
    for(iter = v1.begin() ; iter != v1.end() ; iter++)
    {
        cout<<*iter<<" ";
    }
    cout<<endl<<endl;
    //Use the resize function
    v1.resize(6,2);
    cout<<endl;
    cout<<"vector the size The value of the  : "<<v1.size()<<endl;
    cout<<"vector the capacity value  : "<<v1.capacity()<<endl;
    cout<<"vector the max_size The value of the  : "<<v1.max_size()<<endl;
    cout<<"vector The element in  : ";
    for(iter = v1.begin() ; iter != v1.end() ; iter++)
    {
        cout<<*iter<<" ";
    }
    cout<<endl<<endl;
    return 0;
}

Output results:

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201309/201309251000517.jpg ">

Summary: The reserve of vector increases the capacity of vector, but its size does not change! Resize changes the vector's capacity and also increases its size! This is because :(1) reserve reserves space for the container, but does not actually create an element object in the space, so you cannot refer to an element in the container without adding a new object. When a new element is added, the push_back()/insert() function is called. (2) resize is to change the size of the container and create the object, so after calling this function, you can refer to the object in the container, so when adding a new element, use the operator[] operator or iterator to refer to the element object. Push_back () is called after this new space.

--------------------------------------------------------------------------------

C. begin() returns the first data from an inverse queue.

C.r end ()       Returns the next location of the last data in an inverse queue.

C.p op_back ()   Delete the last data.

C.paush_back (elem) adds a data to the tail.

C.f ront ()       Returns a data.

Mount an ack ()       Returns the last data, without checking for its existence.

C1. Swap (c2)     I'm going to swap the c1 and c2 elements.

Swap (c1 and c2)       Same as above.

--------------------------------------------------------------------------------

These several functions are relatively simple, here do not write the program, interested in their own practice!!


Related articles: