一、transform 算法
1、接收一個輸入容器范圍的 transform 算法函數(shù)原型
transform 算法函數(shù)原型 : 下面的函數(shù)原型作用是 將 一個輸入容器 中的元素 變換后 存儲到 輸出容器 中 ;
template <class InputIt, class OutputIt, class UnaryOperation>
OutputIt transform(InputIt first1, InputIt last1, OutputIt d_first, UnaryOperation unary_op);
-
參數(shù)解析 :
- InputIt first1 參數(shù) : 輸入容器 的 起始迭代器 ( 包含 ) ;
- InputIt last1 參數(shù) : 輸入容器 的 終止迭代器 ( 不包含 ) ;
- OutputIt d_first 參數(shù) : 輸出容器 的 開始迭代器 , 輸出元素個數(shù) 根據(jù) 輸入元素 的 范圍確定 , transform 會將 變換結(jié)果存儲到 輸出容器中 ;
- UnaryOperation unary_op 參數(shù) : 一元函數(shù)對象 , 將輸入容器 的 每個元素 輸入到該 一元函數(shù)對象 中 , 將計算結(jié)果 輸出到 輸出容器 中 ;
-
返回值解析 :
- 該 算法函數(shù) 返回 OutputIt 類型的 返回值是一個 迭代器 , 該迭代器指向最后一個被寫入元素之后的位置 ;
2、代碼示例 - 傳入接受一個參數(shù)的普通函數(shù)
在下面的代碼中 ,
首先 , 創(chuàng)建了一個 vector 數(shù)組容器 , 之后該容器 既作為輸入容器 , 又作為輸出容器 , 將元素輸入后 , 計算后 , 在輸出 到原來的容器中 ;
// 創(chuàng)建一個 vector 數(shù)組容器
vector<int> myVector;
然后 , 定義了一個 接受一個參數(shù)的 普通函數(shù) , 使用該 函數(shù) 作為變換規(guī)則 ;
// 普通函數(shù)
int addone(int& n) {
return ++n;
}
最后 , 將 myVector 數(shù)組容器的 元素范圍 ( 起始迭代器 和 末尾迭代器 ) 作為輸入容器 , 將 myVector 數(shù)組容器的 的 起始迭代器 作為輸出容器 起始點(diǎn) , 也就是 將 輸入容器 的元素 進(jìn)行修改 , 再次放回到 該容器中 ;
// 向 transform 變換算法中 傳入 普通函數(shù)
transform(myVector.begin(), myVector.end(), myVector.begin(), addone);
代碼示例 :
#include "iostream"
using namespace std;
#include <vector>
#include <algorithm>
#include "functional"
// 普通函數(shù)
int addone(int& n) {
return ++n;
}
int main() {
// 創(chuàng)建一個 vector 集合容器
vector<int> myVector;
// 向容器中插入元素
myVector.push_back(9);
myVector.push_back(5);
myVector.push_back(2);
myVector.push_back(7);
// 向 transform 變換算法中 傳入 一元函數(shù)對象
transform(myVector.begin(), myVector.end(), myVector.begin(), addone);
// 遍歷容器
for_each(myVector.begin(), myVector.end(), [](int element) {
cout << element << endl;
});
// 控制臺暫停 , 按任意鍵繼續(xù)向后執(zhí)行
system("pause");
return 0;
};
執(zhí)行結(jié)果 :
10
6
3
8
請按任意鍵繼續(xù). . .
3、代碼示例 - 傳入接受一個參數(shù)的 Lambda 表達(dá)式
在下面的代碼中 ,
首先 , 創(chuàng)建了一個 vector 數(shù)組容器 , 之后該容器 既作為輸入容器 , 又作為輸出容器 , 將元素輸入后 , 計算后 , 在輸出 到原來的容器中 ;
// 創(chuàng)建一個 vector 數(shù)組容器
vector<int> myVector;
然后 , 定義了一個 接受一個參數(shù)的 Lambda 表達(dá)式 , 使用該 Lambda 表達(dá)式 作為變換規(guī)則 ;
// Lambda 表達(dá)式
[](int element) {
return ++element;
}
最后 , 將 myVector 數(shù)組容器的 元素范圍 ( 起始迭代器 和 末尾迭代器 ) 作為輸入容器 , 將 myVector 數(shù)組容器的 的 起始迭代器 作為輸出容器 起始點(diǎn) , 也就是 將 輸入容器 的元素 進(jìn)行修改 , 再次放回到 該容器中 ;
// 向 transform 變換算法中 傳入 普通函數(shù)
transform(myVector.begin(), myVector.end(), myVector.begin(), [](int element) {
return ++element;
});
代碼示例 :
#include "iostream"
using namespace std;
#include <vector>
#include <algorithm>
#include "functional"
int main() {
// 創(chuàng)建一個 vector 集合容器
vector<int> myVector;
// 向容器中插入元素
myVector.push_back(9);
myVector.push_back(5);
myVector.push_back(2);
myVector.push_back(7);
// 向 transform 變換算法中 傳入 Lambda 表達(dá)式
transform(myVector.begin(), myVector.end(), myVector.begin(), [](int element) {
return ++element;
});
// 遍歷容器
for_each(myVector.begin(), myVector.end(), [](int element) {
cout << element << endl;
});
// 控制臺暫停 , 按任意鍵繼續(xù)向后執(zhí)行
system("pause");
return 0;
};
執(zhí)行結(jié)果 :
10
6
3
8
請按任意鍵繼續(xù). . .
4、代碼示例 - 傳入接受一個 一元函數(shù)對象 作為變換規(guī)則
在下面的代碼中 ,
首先 , 創(chuàng)建了一個 vector 數(shù)組容器 , 之后該容器 既作為輸入容器 , 又作為輸出容器 , 將元素輸入后 , 計算后 , 在輸出 到原來的容器中 ;
// 創(chuàng)建一個 vector 數(shù)組容器
vector<int> myVector;
然后 , 定義了一個 一元函數(shù)對象 , 使用該 一元函數(shù)對象 作為變換規(guī)則 ;
// 一元函數(shù)對象
class AddOne{
public:
int operator()(int& n){
return ++n;
}
};
最后 , 將 myVector 數(shù)組容器的 元素范圍 ( 起始迭代器 和 末尾迭代器 ) 作為輸入容器 , 將 myVector 數(shù)組容器的 的 起始迭代器 作為輸出容器 起始點(diǎn) , 也就是 將 輸入容器 的元素 進(jìn)行修改 , 再次放回到 該容器中 ;
// 向 transform 變換算法中 傳入 一元函數(shù)對象
auto AD = AddOne();
transform(myVector.begin(), myVector.end(), myVector.begin(), AD);
代碼示例 :
#include "iostream"
using namespace std;
#include <vector>
#include <algorithm>
#include "functional"
// 一元函數(shù)對象
class AddOne{
public:
int operator()(int& n){
return ++n;
}
};
int main() {
// 創(chuàng)建一個 vector 數(shù)組容器
vector<int> myVector;
// 向容器中插入元素
myVector.push_back(9);
myVector.push_back(5);
myVector.push_back(2);
myVector.push_back(7);
// 向 transform 變換算法中 傳入 一元函數(shù)對象
auto AD = AddOne();
transform(myVector.begin(), myVector.end(), myVector.begin(), AD);
// 遍歷容器
for_each(myVector.begin(), myVector.end(), [](int element) {
cout << element << endl;
});
// 控制臺暫停 , 按任意鍵繼續(xù)向后執(zhí)行
system("pause");
return 0;
};
執(zhí)行結(jié)果 :
10
6
3
8
請按任意鍵繼續(xù). . .
5、代碼示例 - 傳入接受一個 STL 中預(yù)定義的 一元函數(shù)對象 作為變換規(guī)則
在下面的代碼中 ,
首先 , 創(chuàng)建了一個 vector 數(shù)組容器 , 之后該容器 既作為輸入容器 , 又作為輸出容器 , 將元素輸入后 , 計算后 , 在輸出 到原來的容器中 ;
// 創(chuàng)建一個 vector 數(shù)組容器
vector<int> myVector;
然后 , 使用STL 中預(yù)定義的 一元函數(shù)對象 negate 作為變換規(guī)則 ; 該 預(yù)定義函數(shù)對象 源碼如下 , 可以看到直接將輸入?yún)?shù) 進(jìn)行取反 操作 , 在前面加上一個符號 " - " 返回 ;
// STRUCT TEMPLATE negate
template <class _Ty = void>
struct negate {
_CXX17_DEPRECATE_ADAPTOR_TYPEDEFS typedef _Ty argument_type;
_CXX17_DEPRECATE_ADAPTOR_TYPEDEFS typedef _Ty result_type;
constexpr _Ty operator()(const _Ty& _Left) const {
return -_Left;
}
};
最后 , 將 myVector 數(shù)組容器的 元素范圍 ( 起始迭代器 和 末尾迭代器 ) 作為輸入容器 , 將 myVector 數(shù)組容器的 的 起始迭代器 作為輸出容器 起始點(diǎn) , 也就是 將 輸入容器 的元素 進(jìn)行修改 , 再次放回到 該容器中 ;
// 向 transform 變換算法中 傳入 預(yù)定義一元函數(shù)對象
transform(myVector.begin(), myVector.end(), myVector.begin(), negate<int>());
代碼示例 :
#include "iostream"
using namespace std;
#include <vector>
#include <algorithm>
#include "functional"
int main() {
// 創(chuàng)建一個 vector 集合容器
vector<int> myVector;
// 向容器中插入元素
myVector.push_back(-9);
myVector.push_back(5);
myVector.push_back(-2);
myVector.push_back(7);
// 向 transform 變換算法中 傳入 預(yù)定義一元函數(shù)對象
transform(myVector.begin(), myVector.end(), myVector.begin(), negate<int>());
// 遍歷容器
for_each(myVector.begin(), myVector.end(), [](int element) {
cout << element << endl;
});
// 控制臺暫停 , 按任意鍵繼續(xù)向后執(zhí)行
system("pause");
return 0;
};
執(zhí)行結(jié)果 :
9
-5
2
-7
請按任意鍵繼續(xù). . .
6、代碼示例 - 傳入接受一個 使用 函數(shù)適配器 將預(yù)定義二元函數(shù)對象轉(zhuǎn)成的 一元函數(shù)對象
在下面的代碼中 ,
首先 , 創(chuàng)建了一個 vector 數(shù)組容器 , 之后該容器 既作為輸入容器 , 又作為輸出容器 , 將元素輸入后 , 計算后 , 在輸出 到原來的容器中 ;
// 創(chuàng)建一個 vector 數(shù)組容器
vector<int> myVector;
然后 , 使用 函數(shù)適配器 將預(yù)定義二元函數(shù)對象轉(zhuǎn)成的 一元函數(shù)對象 ,
// 使用 函數(shù)適配器 將預(yù)定義二元函數(shù)對象轉(zhuǎn)成的 一元函數(shù)對象
bind2nd(multiplies<int>(), 10)
multiplies 函數(shù)對象源碼如下 : 該函數(shù)對象的 重載 函數(shù)調(diào)用操作符 函數(shù) , 接收 2 個參數(shù) , 使用 bind2nd 函數(shù)適配器 為其設(shè)置第二個參數(shù)為 10 , 那么第一個參數(shù)就是 迭代器范圍的 元素 ;
// STRUCT TEMPLATE multiplies
template <class _Ty = void>
struct multiplies {
_CXX17_DEPRECATE_ADAPTOR_TYPEDEFS typedef _Ty first_argument_type;
_CXX17_DEPRECATE_ADAPTOR_TYPEDEFS typedef _Ty second_argument_type;
_CXX17_DEPRECATE_ADAPTOR_TYPEDEFS typedef _Ty result_type;
constexpr _Ty operator()(const _Ty& _Left, const _Ty& _Right) const {
return _Left * _Right;
}
};
最后 , 將 myVector 數(shù)組容器的 元素范圍 ( 起始迭代器 和 末尾迭代器 ) 作為輸入容器 , 將 myVector 數(shù)組容器的 的 起始迭代器 作為輸出容器 起始點(diǎn) , 也就是 將 輸入容器 的元素 進(jìn)行修改 , 再次放回到 該容器中 ;
// 向 transform 變換算法中 傳入 使用 函數(shù)適配器 將預(yù)定義二元函數(shù)對象轉(zhuǎn)成的 一元函數(shù)對象
transform(myVector.begin(), myVector.end(), myVector.begin(), bind2nd(multiplies<int>(), 10));
代碼示例 :
#include "iostream"
using namespace std;
#include <vector>
#include <algorithm>
#include "functional"
int main() {
// 創(chuàng)建一個 vector 集合容器
vector<int> myVector;
// 向容器中插入元素
myVector.push_back(9);
myVector.push_back(5);
myVector.push_back(2);
myVector.push_back(7);
// 向 transform 變換算法中 傳入 使用 函數(shù)適配器 將預(yù)定義二元函數(shù)對象轉(zhuǎn)成的 一元函數(shù)對象
transform(myVector.begin(), myVector.end(), myVector.begin(), bind2nd(multiplies<int>(), 10));
// 遍歷容器
for_each(myVector.begin(), myVector.end(), [](int element) {
cout << element << endl;
});
// 控制臺暫停 , 按任意鍵繼續(xù)向后執(zhí)行
system("pause");
return 0;
};
執(zhí)行結(jié)果 :
90
50
20
70
請按任意鍵繼續(xù). . .
7、代碼示例 - 將變換結(jié)果輸出到標(biāo)準(zhǔn)輸出流中
ostream_iterator 函數(shù)對象 是 一個模板類 , 其提供了一個輸出迭代器 , 可以將元素逐個寫入到輸出流中 , 通常是 std::ostream 對象 , 如 std::cout 標(biāo)準(zhǔn)輸出流 ;
ostream_iterator 函數(shù)對象 定義在 <iterator> 頭文件中 , 使用前先導(dǎo)入該頭文件 ;
// ostream_iterator 輸出流迭代器 頭文件
#include "iterator"
ostream_iterator 函數(shù)對象 的 構(gòu)造函數(shù)接受兩個參數(shù) :
- 一個輸出流對象的引用
- 一個可選的分隔符字符串 ;
每次迭代器被解引用以寫入元素時 , 它都會將元素寫入輸出流 , 并在元素之間插入分隔符 ;
在下面的代碼中 ,
首先 , 創(chuàng)建了一個 vector 數(shù)組容器 , 之后該容器 既作為輸入容器 , 又作為輸出容器 , 將元素輸入后 , 計算后 , 在輸出 到原來的容器中 ;
// 創(chuàng)建一個 vector 數(shù)組容器
vector<int> myVector;
然后 , 使用 函數(shù)適配器 將預(yù)定義二元函數(shù)對象轉(zhuǎn)成的 一元函數(shù)對象 ,
// 使用 函數(shù)適配器 將預(yù)定義二元函數(shù)對象轉(zhuǎn)成的 一元函數(shù)對象
bind2nd(multiplies<int>(), 10)
multiplies 函數(shù)對象源碼如下 : 該函數(shù)對象的 重載 函數(shù)調(diào)用操作符 函數(shù) , 接收 2 個參數(shù) , 使用 bind2nd 函數(shù)適配器 為其設(shè)置第二個參數(shù)為 10 , 那么第一個參數(shù)就是 迭代器范圍的 元素 ;
// 向 transform 變換算法中 傳入 使用 函數(shù)適配器 將預(yù)定義二元函數(shù)對象轉(zhuǎn)成的 一元函數(shù)對象
// 將變換結(jié)果 , 輸出到 屏幕 標(biāo)準(zhǔn)輸出流 中
transform(myVector.begin(), myVector.end(), ostream_iterator<int>(cout, " "), bind2nd(multiplies<int>(), 10));
最后 , 將 myVector 數(shù)組容器的 元素范圍 ( 起始迭代器 和 末尾迭代器 ) 作為輸入容器 , 將 myVector 數(shù)組容器的 的 起始迭代器 作為輸出容器 起始點(diǎn) , 也就是 將 輸入容器 的元素 進(jìn)行修改 , 再次放回到 該容器中 ;
// 向 transform 變換算法中 傳入 使用 函數(shù)適配器 將預(yù)定義二元函數(shù)對象轉(zhuǎn)成的 一元函數(shù)對象
transform(myVector.begin(), myVector.end(), myVector.begin(), bind2nd(multiplies<int>(), 10));
代碼示例 :
#include "iostream"
using namespace std;
#include <vector>
#include <algorithm>
#include "functional"
// ostream_iterator 輸出流迭代器 頭文件
#include "iterator"
int main() {
// 創(chuàng)建一個 vector 集合容器
vector<int> myVector;
// 向容器中插入元素
myVector.push_back(9);
myVector.push_back(5);
myVector.push_back(2);
myVector.push_back(7);
// 向 transform 變換算法中 傳入 使用 函數(shù)適配器 將預(yù)定義二元函數(shù)對象轉(zhuǎn)成的 一元函數(shù)對象
// 將變換結(jié)果 , 輸出到 屏幕 標(biāo)準(zhǔn)輸出流 中
transform(myVector.begin(), myVector.end(), ostream_iterator<int>(cout, " "), bind2nd(multiplies<int>(), 10));
// 控制臺暫停 , 按任意鍵繼續(xù)向后執(zhí)行
system("pause");
return 0;
};
執(zhí)行結(jié)果 :文章來源:http://www.zghlxwxcb.cn/news/detail-810006.html
90 50 20 70 請按任意鍵繼續(xù). . .
文章來源地址http://www.zghlxwxcb.cn/news/detail-810006.html
到了這里,關(guān)于【C++】STL 算法 - transform 變換算法 ② ( 變換規(guī)則為 普通函數(shù) | 變換規(guī)則為 Lambda 表達(dá)式 | 變換規(guī)則為 函數(shù)對象 | 變換規(guī)則為 函數(shù)適配器轉(zhuǎn)換的函數(shù)對象 )的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!