1、STL算法--find_if()
(1)、代碼如下:
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
template<typename Type>
class IsDiv{
public:
IsDiv(const Type &divisor){
this->divisor = divisor;
}
bool operator()(Type &t){
return t%divisor == 0;
}
protected:
private:
Type divisor;
};
int main(void){
vector<int> v2;
for(int i = 10; i < 33; i++){
v2.push_back(i);
}
int a = 4;
IsDiv<int> myDiv(a);
//find_if(v2.begin(), v2.end(), myDiv);
vector<int>::iterator it;
it =find_if(v2.begin(), v2.end(), IsDiv<int>(a) );
if(it == v2.end()){
cout<<"容器中沒有值是4的元素"<<endl;
}else{
cout<<"第一個被4整除的元素是:"<<*it<<endl;
}
return 0;
}(2)、運行結果:

2、STL算法--plus的使用
(1)、代碼如下:
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
//plus 預定義好的函數對象,能實現不同數據 + 算法;
//實現了數據類型和算法的分離======》通過函數對象技術實現的;
//
//思考,怎么知道plus<type>是2個參數------>多看看源碼;
void main21(){
plus<int> intAdd;
int x = 10;
int y = 20;
int z = intAdd(x, y);
cout<<"z:"<<z<<endl;
plus<string> stringAdd;
string s1 = "aaa";
string s2 = "bbb";
string s3 = stringAdd(s1, s2);
cout<<"s3:"<<s3<<endl;
vector<string> v1;
v1.push_back("bbb");
v1.push_back("aaa");
v1.push_back("ccc");
v1.push_back("zzz");
v1.push_back("ccc");
v1.push_back("ccc");
sort(v1.begin(), v1.end(), greater<string>()); //降序排列;
vector<string>::iterator it;
for(it = v1.begin(); it != v1.end(); it++){
cout<<*it<<endl;
}
//求“ccc”出現的字符串的個數;
string sc = "ccc"; //函數適配器:將函數和參數強行綁定;
//equal_to<string>有2個參數,left參數來自容器,right參數來自sc,
//bind2nd就是函數適配器:把預定義函數對象和第二個參數進行綁定;`
int num = count_if(v1.begin(), v1.end(), bind2nd(equal_to<string>(), sc));
cout<<"num:"<<num<<endl;
}
int main(void){
main21();
return 0;
}(2)、運行結果:

3、STL算法--for_each()
(1)、代碼如下:
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
void printV(vector<int> &v){
vector<int>::iterator it;
for(it = v.begin(); it != v.end(); it++){
cout<<*it<<" ";
}
cout<<endl;
}
void showElem(int &n){
cout<<n<<" ";
}
class MyShow{
public:
MyShow(){
num = 0;
}
void operator()(int &n){
num++;
cout<<n<<" ";
}
void printNum(){
cout<<"num :"<<num<<endl;
}
private:
int num;
};
int main(void){
vector<int> v1;
v1.push_back(1);
v1.push_back(3);
v1.push_back(5);
printV(v1);
//第三個參數是:函數對象/回掉函數
//for_each(v1.begin(), v1.end(), showElem); //利用的是回調函數
for_each(v1.begin(), v1.end(), MyShow()); //利用的是函數對象(這個類中重載了())
//函數的返回值是函數對象
cout<<endl;
MyShow my1 = for_each(v1.begin(), v1.end(), MyShow()); //利用的是函數對象(這個類中重載了())
my1.printNum();
return 0;
}(2)、運行結果:

4、for_each()和transform()的區別
(1)、代碼如下:
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
void showElem(int &n){
cout<<n<<" ";
}
int showElem2(int &n){
cout<<n<<" ";
return n;
}
//for_each和transform的本質區別:
//結論:
//1、一般情況下,for_each所使用的函數對象,參數是引用,沒有返回值;
//2、transform所使用的函數對象,參數一般不使用引用,而是還有返回值;
int main(void){
vector<int> v1;
v1.push_back(1);
v1.push_back(3);
v1.push_back(5);
vector<int> v2 = v1;
for_each(v1.begin(), v1.end(), showElem);
transform(v2.begin(), v2.end(), v2.begin(), showElem2);//transform對回調函數的要求;返回值必須有
cout<<endl;
return 0;
}運行結果:

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。