vector::operator= member function


The vector operator assignment functions assign one vector to another.We can perform vector assignment in three ways.

T:Type of the vector.

1 vector& operator=(const vector& x);
2 vector& operator=(initializer_list<T> l);(added in C++11
3 vector& operator=(vector&& x) noexcept; (added in C++11 )

More discussion of these functions is provided below.


vector& operator=(const vector& x);

This operator function simply assigns the right side vector to the left side vector.In assigning the vector all the elements of the right vector is copied.Note only the elements are copied not the extra storage the right vector might hold.

Parameters:
x – A vector to be assigned to another vector.

Return type
vector& – A reference to the vector.

Code example

vector<int> vec={ 2 , 43 , 90 } ,
vec1 ;

vec1=vec ;

/* accessing vec element */
for( auto elem:vec )
{
cout<< elem << ” ” ;
}

/* accessing vec1 element */
for( auto elem:vec1 )
{
cout<< elem << ” ” ;
}

Output

2 43 90
2 43 90

Here is another code example showing that the extra storage is not copied.

Link: vector::capacity function
Code example

vector<int> vec2 ;

vec.reserve( 5 ) ;

vec2=vec ;

cout<< “vec capacity =” << vec.capacity( ) << endl
<< “vec2 capacity =” << vec2.capacity( ) ;

Output

vec capacity=5
vec2 capacity=3

The capacity of vec2 is only 3 not 5 ,although vec whose capacity is 5 is assigned to it.The extra two storage is not copied by vec2.

Link: vector::reserve function






vector& operator=(initializer_list<T> l);(added in C++11 )

This function is added in C++11 and the vector is assigned using an initializer_list object.

Parameters:
l – An initializer_list object assigned to the vector.

Return type
vector& – A reference to the vector.

Assigning an initializer_list object to the vector will completely change the vector resulting in the vector size same as the number of elements assigned.The old data of the vector is lost.

Code example

intializer_list<string> il={ “Napolean” , ” Hitler” };

vector<string> vecSt=il ;

for(auto elem:vecSt)
{
cout<< elem ;
}

vector<string> vecSt1={ “Bonaparte” , ” Adolf” } ;

vecSt1=il ;

cout<< vecSt1[0] << vecSt1[1] ;

Output

Napolean Hitler
Napolean Hitler

In the second ‘cout’ you will notice that the old data of vecSt1 is lost.


 


Stop wasting time,earn money($$$) from your website-Join Now!


vector& operator=(vector&& x) noexcept;(added in C++11 )

This operator function will move the data of the right side vector instead of copying the data.In moving the data all the elements and the extra storage of the right side vector gets stolen,leaving nothing for the right side vector.Hence the size and capacity of the right side vector is reduced to 0 after performing move assignment operation.

Parameters:
x – A vector which is to be moved.

Return type
vector& – A reference to the vector.

To perform move assignment operation you must use the function std::move().If this function is not used the normal assignment operator is called.Note after performing the move assignment the right side vector will not hold any elements so accessing the element will give you an undefined result.

Code example

vector< float> vecF = { 3.4 , .90 } ,
vecF1 ;

vecF1=vecF ; ///calls the normal assignment operator,copies the vector

vecF1=std::move(vecF) ; ///Move vecF vector

cout<< vecF1[0] << ” ” << vecF1[1] ;

cout<< vecF[0] << endl //undefined
<< vecF1[1] ; //undefined

cout<< “vecF cpacity” << vecF.capacity() << endl
<< “vecF size” << vecF.size( ) ;

Output,

3.4 .90
undefined
undefined
vecF capacity=0
vecF size=0

The capacity and size of vecF is reduced to 0.


Conclusion

For all the above assignment operators the type must be the same,difference in type will give you an error.

Code example

vector<int> vecI={ 2 ,3 ,4 } ;

vector<double> vecD=vecI ; ///error type not same

The vecI type is int type but the vecD type is double,so their is a type conflict here.