swap_ranges

来源:互联网 发布:dw软件免费下载 编辑:程序博客网 时间:2024/04/29 15:36
 
// swap_ranges.cpp -- 2011-10-03-18.25#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[]){vector<int> vec1(10, 1) ;vector<int> vec2(12, 2) ;for_each(vec1.begin(), vec1.end(), Print<int> ()) ;std ::cout << std ::endl ;for_each(vec2.begin(), vec2.end(), Print<int> ()) ;std ::cout << "\n---------------------------------" << std ::endl ;//swap_ranges (beg1, end1, beg2) ;//操作前:[beg1,end1)标示第一个输入序列.[beg2,...)标示第二个输入序列.//操作后:两个输入序列的元素整体被交换.//返回值:返回指向第二个输入序列中被交换的最后一个元素之后的元素的迭代器.//备注:必须保证第二个输入序列中的元素至少与第一个输入序列中的元素一样多.//两个输入序列的不能有重叠.否则将抛出异常.vector<int> ::iterator iter = swap_ranges(vec1.begin(), vec1.end(), vec2.begin()) ;if (iter != vec2.end())std ::cout << *iter << std ::endl ;for_each(vec1.begin(), vec1.end(), Print<int> ()) ;std ::cout << std ::endl ;for_each(vec2.begin(), vec2.end(), Print<int> ()) ;std ::cout << std ::endl ;std ::cin.get() ;return 0 ;}
原创粉丝点击