cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : STL Containers : list : 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
list
comparison operators
list::list
list::~list
member functions:
· list::assign
· list::back
· list::begin
· list::clear
· list::empty
· list::end
· list::erase
· list::front
· list::get_allocator
· list::insert
· list::max_size
· list::merge
· list::operator=
· list::pop_back
· list::pop_front
· list::push_back
· list::push_front
· list::rbegin
· list::remove
· list::remove_if
· list::rend
· list::resize
· list::reverse
· list::size
· list::sort
· list::splice
· list::swap
· list::unique

-

list::operator= public member function
list<T,Allocator>& operator= ( const list<T,Allocator>& x );

Copy container content

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

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

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

Parameters

x
A list object containing elements of the same type.

Return value

*this

Example

// assignment operator with lists
#include <iostream>
#include <list>
using namespace std;

int main ()
{
  list<int> first (3);      // list of 3 zero-initialized ints
  list<int> second (5);     // list of 5 zero-initialized ints

  second=first;
  first=list<int>();

  cout << "Size of first: " << int (first.size()) << endl;
  cout << "Size of second: " << int (second.size()) << endl;
  return 0;
}
Both list containers of int elements are initialized to sequences with different sizes. Then, second is assigned to first, so both are now equal and with a size of 3. And then, first is assigned to a newly constructed empty container object, so its size is finally 0. Output:
Size of first: 0
Size of second: 3

Complexity

Linear on sizes (destruction, copy construction).

See also

list::assign Assign new content to container (public member function)

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