vector_pro::swap¶
| C++ | |
|---|---|
Swap content¶
- Swap contents between two index ( 1. 2. ):
After calling this function, the contents at the two indexes will be moved to another index. - Swap containers ( 3. ):
After the call to this member function, the elements in this container are those which were inanotherbefore the call, and the elements ofanotherare those which were in this. All iterators, references and pointers remain valid for the swapped objects.
Different from
std::vector, thevector_pro::swaphas two using ways.Please note that calling the function by passing illegal
idx1oridx2will get an exception.
Parameters¶
idx1,idx2
Target index. (can be in different container but should have the same type)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.