// iter_swap.cpp -- 2011-10-03-18.19
#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <vector>
using std ::vector ;
template<class T>
class Print
{
public:
void operator () (const T & t) const
{
std ::cout << t << " " ;
}
} ;
int _tmain(int argc, _TCHAR* argv[])
{
int arr1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9} ;
vector<int> vec1(arr1, arr1 + sizeof arr1 / sizeof (int)) ;
for_each(vec1.begin(), vec1.end(), Print<int> ()) ;
std ::cout << std ::endl ;
// iter_swap (iter1, iter2) ;
// 操作前:iter1和iter2是两个前向迭代器.
// 操作后:iter1和iter2所指向的值被交换.
// 返回值:无.
// 备注: 无.
iter_swap(vec1.begin(), vec1.end() - 1) ;
for_each(vec1.begin(), vec1.end(), Print<int> ()) ;
std ::cin.get() ;
return 0 ;
}
iter_swap
最新推荐文章于 2025-04-19 07:00:00 发布
本文探讨了C++中迭代器交换的概念及其在模板函数中的应用,通过实例展示了如何在容器元素间进行值交换。
1447

被折叠的 条评论
为什么被折叠?



