变动性算法

1.复制函数

  • OutputIterator copy( InputIterator sourceBeg, OutputIterator sourceEnd, OutputIterator destBeg)//将源区间[sourceBeg, sourceEnd)中的所有元素复制到以destBeg为起点的目标区间去。
  • BidirectionalIterator1 copy_backward( BidirectionalIterator1 sourceBeg, BidirectionalIterator1 sourceEnd, BidirectionalIterator2 destEnd)////将源区间[sourceBeg, sourceEnd)中的所有元素复制到以destEnd为终点的目标区间去。
  • 注:第3个参数不在源区间内(即:destBeg或destEnd不在[sourceBeg, sourceEnd)内)
// STL.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <print.hpp>
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	vector<char> source(10,'.');
	for(int c='a';c<='f';++c)
	{
		source.push_back(c);
	}
	source.insert(source.end(),10,'.');
	PRINT_ELEMENTS(source,"source:");
	vector<char> c1(source.begin(),source.end());
	copy(c1.begin()+10,c1.begin()+16,c1.begin()+7);
	PRINT_ELEMENTS(c1,"c1: ");
	vector<char> c2(source.begin(),source.end());
	copy_backward(c2.begin()+10,c2.begin()+16,c2.begin()+19);
	PRINT_ELEMENTS(c2,"c2: ");
	return 0;
}

2.转换和结合函数

转换元素
OutputIterator transform( InputIterator sourceBeg, InputIterator sourceEnd, OutputIterator destBeg, UnaryFunc op)
  • 针对源区间[sourceBeg, sourceEnd)中的每一个元素调用:op(elem),并将结果写到destBeg起始的目标区间去。
  • 返回目标区间内“最后一个被转换的元素"的下一个位置,也就是第一个未被覆盖的元素的位置。
将两序列的元素加以结合
OutputIterator transform( InputIterator1 source1Beg, InputIterator1 source1End, InputIterator2 sourceBeg, OutputIterator destBeg, BinaryFunc op)
//针对第一个源区间[source1Beg, source1End)以及“从source2Beg开始的第二个源区间"的对于元素,调用:
// op(source1Elem, source2Elem)
//并将结果写入以destBeg起始的目标区间内。
// STL.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <print.hpp>
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	vector<int> coll1;
	list<int>	coll2;
	INSERT_ELEMENTS(coll1,1,9);
	PRINT_ELEMENTS(coll1);
	transform(coll1.begin(),coll1.end(),coll1.begin(),coll1.begin(),
		multiplies<int>());
	PRINT_ELEMENTS(coll1,"squared:");
	transform(coll1.begin(),coll1.end(),coll1.rbegin(),back_inserter(coll2),
		plus<int>());
	PRINT_ELEMENTS(coll2,"coll2:");
	cout<<"diff:";
	transform(coll1.begin(),coll1.end(),coll2.begin(),ostream_iterator<int>(cout," "),
		minus<int>());
	cout<<endl;
	return 0;
}


3.互换元素内容

ForwardIterator2 swap_ranges( ForwardIterator1 beg1, ForwardIterator1 end1, ForwardIterator2 beg2)
//将区间[beg1, end1)内的元素和"从beg2开始的区间"内的对于元素互换。
//返回第二个区间中"最后一个被交换元素"的下一个位置。

4.赋予新值

赋予完全相同的数值
  • void fill( ForwardIterator beg, ForwardIterator end, const T& newValue) //将区间[beg,end)内的每一个元素都赋予新值newValue。
  • void fill_n( ForwardIterator beg, Size num, const T& newValue) //将"从beg开始的前num个元素"赋予新值newValue。
赋予新产生的数值
  • void generate( Forwarditerator beg, ForwardIterator end, Func op) //调用op()产生新值,并赋值给区间[beg, end)内的每个元素。
  • void generate_n( OutputIterator beg, Size num, Func op) //调用op(),赋值给"以beg开始的区间"内的前num个元素。
// STL.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <print.hpp>
#include <iostream>
#include <cstdlib>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	list<int> coll;
	generate_n(back_inserter(coll),5,rand);
	PRINT_ELEMENTS(coll);
	generate(coll.begin(),coll.end(),rand);
	PRINT_ELEMENTS(coll);
	return 0;
}


5.替换元素

替换序列内的元素
  • void replace( ForwardIterator beg, ForwardIterator end, const T& oldValue, const T& newValue) //将[beg,end)之内每一个"与oldValue相等"的元素替换为newValue
  • void replace_if( ForwardIterator beg, ForwardIterator end, UnaryPredicate op, const T& newValue) //将区间[beg, end)之内的每一个令一元判断式:op(elem)获得true的元素替换为newValue。
复制并替换元素
  • OutputIterator replace_copy( InputIterator sourceBeg, InputIterator sourceEnd, OutputInterator destBeg, const T& oldValue, const T& newValue) //将源区间[beg, end)中的元素复制到"以destbeg为起点"的目标区间去,同时将其中"与oldValue相等"的所有元素替换为newValue。
  • OutputIterator replace_copy_if( InputIterator sourceBeg, InputIterator sourceEnd, OutputInterator destBeg, UnaryPredicate op, const T& newValue) //令一元判断式op(elem)结果为true的所有元素替换为newValue。
// STL.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <print.hpp>
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	list<int> coll;
	INSERT_ELEMENTS(coll,2,6);
	INSERT_ELEMENTS(coll,4,9);
	PRINT_ELEMENTS(coll);
	replace_copy(coll.begin(),coll.end(),ostream_iterator<int>(cout," "),
		5,55);
	cout<<endl;
	replace_copy_if(coll.begin(),coll.end(),ostream_iterator<int>(cout," "),
		bind2nd(less<int>(),5),42);
	cout<<endl;
	replace_copy_if(coll.begin(),coll.end(),ostream_iterator<int>(cout," "),
		bind2nd(modulus<int>(),2),0);
	cout<<endl;
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值