swap_ranges

 
// 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 ;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值