How to change a particular element of a C++ STL Vector?

This is a tutorial that focuses on ways using which the programmers can change a particular element of a C++ STL vector. Often we need to alter or update the existing information as per our latest requirements. This post will definitely help you to carry out the task of changing a particular value in a C++ STL vector.

STL stands for Standard Template Library. It is a set of C++ template classes to provide the common programming data structures and functions like arrays, lists, vectors, stacks to name a few. It consists of components which are –

  1. Containers
  2. Functions
  3. Iterators
  4. Algorithms

Vectors are a type of sequential containers. This refers to the fact that the data in such containers (vectors) can be accessed in a sequential manner. However, we need to use specific operators to access particular locations of the vector.

 

Libraries

Code :

#include<iostream>
#include<vector>

iostream : This library provides all the standard input and output functions

vector : This library provides the vector operation functions to manipulate vectors

 

Declaration and Initialization of the vector

Code :

int i;

vector<int> vec;

for(i=1;i<=10;i++)
{
    vec.push_back(i);

}

The vectors store elements that have the same data-type. The syntax for vector declaration requires the keyword vector followed by the data-type of the data to be stored enclosed in ‘<‘ and ‘>’ and then the name of the vector. Users can also specify the initial size of the vector. However, that part is optional.

The for loop ranges from 1 to 10 where in each iteration we are pushing one value (value of i) in the vector. This can be done using the function push_back():

Syntax : vector_name.push_back(pushvalue)

this pushes the value passed as the parameter to the calling vector. In our case,

vec.push_back(i);

this function call pushes the current value of i to the vector. Therefore, the vector will contain the numbers from 1 to 10 at the end of the loop.

 

Display the vector

Code :

cout<<"\nInitial data in vector  :     ";
for (int x : vec){

     cout << x << "      ";

}

We display the current vector using a for loop for reference.

Output :

Initial data in vector : 1 2 3 4 5 6 7 8 9 10

 

Changing a particular element

There are two ways which can be used for this step.  We will see both of them one by one.

 

1. Using ‘at’ operator

The at operator is very simple to use and requires one parameter which is the index location of the element which we want to change/modify.

Syntax : vector_name.at(index)

Code :

vec.at(4) = -1;

The at operator returns the pointer to the particular element present at the specified index. We can directly enter the new or updated value using the ‘=’ operator to assign the value to that location. For instance, the vector element at index 4 is modified as -1 in this case.

 

2. Using ‘[ ]’ operator

Similarly, the ‘[ ]’ operator requires one parameter which is the index location of the element which we want to change/modify.

Syntax : vector_name[index]

Code :

vec[8] = -1;

This operator returns the pointer to the particular element present at the specified index. We can directly enter the updated value using the ‘=’ operator. For instance, in this case the element at index location 8 is modified as -1.

 

Display the result  vector

Code :

cout<< "\n\nFinal data in vector    :     ";
for (int x : vec){

     cout << x << "      ";

}

The modified data at vector locations index 4 and index 8 are reflected when we display the final elements of the vector.

Output :

Final data in vector : 1 2 3 4 -1 6 7 8 -1 10

 

In conclusion, we can use either of the two methods (operators) mentioned to change a particular element of a C++ STL vector.

Complete Code

// C++ code to change a particular element of a STL Vector

#include<iostream>
#include<vector>

using namespace std;

int main(){

    int i;

    vector<int> vec;

    for(i=1;i<=10;i++)
    {
        vec.push_back(i);

    }

    cout<<"\nInitial data in vector  :     ";
     for (int x : vec){

        cout << x << "      ";

     }

    vec.at(4) = -1;    //at operator


    vec[8] = -1;      //[] operator


    cout<< "\n\nFinal data in vector    :     ";
     for (int x : vec){

        cout << x << "      ";

     }


    cout<<"\n\n";
    return 0;

}

Output :

Initial data in vector : 1 2 3 4 5 6 7 8 9 10

Final data in vector : 1 2 3 4 -1 6 7 8 -1 10

 

You may also learn,

C++ Program to Access Elements from a Vector in

Inserting an element into a specific position of a vector using C++

Leave a Reply

Your email address will not be published. Required fields are marked *