iter_swap
Gli scambi due valori sono segnalati da una coppia degli iteratori specificati.
template<class ForwardIterator1, class ForwardIterator2>
void iter_swap(
ForwardIterator1 _Left,
ForwardIterator2 _Right
);
Parametri
_Left
Uno degli iteratori avanti del cui valore è possibile scambiare._Right
Il secondo degli iteratori avanti del cui valore è possibile scambiare.
Note
swap deve essere utilizzato relative aiter_swap, che è stata inclusa nello standard C++ per compatibilità con le versioni precedenti.Se Fit1 e Fit2 è iteratori avanti, quindi iter_swap ( Fit1, Fit2 ), è equivalente a swap (*Fit1, *Fit2 ).
I tipi di valore degli iteratori avanti di input devono avere lo stesso valore.
Esempio
// alg_iter_swap.cpp
// compile with: /EHsc
#include <vector>
#include <deque>
#include <algorithm>
#include <iostream>
#include <ostream>
using namespace std;
class CInt;
ostream& operator<<( ostream& osIn, const CInt& rhs );
class CInt
{
public:
CInt( int n = 0 ) : m_nVal( n ){}
CInt( const CInt& rhs ) : m_nVal( rhs.m_nVal ){}
CInt& operator=( const CInt& rhs ) { m_nVal =
rhs.m_nVal; return *this; }
bool operator<( const CInt& rhs ) const
{ return ( m_nVal < rhs.m_nVal );}
friend ostream& operator<<( ostream& osIn, const CInt& rhs );
private:
int m_nVal;
};
inline ostream& operator<<( ostream& osIn, const CInt& rhs )
{
osIn << "CInt(" << rhs.m_nVal << ")";
return osIn;
}
// Return whether modulus of elem1 is less than modulus of elem2
bool mod_lesser ( int elem1, int elem2 )
{
if ( elem1 < 0 )
elem1 = - elem1;
if ( elem2 < 0 )
elem2 = - elem2;
return elem1 < elem2;
};
int main( )
{
CInt c1 = 5, c2 = 1, c3 = 10;
deque<CInt> deq1;
deque<CInt>::iterator d1_Iter;
deq1.push_back ( c1 );
deq1.push_back ( c2 );
deq1.push_back ( c3 );
cout << "The original deque of CInts is deq1 = (";
for ( d1_Iter = deq1.begin( ); d1_Iter != --deq1.end( ); d1_Iter++ )
cout << " " << *d1_Iter << ",";
d1_Iter = --deq1.end( );
cout << " " << *d1_Iter << " )." << endl;
// Exchanging first and last elements with iter_swap
iter_swap ( deq1.begin ( ) , --deq1.end ( ) );
cout << "The deque of CInts with first & last elements swapped is:\n deq1 = (";
for ( d1_Iter = deq1.begin( ); d1_Iter != --deq1.end( ); d1_Iter++ )
cout << " " << *d1_Iter << ",";
d1_Iter = --deq1.end( );
cout << " " << *d1_Iter << " )." << endl;
// Swapping back first and last elements with swap
swap ( *deq1.begin ( ) , *(deq1.end ( ) -1 ) );
cout << "The deque of CInts with first & last elements swapped back is:\n deq1 = (";
for ( d1_Iter = deq1.begin( ); d1_Iter != --deq1.end( ); d1_Iter++ )
cout << " " << *d1_Iter << ",";
d1_Iter = --deq1.end( );
cout << " " << *d1_Iter << " )." << endl;
// Swapping a vector element with a deque element
vector <int> v1;
vector <int>::iterator Iter1;
deque <int> deq2;
deque <int>::iterator d2_Iter;
int i;
for ( i = 0 ; i <= 3 ; i++ )
{
v1.push_back( i );
}
int ii;
for ( ii = 4 ; ii <= 5 ; ii++ )
{
deq2.push_back( ii );
}
cout << "Vector v1 is ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")." << endl;
cout << "Deque deq2 is ( " ;
for ( d2_Iter = deq2.begin( ) ; d2_Iter != deq2.end( ) ; d2_Iter++ )
cout << *d2_Iter << " ";
cout << ")." << endl;
iter_swap ( v1.begin ( ) , deq2.begin ( ) );
cout << "After exchanging first elements,\n vector v1 is: v1 = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")." << endl << " & deque deq2 is: deq2 = ( ";
for ( d2_Iter = deq2.begin( ) ; d2_Iter != deq2.end( ) ; d2_Iter++ )
cout << *d2_Iter << " ";
cout << ")." << endl;
}
Requisiti
intestazione: <algorithm>
Spazio dei nomi: deviazione standard