Skip to content

vector_pro::swap

C++
1
2
3
void swap(const size_type idx1, const size_type idx2);
void swap(const_iterator_pro<value_type> idx1, const_iterator_pro<value_type> idx2)
void swap(vector_pro<value_type> &another);

Swap content

  1. Swap contents between two index ( 1. 2. ):
    After calling this function, the contents at the two indexes will be moved to another index.
  2. Swap containers ( 3. ):
    After the call to this member function, the elements in this container are those which were in another before the call, and the elements of another are those which were in this. All iterators, references and pointers remain valid for the swapped objects.

Different from std::vector , the vector_pro::swap has two using ways.

Please note that calling the function by passing illegal idx1 or idx2 will get an exception.

Parameters

  1. idx1 , idx2
    Target index. (can be in different container but should have the same type)
  2. another
    Another vector container of the same type

Return value

none

Example

C++
// vector_pro::swap
#include <iostream>
#include "vector_pro.h"

/**
 * Output:
 * foo contains: 200 100 200 200 200
 * bar contains: 100 100 200
 */

int main ()
{
  vector_pro<int> foo (3,100);   // three ints with a value of 100
  vector_pro<int> bar (5,200);   // five ints with a value of 200

  foo.swap(bar);
  foo.swap(bar.begin() + 2, foo.begin() + 1);

  std::cout << "foo contains:";
  for (unsigned i=0; i<foo.size(); i++)
    std::cout << ' ' << foo[i];
  std::cout << '\n';

  std::cout << "bar contains:";
  for (unsigned i=0; i<bar.size(); i++)
    std::cout << ' ' << bar[i];
  std::cout << '\n';

  return 0;
}

Complexity

Constant.