用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - c++代码库

std::transform 一元、二元函数

2012-11-27 作者: 程序猿style举报

[c++]代码库

//  std::transform  一元、二元函数
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <iostream>
#include <functional>

int main() {
	using namespace std;

	string strSample("THIS is a TEst string!");
	cout << "The sample string is: " << strSample << endl;

	string strLowerCaseCopy;
	strLowerCaseCopy.resize(strSample.size());

	transform(strSample.begin() // start of source range
			, strSample.end() // end of source range
			, strLowerCaseCopy.begin() // start of destination range
			, tolower); // unary function

	cout << "Result of 'transform' on the string with 'tolower':" << endl;
	cout << "\"" << strLowerCaseCopy << "\"" << endl << endl;

	// Two sample vectors of integers...
	vector<int> vecIntegers1, vecIntegers2;
	for (int nNum = 0; nNum < 10; ++nNum) {
		vecIntegers1.push_back(nNum);
		vecIntegers2.push_back(10 - nNum);
	}

	// A destination range for holding the result of addition
	deque<int> dqResultAddition(vecIntegers1.size());

	transform(vecIntegers1.begin() // start of source range 1
			, vecIntegers1.end() // end of source range 1
			, vecIntegers2.begin() // start of source range 2
			, dqResultAddition.begin() // start of destination range
			, plus<int>()); // binary function

	cout << "Result of 'transform' using binary function 'plus': " << endl;
	cout << endl << "Index Vector1 + Vector2 = Result (in Deque)" << endl;
	for (size_t nIndex = 0; nIndex < vecIntegers1.size(); ++nIndex) {
		cout << nIndex << " \t " << vecIntegers1[nIndex] << "\t+ ";
		cout << vecIntegers2[nIndex] << " \t = ";

		cout << dqResultAddition[nIndex] << endl;
	}

	return 0;
}


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...