cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : STL Containers : map : 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
map
comparison operators
map::map
map::~map
member functions:
· map::begin
· map::clear
· map::count
· map::empty
· map::end
· map::equal_range
· map::erase
· map::find
· map::get_allocator
· map::insert
· map::key_comp
· map::lower_bound
· map::max_size
· map::operator=
· map::operator[]
· map::rbegin
· map::rend
· map::size
· map::swap
· map::upper_bound
· map::value_comp

-

map::operator= public member function
map<Key,T,Compare,Allocator>&
     operator= ( const map<Key,T,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 map x, if any.

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

Parameters

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

Return value

*this

Example

// assignment operator with maps
#include <iostream>
#include <map>
using namespace std;

int main ()
{
  map<char,int> first;
  map<char,int> second;

  first['x']=8;
  first['y']=16;
  first['z']=32;

  second=first;           // second now contains 3 ints
  first=map<char,int>();  // and first is now 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: 3

Complexity

Linear on sizes (destruction, copy construction).

See also

map::insert Insert element (public member function)
map::operator[] Access element (public member function)
map::map Construct map (public member function)

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