English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ vector operator=() Usage and Example

C++ Vector (Container)

This function assigns the new value to the vector (vector) container and replaces the old value.

Syntax

Two vectors (vector) 'v' and 'v'1

v.operator=(v1);

Here, the vector v1The value is assigned to the vector v2.

Parameter

v1:v1is a vector (vector) object.

Return value

It returns* this.

Example1

Let's look at a simple example.

#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<char> v{'C','#'};
vector<char> v1;
v1.operator=(v);
for(int i=0; i<v.size(); i++)
std::cout << v[i];
return 0;
}

Output:

C#

In this example, the value of the vector v is assigned to the vector v using the operator =() function1.

Example2

Let's look at another simple example.

#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<string> v{"java"};
vector<string> v1{".NET"};
cout << "Initially, v1value is :";
for(int i = 0; i < v1.size();i++)
std::cout << v1[i];
cout << '\n';
cout << "Now, vector v1value is :";
v1.operator=(v);
for(int i = 0; i < v1.size();i++)
std::cout << v1[i];
return 0;
}

Output:

java

In this example, use the operator=() function to replace the original content and assign the value of vector v to vector v1.

C++ Vector (Container)