一、for_each()遍历算法
?
# include <iostream>
# include <vector>
# include <algorithm>
using namespace std;
void printf_01 (int a)
{
cout<<a<<" ";
}
class printf_02
{
public:
void operator ()(int a)
{
cout<<a<<" ";
}
};
int main (void)
{
vector<int> v;
for(int i=0;i<10;++i)
{
v.push_back(i);
}
for_each(v.begin(),v.end(),printf_01);//第一种,用普通函数实现
cout<<endl<<"---------------"<<endl;
for_each(v.begin(),v.end(),printf_02());//第二种,用仿函数实现
return 0;
}
?
二、transform()遍历算法
# include <iostream>
# include <vector>
# include <algorithm>
using namespace std;
class printf_01
{
public:
void operator ()(int a)
{
cout<<a<<" ";
}
};
class transform_01
{
public:
int operator ()(int a)
{
return a+100;
}
};
int main (void)
{
vector<int> v;
for(int i=0;i<10;++i)
{
v.push_back(i);
}
for_each(v.begin(),v.end(),printf_01());
cout<<endl<<"-----------------"<<endl;
vector<int> v1;
v1.resize(v.size());
transform(v.begin(),v.end(),v1.begin(),transform_01());//在使用transform前一定要先 v1.resize(v.size())开辟空间
for_each(v1.begin(),v1.end(),printf_01());
return 0;
}
?三、find查找算法
//常规查找
# include <iostream>
# include <vector>
# include <algorithm>
using namespace std;
int main (void)
{
vector<int> v;
for(int i=0;i<10;++i)
{
v.push_back(i);
}
vector<int>::iterator it=find(v.begin(),v.end(),5);
if(it==v.end())
{
cout<<"Not find!"<<endl;
}
else
{
cout<<"Find it!"<<" "<<*it<<endl;
}
return 0;
}
//自定义数据类型查找
# include <iostream>
# include <vector>
# include <string>
# include <algorithm>
using namespace std;
class student
{
public:
student(string m_name,int m_age)
{
name=m_name;
age=m_age;
}
bool operator==(const student& stu)//自定义数据类型必须指明查找标准,重载=运算符
{
if(age==stu.age&&name==stu.name)
return true;
else
return false;
}
int age;
string name;
};
int main (void)
{
student stu1("aaa",10);
student stu2("bbb",20);
student stu3("ccc",30);
student stu4("ddd",40);
student stu5("eee",50);
vector<student> v;
v.push_back(stu1);
v.push_back(stu2);
v.push_back(stu3);
v.push_back(stu4);
v.push_back(stu5);
student stu("ccc",30);
vector<student>::iterator it=find(v.begin(),v.end(),stu);
if(it==v.end())
{
cout<<"Not find!"<<endl;
}
else
{
cout<<"Find it!"<<endl<<"Name:"<<it->name<<" "<<"Age:"<<it->name<<endl;
}
return 0;
}
四、find_if查找算法
//自定义数据查找
# include <iostream>
# include <vector>
# include <string>
# include <algorithm>
using namespace std;
class student
{
public:
student(string m_name,int m_age)
{
name=m_name;
age=m_age;
}
int age;
string name;
};
class compare
{
public:
bool operator()(student stu)
{
return stu.age>20;
}
};
int main (void)
{
student stu1("aaa",10);
student stu2("bbb",20);
student stu3("ccc",30);
vector<student> v;
v.push_back(stu1);
v.push_back(stu2);
v.push_back(stu3);
vector<student>::iterator it=find_if(v.begin(),v.end(),compare());
if(it!=v.end())
{
cout<<"Find it!"<<endl<<"Age:"<<it->age<<" "<<"Name:"<<it->name<<endl;
}
else
{
cout<<"Not find it!"<<endl;
}
return 0;
}
//普通查找
# include <iostream>
# include <vector>
# include <algorithm>
using namespace std;
class greatertwo
{
public:
bool operator ()(int a)
{
return a>20;
}
};
int main (void)
{
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
vector<int>::iterator it=find_if(v.begin(),v.end(),greatertwo());
if(it!=v.end())
{
cout<<"Find it!"<<" "<<*it<<endl;
}
else
{
cout<<"Not find it!"<<endl;
}
return 0;
}
五、adjacent_find查找相邻且相同元素的算法
# include <iostream>
# include <vector>
# include <algorithm>
using namespace std;
int main (void)
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(3);
v.push_back(4);
vector<int>::iterator it=adjacent_find(v.begin(),v.end());//adjacent_find()
if(it==v.end())
{
cout<<"Not find it!"<<endl;
}
else
{
cout<<"Find it!"<<endl<<*it<<" "<<*(it++)<<endl;
}
return 0;
}
六、binary_search()查找某个元素是否存在算法(元素递增的等差数列)? ? ?????
# include <iostream>
# include <vector>
# include <algorithm>
using namespace std;
int main (void)
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(5);
/*
v.push_back(5);//如果是这样就错了
v.push_back(4);
v.push_back(3);
v.push_back(2);
v.push_back(1);
*/
bool a=binary_search(v.begin(),v.end(),3);
if(a)
{
cout<<"Find it!"<<endl;
}
else
{
cout<<"Not find it!"<<endl;
}
return 0;
}
七、count()统计相同个数算法
//统计普通类型
# include <iostream>
# include <vector>
# include <algorithm>
using namespace std;
int main (void)
{
vector<int> v;
v.push_back(1);
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
int a=count(v.begin(),v.end(),1);
cout<<"The number is:"<<a<<endl;
return 0;
}
//统计自定义数据类型
# include <iostream>
# include <string>
# include <vector>
# include <algorithm>
using namespace std;
class student
{
public:
student(string m_name,int m_age)
{
name=m_name;
age=m_age;
}
bool operator ==(student stu)//需要重载=运算符
{
if(stu.age==age)
return true;
else
return false;
}
string name;
int age;
};
int main (void)
{
vector<student> v;
student stu1("aaa",29);
student stu2("bbb",29);
student stu3("ccc",49);
v.push_back(stu1);
v.push_back(stu2);
v.push_back(stu3);
student stu4("ddd",29);
int num=count(v.begin(),v.end(),stu4);
cout<<num<<endl;
return 0;
}
八、count_if按照条件查找
//普通查找
# include <iostream>
# include <vector>
# include <algorithm>
using namespace std;
class compare
{
public:
bool operator ()(int a)
{
return a>2;
}
};
int main (void)
{
vector<int> v;
v.push_back(1);
v.push_back(3);
v.push_back(4);
v.push_back(4);
int num=count_if(v.begin(),v.end(),compare());
cout<<num<<endl;
return 0;
}
//自定义数据类型的查找
# include <iostream>
# include <string>
# include <vector>
# include <algorithm>
using namespace std;
class student
{
public:
student(string m_name,int m_age)
{
name=m_name;
age=m_age;
}
string name;
int age;
};
class compare
{
public:
bool operator ()(const student stu)
{
return stu.age>25;
}
};
int main (void)
{
vector<student> v;
student stu1("aaa",30);
student stu2("bbb",20);
student stu3("ccc",30);
v.push_back(stu1);
v.push_back(stu2);
v.push_back(stu3);
int num=count_if(v.begin(),v.end(),compare());
cout<<num<<endl;
return 0;
}
九、sort排序算法
# include <iostream>
# include <vector>
# include <algorithm>
# include <functional>
using namespace std;
void myprintf(int a)
{
cout<<a<<" ";
}
int main (void)
{
vector<int> v;
v.push_back(10);
v.push_back(30);
v.push_back(20);
sort(v.begin(),v.end());
for_each(v.begin(),v.end(),myprintf);
cout<<endl<<"------------"<<endl;
sort(v.begin(),v.end(),greater<int>());
for_each(v.begin(),v.end(),myprintf);
cout<<endl;
return 0;
}
十、random_shuffle洗牌算法
# include <iostream>
# include <vector>
# include <algorithm>
# include <ctime> //time()函数所在库(或者"time.h")
# include <stdlib.h> //rand()函数和srand()函数所在库
using namespace std;
void myprintf(int a)
{
cout<<a<<" ";
}
int main (void)
{
srand(time(NULL));//加入随机数种子,使每次洗牌算法执行结果不同
vector<int> v;
for(int i=0;i<10;++i)
{
v.push_back(i);
}
for_each(v.begin(),v.end(),myprintf);
cout<<endl<<"----------------------"<<endl;
random_shuffle(v.begin(),v.end());
for_each(v.begin(),v.end(),myprintf);
cout<<endl;
return 0;
}
? ? ? ? ?
|