iter_swap
function template
<algorithm>
template <class ForwardIterator1, class ForwardIterator2>
void iter_swap ( ForwardIterator1 a, ForwardIterator2 b );
Exchange values of objects pointed by two iterators
Assigns the content of *a to *b and the content of *b to *a.
The behavior of this function template is equivalent to:
1 2 3 4 5
|
template <class ForwardIterator1, class ForwardIterator2>
void iter_swap ( ForwardIterator1 a, ForwardIterator2 b )
{
swap (*a, *b);
}
|
Parameters
- a, b
- Forward iterators to the objects whose contents are swapped.
Return value
none
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
// iter_swap example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main () {
int myints[]={10,20,30,40,50 }; // myints: 10 20 30 40 50
vector<int> myvector (4,99); // myvector: 99 99 99 99
iter_swap(myints,myvector.begin()); // myints: [99] 20 30 40 50
// myvector: [10] 99 99 99
iter_swap(myints+3,myvector.begin()+2); // myints: 99 20 30 [99]
// myvector: 10 99 [40] 99
cout << "myvector contains:";
for (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
cout << " " << *it;
cout << endl;
return 0;
}
|
Output:
myvector contains: 10 99 40 99
|
Complexity
Constant: Performs exactly one copy construction and two assignments (although notice that each of these operations works on its own complexity).
See also
swap | Exchange values of two objects (function template) |
copy | Copy range of elements (function template) |
replace | Replace value in range (function template) |
|