cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : STL Containers : vector : 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
vector
comparison operators
vector::vector
vector::~vector
member functions:
· vector::assign
· vector::at
· vector::back
· vector::begin
· vector::capacity
· vector::clear
· vector::empty
· vector::end
· vector::erase
· vector::front
· vector::get_allocator
· vector::insert
· vector::max_size
· vector::operator=
· vector::operator[]
· vector::pop_back
· vector::push_back
· vector::rbegin
· vector::rend
· vector::reserve
· vector::resize
· vector::size
· vector::swap

-

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

Copy vector content

Assigns a copy of vector x as the new content for the vector object.

The elements contained in the vector 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 vector object and vector x will have the same size and compare equal to each other.

Parameters

x
A vector object containing elements of the same type.

Return value

*this

Example

// vector assignment
#include <iostream>
#include <vector>
using namespace std;

int main ()
{
  vector<int> first (3,0);
  vector<int> second (5,0);

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

  cout << "Size of first: " << int (first.size()) << endl;
  cout << "Size of second: " << int (second.size()) << endl;
  return 0;
}
Both vectors of int elements are initialized to sequences of zeros of 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 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

vector::assign Assign vector content (public member function)

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