C++ Set Library - operator= Function



Description

It assigns new contents to the container, replacing its current content.

Declaration

Following are the ways in which std::set::operator= works in various C++ versions.

C++98

set& operator= (const set& x);

C++11

set& operator= (const set& x);
set& operator= (set&& x);	
set& operator= (initializer_list<value_type> il)

Return value

It returns *this.

Exceptions

If an exception is thrown, the container is in a valid state.

Time complexity

Linear in size of the container.

Example

The following example shows the usage of std::set::operator=.

#include <iostream>
#include <set>

int main () {
   int myints[] = { 10,20,30,40,50 };
   std::set<int> first (myints,myints+10);   
   std::set<int> second;                   

   second = first;                         
   first = std::set<int>();                

   std::cout << "Size of first: " << int (first.size()) << '\n';
   std::cout << "Size of second: " << int (second.size()) << '\n';
   return 0;
}

The above program will compile and execute properly.

Size of first: 0
Size of second: 8
set.htm
Advertisements