📄 ite_4254.htm
字号:
<HTML><TITLE>iter_swap</TITLE><BODY>
<A HREF="ref.htm"><IMG SRC="images/banner.gif"></A>
<P><STRONG>Click on the banner to return to the Class Reference home page.</STRONG></P>
<P>©Copyright 1996 Rogue Wave Software</P>
<H2>iter_swap</H2>
<HR><PRE> Algorithm</PRE><HR>
<A NAME="Summary"><H3>Summary</H3></A>
<P>Exchange values pointed at in two locations </P>
<H3>Contents</H3>
<UL>
<A HREF="#Synopsis"><LI>Synopsis</LI></A>
<A HREF="#Description"><LI>Description</LI></A>
<A HREF="#Example"><LI>Example</LI></A>
<A HREF="#Warning"><LI>Warning</LI></A>
<A HREF="#See Also"><LI>See Also</LI></A>
</UL>
<A NAME="Synopsis"><H3>Synopsis</H3></A>
<PRE>#include <algorithm></PRE>
<PRE>
template <class ForwardIterator1, class ForwardIterator2>
void <B>iter_swap</B> (ForwardIterator1, ForwardIterator2);
</PRE>
<A NAME="Description"><H3>Description</H3></A>
<P>The <B><I>iter_swap</B></I> algorithm exchanges the values pointed at by the two iterators <SAMP>a </SAMP>and <SAMP>b</SAMP>.</P>
<A NAME="Example"><H3>Example</H3></A>
<PRE>#include <vector>
#include <algorithm>
#include <iostream.h>
int main ()
{
int d1[] = {6, 7, 8, 9, 10, 1, 2, 3, 4, 5};
//
// Set up a vector.
//
vector<int> v(d1+0, d1+10);
//
// Output original vector.
//
cout << "For the vector: ";
copy(v.begin(), v.end(), ostream_iterator<int>(cout," "));
//
// Swap the first five elements with the last five elements.
//
swap_ranges(v.begin(), v.begin()+5, v.begin()+5);
//
// Output result.
//
cout << endl << endl
<< "Swaping the first 5 elements with the last 5 gives: "
<< endl << " ";
copy(v.begin(), v.end(), ostream_iterator<int>(cout," "));
//
<B> </B>// Now an example of iter_swap -- swap first and last elements.
<B> </B>//
<B>iter_swap</B>(v.begin(), v.end()-1);
//
// Output result.
//
cout << endl << endl
<< "Swaping the first and last elements gives: "
<< endl << " ";
copy(v.begin(), v.end(), ostream_iterator<int>(cout," "));
cout << endl;
return 0;
}
Output :
For the vector: 6 7 8 9 10 1 2 3 4 5
Swaping the first five elements with the last five gives:
1 2 3 4 5 6 7 8 9 10
Swaping the first and last elements gives:
10 2 3 4 5 6 7 8 9 1
</PRE>
<A NAME="Warning"><H3>Warning</H3></A>
<P>If your compiler does not support default template parameters, then you will need to always supply the <SAMP>Allocator</SAMP> template argument. For instance, you'll have to write :</P>
<P><SAMP>vector<int, allocator></SAMP></P>
<P>instead of : </P>
<P><SAMP>vector<int></SAMP></P>
<A NAME="See Also"><H3>See Also</H3></A>
<P><A HREF="Ite_5295.htm"><B><I>Iterators</B></I></A>, <A HREF="swa_4514.htm"><B><I>swap</B></I></A>, <A HREF="swa_1153.htm"><B><I>swap_ranges</B></I></A></P>
<HR>
<A HREF="Ite_5295.htm"><IMG SRC="images/prev.gif"></A> <A HREF="ref.htm#contents"><IMG SRC="images/toc.gif"></A> <A HREF="les_7517.htm"><IMG SRC="images/next.gif"></A></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -