C++transfromの使用
1定義:
もしあなたが知っているなら、自分で補う.
2使い方
次のコードでは、2つの使用方法を簡単に説明します.
a.容器の元素を入参とする
b.コンテナの要素でメンバー関数を呼び出す
3コード実装
4運転結果
もしあなたが知っているなら、自分で補う.
2使い方
次のコードでは、2つの使用方法を簡単に説明します.
a.容器の元素を入参とする
b.コンテナの要素でメンバー関数を呼び出す
3コード実装
#include
#include
#include
#include //!< for transfrom function
#include //!< for mem_fun function
using namespace std;
//! ,
string* Add1(string *str)
{
*str += '1';
return str;
}
int main()
{
vector vctStr;
vctStr.push_back(new string("hello"));
vctStr.push_back(new string("boy"));
vctStr.push_back(new string("girl"));
for (auto i : vctStr)
{
cout << *i << " ";
}
cout << endl;
//! 1:
vector vctStrDst(vctStr.size());
transform(vctStr.begin(), vctStr.end(), vctStrDst.begin(), Add1);
for (auto i : vctStrDst)
{
cout << *i << " ";
}
cout << endl;
//! 2:
vector vctIntDst(vctStr.size());
transform(vctStr.begin(), vctStr.end(),
vctIntDst.begin(), mem_fun(&string::size));
for (auto i : vctIntDst)
{
cout << i << " ";
}
cout << endl;
//!
for (auto i : vctStr)
{
delete i;
}
vctStr.clear();
cin.get();
return 0;
}
4運転結果
hello boy girl
hello1 boy1 girl1
6 4 5