cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : STL Containers : set : operator=
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
STL Containers
bitset
deque
list
map
multimap
multiset
priority_queue
queue
set
stack
vector
set
comparison operators
set::set
set::~set
member functions:
· set::begin
· set::clear
· set::count
· set::empty
· set::end
· set::equal_range
· set::erase
· set::find
· set::get_allocator
· set::insert
· set::key_comp
· set::lower_bound
· set::max_size
· set::operator=
· set::rbegin
· set::rend
· set::size
· set::swap
· set::upper_bound
· set::value_comp

-

set::operator= public member function
set<Key,Compare,Allocator>&
     operator= ( const set<Key,Compare,Allocator>& x );

Copy container content

Assigns a copy of the elements in x as the new content for the container.

The elements contained in the object before the call are dropped, and replaced by copies of those in set x, if any.

After a call to this member function, both the set object and x will have the same size and compare equal to each other.

Parameters

x
A set object with the same class template parameters (Key, Compare and Allocator).

Return value

*this

Example

// assignment operator with sets
#include <iostream>
#include <set>
using namespace std;

int main ()
{
  int myints[]={ 12,82,37,64,15 };
  set<int> first (myints,myints+5);   // set with 5 ints
  set<int> second;                    // empty set

  second=first;                       // now second contains the 5 ints
  first=set<int>();                   // and first is empty

  cout << "Size of first: " << int (first.size()) << endl;
  cout << "Size of second: " << int (second.size()) << endl;
  return 0;
}
Output:
Size of first: 0
Size of second: 5

Complexity

Linear on sizes (destruction, copy construction).

See also

set::insert Insert element (public member function)
set::set Construct set (public member function)

© The C++ Resources Network, 2000-2007 - All rights reserved
Spotted an error? - contact us