Skip to content
Related Articles

Related Articles

vector::operator= and vector::operator[ ] in C++ STL

Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 30 Jun, 2022
Improve Article
Save Article

Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container.
 

vector::operator=

This operator is used to assign new contents to the container by replacing the existing contents. 
It also modifies the size according to the new contents.

Syntax : 

vectorname1 = (vectorname2)
Parameters :
Another container of the same type.
Result :
Assign the contents of the container passed as 
parameter to the container written on left side of the operator.

Examples:  

Input  :  myvector1 = 1, 2, 3
          myvector2 = 3, 2, 1, 4
          myvector1 = myvector2;
Output :  myvector1 = 3, 2, 1, 4

Input  :  myvector1 = 2, 6, 1, 5
          myvector2 = 3, 2
          myvectoe1 = myvector2;
Output :  myvector1 = 3, 2

Errors and Exceptions
1. If the containers are of different types, an error is thrown. 
2. It has a basic no exception throw guarantee otherwise.

Time Complexity – Linear O(N)

C++




// CPP program to illustrate
// Implementation of = operator
#include <iostream>
#include <vector>
using namespace std;
 
int main()
{
    vector<int> myvector1{ 1, 2, 3 };
    vector<int> myvector2{ 3, 2, 1, 4 };
    myvector1 = myvector2;
    cout << "myvector1 = ";
    for (auto it = myvector1.begin(); it != myvector1.end(); ++it)
        cout << ' ' << *it;
    return 0;
}

Output: 

myvector1= 3 2 1 4
vector::operator[]

This operator is used to reference the element present at position given inside the operator. It is similar to the at() function, the only difference is that the at() function throws an out-of-range exception when the position is not in the bounds of the size of vector, while this operator causes undefined behavior.

Syntax :  

vectorname[position]
Parameters :
Position of the element to be fetched.
Returns :
Direct reference to the element at the given position.

Examples: 

Input  :  myvector = 1, 2, 3
          myvector[2];
Output :  3

Input  :  myvector = 3, 4, 1, 7, 3
          myvector[3];
Output :  7

Errors and Exceptions
1. If the position is not present in the vector, it shows undefined behavior. 
2. It has a no exception throw guarantee otherwise. 

Time Complexity – Constant O(1)

C++




// CPP program to illustrate
// Implementation of [] operator
#include <iostream>
#include <vector>
using namespace std;
 
int main()
{
    vector<int> myvector;
    myvector.push_back(3);
    myvector.push_back(4);
    myvector.push_back(1);
    myvector.push_back(7);
    myvector.push_back(3);
    cout << myvector[3];
    return 0;
}

Output: 

7

Application 
Given a vector of integers, print all the integers present at odd positions. 

Input  :1, 2, 3, 4, 5, 6, 7, 8, 9
Output :2 4 6 8
Explanation - 2, 4, 6, and 8 are at position 1, 3, 5 and 7 which are odd

Algorithm 
1. Run a loop till the size of the vector. 
2. Check if the position is not divisible by 2, then print the element at that position.

C++




// CPP program to illustrate
// Application of [] operator
#include <iostream>
#include <vector>
using namespace std;
 
int main()
{
    vector<int> myvector;
    myvector.push_back(1);
    myvector.push_back(2);
    myvector.push_back(3);
    myvector.push_back(4);
    myvector.push_back(5);
    myvector.push_back(6);
    myvector.push_back(7);
    myvector.push_back(8);
    myvector.push_back(9);
    // Vector becomes 1, 2, 3, 4, 5, 6, 7, 8, 9
 
    for (int i = 0; i < myvector.size(); ++i) {
        if (i % 2 != 0) {
            cout << myvector[i];
            cout << " ";
        }
    }
    return 0;
}

Output: 

2 4 6 8

 Let us see the differences in a tabular form -:

 vector::operator=vector::operator[ ] 
1.It is used to assign new contents to the container and replace its current contentsIt is used to assign new contents to the container by replacing its current contents.
2.

Its syntax is -:

vector& operator= (const vector& x);

Its syntax is -:

vector& operator= (const vector& x);

3.

It takes two parameters -:

1. A vector object of the same type

2. An initializer_list object.

It takes two parameters that are -:

1. A vector object of the same type

2. An initializer_list object.

4.Its complexity is linear.Its complexity is linear.
5.It is defined in <vector> header file.It is defined in <vector> header file.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!