C++

Most Useful C++ Vector Member Functions

C++ vector member functions can be put under the following categories: construction, capacity, iterators, element access, and modifiers. Each of these categories has many functions. All these functions are not used in many applications. This article explains the most useful of these functions. With the exception of the modifiers category, not more than three functions for each category are explained in this article. The modifiers category can be broken down into more sub categories. In each of these sub categories, not more than three functions will be explained. If more than three functions are to be explained for a given category, then they will be taught by way of illustration.

A program to use C++ vector should begin with,

    #include <iostream>

    #include <vector>

Article Content

Construction/Destruction

The following code segments show different ways of creating the same vector:

    vector <float> vtr;

        vtr.push_back(5.5);

        vtr.push_back(6.6);

        vtr.push_back(7.7);

        vtr.push_back(8.8);

        vtr.push_back(9.9);

    vector <float> vtr(3);    //with initial number of elements

        vtr[0] = 5.5;

        vtr[1] = 6.6;

        vtr[2] = 7.7;

    vector <float> vtr(5, 0.0);    //No. Elements:5; each value:0.0

    vector <float> vtr{5.5, 6.6, 7.7, 8.8, 9.9};    //initializing

    vector <float> vtr = {5.5, 6.6, 7.7, 8.8, 9.9};    //constructing and copying

    vector <float> vtr;

    vtr = {5.5, 6.6, 7.7, 8.8, 9.9};

    vector <float> vtr1{5.5, 6.6, 7.7, 8.8, 9.9};

    vector <float> vtr2(vtr1);

    const vector <float> vtr = {5.5, 6.6, 7.7, 8.8, 9.9};

A const vector is a vector whose elements cannot be changed. The values are read-only.

Destruction

To destroy a vector, just let it go out of scope.

Capacity

size() const noexcept

The number of elements in a vector is returned by this member function. With the following code segment, the output is 5:

    vector <float> vtr = {5.5, 6.6, 7.7, 8.8, 9.9};


    float sz = vtr.size();


    cout << sz << '\n';


empty() const noexcept

This method returns true (1) if the vector has no element and false (0) if the vector has at least one element. With the following code, the output is 1 (for true):

    vector <float> vtr = {};

    bool bl = vtr.empty();

    cout << bl << '\n';

Iterator and Vector Access

An iterator is an elaborated pointer. When the vector, vtr has been created, vtr.begin() would return an iterator, pointing to the first element of the list. It can then be incremented to access the elements after the first, accordingly.

When the vector, vtr has been created, vtr.end() would return an iterator, pointing just after the last element of the list. It can then be decremented to access the last element and elements before the last, accordingly. The following program illustrates this:

#include <iostream>

#include <vector>

using namespace std;

int main()

{

    vector <float> vtr = {5.5, 6.6, 7.7, 8.8, 9.9};

    vector<float>::iterator iterB = vtr.begin();

    iterB++;

    vector<float>::iterator iterE = vtr.end();

    iterE--;

    cout << *iterB << ", " << *iterE << ' ' << endl;

    *iterB = 66.66; *iterE = 99.99;

    cout << *iterB << ", " << *iterE << ' ' << endl;

    return 0;

}

The output is:

    6.6, 9.9

    66.66, 99.99

The values of two elements were accessed, read and changed by two iterators.

Element Access

at(i)

This is similar to vtr[i], and it is better. It can be used to read or change the value of an element. Index counting begins from zero. The reader can test the following program:

#include <iostream>

#include <vector>

using namespace std;

int main()

{

    vector <float> vtr = {5.5, 6.6, 7.7, 8.8, 9.9};

    cout << vtr[1] << ", " << vtr[4] << ' ' << endl;

    vtr[1] = 66.66; vtr[4] = 99.99;

    cout << vtr[1] << ", " << vtr[4] << ' ' << endl;

    return 0;

}

The output is:

   6.6, 9.9

    66.66, 99.99

The values of two elements were accessed, read and changed through referencing.

Returning the First Value

The following code returns (copies out) the first element:

    vector <float> vtr = {5.5, 6.6, 7.7, 8.8, 9.9};

    float val = vtr.front();

    cout << val << endl;

The output is, 5.5. The member function used here is: front().

Returning the Last Value

The following code returns (copies out) the last element:

    vector <float> vtr = {5.5, 6.6, 7.7, 8.8, 9.9};

    float val = vtr.back();

    cout << val << endl;

The output is, 9.9. The member function used here is: back().

Push Back and Pop Back

push_back()

An element can be appended at the back of the vector. The value can be a literal or be represented by the identifier. The following code illustrates this:

    float flt = 10.10;

    vector <float> vtr = {5.5, 6.6, 7.7, 8.8, 9.9};

    vtr.push_back(flt);

    for (int i=0; i<vtr.size(); i++)

        cout << vtr[i] << ", ";

    cout << endl;

The original list is:

    5.56.67.78.89.9

The new list is:

    5.56.67.78.89.910.1

pop_back()

The last element can be removed from the vector. The following code illustrates this:

    vector <float> vtr = {5.5, 6.6, 7.7, 8.8, 9.9};

    vtr.pop_back();

    for (int i=0; i<vtr.size(); i++)

        cout << vtr[i] << ", ";

    cout << endl;

The original list is:

    5.56.67.78.89.9

The new list is:

    5.56.67.78.8

Note: The vector does not have the push_front() and pop_front() member functions.

Inserting

Inserts in front of the element pointed to, by the iterator.

insert(p, e)

This inserts an element before the element pointed to by the iterator, p. e can be a literal or identifier. Illustration:

    float flt = 7.0;

    vector <float> vtr = {5.5, 6.6, 7.7, 8.8, 9.9};

    vector<float>::iterator iterB = vtr.begin();

    iterB++; iterB++;
   
    vtr.insert(iterB, flt);

    for (int i=0; i<vtr.size(); i++)

        cout << vtr[i] << ", ";

    cout << endl;

The original list is:

    5.56.67.78.89.9

The new list is:

    5.56.677.78.89.9

This insert function returns an iterator (not coded) that points to the element inserted.

insert(p, il)

A list can be inserted in front of the element pointed to by the iterator. Illustration:

    vector <float> vtr = {5.5, 6.6, 7.7, 8.8, 9.9};

    vector<float>::iterator iterB = vtr.begin();

    iterB = iterB + 2;

    vtr.insert(iterB, {7.1, 7.2, 7.3});

    for (int i=0; i<vtr.size(); i++)

        cout << vtr[i] << ", ";

    cout << endl;

The original list is:

    5.56.67.78.89.9

The new list is:

    5.5, 6.6, 7.1, 7.2, 7.3, 7.7, 8.8, 9.9

This insert function returns an iterator (not coded) that points to the first element inserted.

insert(p,n,t)

Inserts the same value t, n times, in front of the element pointed to by p. Illustration:

    vector <float> vtr = {5.5, 6.6, 7.7, 8.8, 9.9};

    vector<float>::iterator iterB = vtr.begin();

    iterB = iterB + 2;

    vtr.insert(iterB, 3, 7.1);

    for (int i=0; i<vtr.size(); i++)

        cout << vtr[i] << ", ";

    cout << endl;

The original list is:

    5.56.67.78.89.9

The new list is:

    5.56.67.17.17.17.78.89.9

This insert function returns an iterator (not coded) that points to the first element inserted.

insert(p, i, j)

A range can be copied from another vector and inserted into the vector of interest. The following code illustrates this:

    vector <float> vtr1 = {7.1, 7.2, 7.3, 7.4, 7.5};

    vector<float>::iterator iterB1 = vtr1.begin();

    iterB1 = iterB1 + 1;

    vector<float>::iterator iterE1 = vtr1.end();

    iterE1 = iterE1 - 2;

    vector <float> vtr2 = {5.5, 6.6, 7.7, 8.8, 9.9};

    vector<float>::iterator iterB2 = vtr2.begin();

    iterB2 = iterB2 + 2;

    vtr2.insert(iterB2, iterB1, iterE1);

    for (int i=0; i<vtr2.size(); i++)

        cout << vtr2[i] << ", ";

    cout << endl;

The original list for vtr2 is:

    5.56.67.78.89.9

The new list for vtr2 is:

    5.56.67.27.37.78.89.9

The range identified to be copied is [7.2, 7.3, 7.4), indicated as [i, j), meaning the last value, 7.4 is not included.

This insert function returns an iterator (not coded) that points to the first element inserted.

Assigning Elements (Replacements)

Assigning to elements means replacing the elements with copies. All assignment member functions, return void.

assign(il)

A list can replace all the elements in the vector in question. The list can be longer or shorter. Illustration for short list:

    vector <float> vtr = {5.5, 6.6, 7.7, 8.8, 9.9};

    vector<float>::iterator iterB = vtr.begin();

    iterB = iterB + 2;

    vtr.assign({55.55, 66.66, 77.77});

    for (int i=0; i<vtr.size(); i++)

        cout << vtr[i] << ", ";

    cout << endl;

The original list is:

    5.56.67.78.89.9

The new list is:

    55.55, 66.66, 77.77

Illustration for longer list:

    vector <float> vtr = {5.5, 6.6, 7.7, 8.8, 9.9};

    vector<float>::iterator iterB = vtr.begin();

    iterB = iterB + 2;

    vtr.assign({55.55, 66.66, 77.77, 88.88, 99.99, 1010.1111});

    for (int i=0; i<vtr.size(); i++)

        cout << vtr[i] << ", ";

    cout << endl;

The original list is:

    5.56.67.78.89.9

The new list is:

    55.55, 66.66, 77.77, 88.88, 99.99, 1010.11

assign(n,t)

The same value, repeated a number of times, can replace all the elements in the vector in question. The new list can be longer or shorter. Illustration for short list:

    vector <float> vtr = {5.5, 6.6, 7.7, 8.8, 9.9};

    vector<float>::iterator iterB = vtr.begin();

    iterB = iterB + 2;

    vtr.assign(2, 77.77);

    for (int i=0; i<vtr.size(); i++)

        cout << vtr[i] << ", ";

    cout << endl;

The original list is:

    5.56.67.78.89.9

The new list is:

   77.77, 77.77

assign(i,j)

A range can be copied from another vector and be used to replace all the content of the vector of interest. The following code illustrates this:

    vector <float> vtr1 = {7.1, 7.2, 7.3, 7.4, 7.5};

    vector<float>::iterator iterB1 = vtr1.begin();

    iterB1 = iterB1 + 1;

    vector<float>::iterator iterE1 = vtr1.end();

    iterE1 = iterE1 - 2;


    vector <float> vtr2 = {5.5, 6.6, 7.7, 8.8, 9.9};

    vector<float>::iterator iterB2 = vtr2.begin();

    iterB2 = iterB2 + 2;

    vtr2.assign(iterB1, iterE1);

    for (int i=0; i<vtr2.size(); i++)

        cout << vtr2[i] << ", ";

    cout << endl;

The original list for vtr2 is:

   5.56.67.78.89.9

The new list for vtr2 is:

    7.2, 7.3

The range identified to be copied is [7.2, 7.3, 7.4), indicated as [i, j), meaning the last value, 7.4 is not included.

Erase

Uses iterator.

erase(q)

Removes the element pointed to by q, shortening the length of the vector. Returns iterator pointing to the next element, after removal. Example:

    vector <float> vtr = {5.5, 6.6, 7.7, 8.8, 9.9};

    vector<float>::iterator iterB = vtr.begin();

    iterB = iterB + 2;

    vector<float>::iterator itR = vtr.erase(iterB);

    for (int i=0; i<vtr.size(); i++)

        cout << vtr[i] << ", ";

    cout << endl;

    cout << *itR << endl;

Original list is:

    5.56.67.78.89.9

Output is:

    5.56.68.89.9,

    8.8

erase(q1, q2)

Removes a range of elements beginning from that pointed to by the iterator q1, to that pointed to by q2, but excluding the one of q2 – that is, it removes [q1, q2). Returns iterator pointing to the next element, after removal of range. Illustration:

    vector <float> vtr = {5.5, 6.6, 7.7, 8.8, 9.9};

    vector<float>::iterator iterB = vtr.begin();

    iterB = iterB + 2;

    vector<float>::iterator iterE = vtr.end();

    iterE = iterE - 1;

    vector<float>::iterator itR = vtr.erase(iterB, iterE);

    for (int i=0; i<vtr.size(); i++)

        cout << vtr[i] << ", ";

    cout << endl;

    cout << *itR << endl;

Original list is:

    5.56.67.78.89.9

Output is:

    5.56.69.9,

    9.9

Clear

clear()

Returns void. All elements in a vector can be removed, reducing the length of the vector to zero. This is how:

    vector <float> vtr = {5.5, 6.6, 7.7, 8.8, 9.9};

    vtr.clear();

    cout << vtr.size() << endl;

Original list is:

    5.56.67.78.89.9

Output is:

    0

Swapping

x.swap(y)

The elements of two different vectors can be swapped, independent of their different lengths. Illustration:

    vector <float> vtr1 = {1.1, 2.2, 3.3, 4.4};

    vector <float> vtr2 = {5.5, 6.6, 7.7, 8.8, 9.9};

    vtr2.swap(vtr1);

    cout << "New vtr1: ";

    for (int i=0; i<vtr1.size(); i++)

        cout << vtr1[i] << ", ";

    cout << endl;

    cout << "New vtr2: ";

    for (int i=0; i<vtr2.size(); i++)

        cout << vtr2[i] << ", ";

    cout << endl;

Output is:

    New vtr1: 5.5, 6.6, 7.7, 8.8, 9.9,

    New vtr2: 1.1, 2.2, 3.3, 4.4,

Conclusion

C++ vector has many member functions. These functions can be categorized in the following subcategories: Construction/Destruction, Capacity, Iterator and Vector Access, Element Access, Push Back and Pop Back, Inserting, Assigning Elements (Replacements), Erase, Clear, and Swapping. The most useful functions in each of these subcategories have been explained above.

About the author

Chrysanthus Forcha

Discoverer of mathematics Integration from First Principles and related series. Master’s Degree in Technical Education, specializing in Electronics and Computer Software. BSc Electronics. I also have knowledge and experience at the Master’s level in Computing and Telecommunications. Out of 20,000 writers, I was the 37th best writer at devarticles.com. I have been working in these fields for more than 10 years.