序号 | 功能 | 函数名称 | 说明 |
1 | 循环 | for_each | 遍历容器元素,对每元素执行相同的操作 |
2 | 查询 | find | 在序列中找出某个值的第一次出现的位置 |
find_if | 在序列中找出符合某谓词的第一个元素 | ||
find_first_of | 在序列中找出第一次出现指定值集中之值的位置 | ||
adjacent_find | 在序列中找出第一次相邻值相等元素的位置 | ||
find_end | 在序列中找出一子序列的最后一次出现的位置 | ||
search | 在序列中找出一子序列的第一次出现的位置 | ||
search_n | 在序列中找出一值连续n次出现的位置 | ||
3 | 计数 | count | 在序列中统计某个值出现的次数 |
count_if | 在序列中统计与某谓词(表达式)匹配的次数 | ||
4 | 比较 | equal | 两个序列中的对应元素都相同时为真 |
mismatch | 找出两个序列相异的第一个元素 |
1.1、for_each
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
class Print
{
private:
int sum;
int max;
int min;
int count;
public:
Print() :count(0), sum(0) {}
int GetSum() { return sum; }
int GetMax() { return max; }
int GetMin() { return min; }
void operator()(int x)
{
if (count == 0)
{
max = x;
min = x;
}
else
{
if (max < x)
{
max = x;
}
if (min>x)
{
min = x;
}
}
sum += x;
count++;
}
};
int main()
{
vector<int> ivec = { 1, 4, 2, 8, 5, 7 };
Print p = for_each(ivec.begin(), ivec.end(), Print());
cout << "Max: " << p.GetMax() << endl;
cout << "Min: " << p.GetMin() << endl;
cout << "Sum: " << p.GetSum() << endl;
}
1.2、for_each 采用模板传参技术
#include<iostream>
#include<functional>
#include<algorithm>
#include<vector>
using namespace std;
template <class T,class _outpara>
class Print:public unary_function<T,_outpara>
{
private:
T sum;
T max;
T min;
T count;
public:
Print() :count(0), sum(0) {}
T GetSum() { return sum; }
T GetMax() { return max; }
T GetMin() { return min; }
_outpara operator()(T x)
{
if (count == 0)
{
max = x;
min = x;
}
else
{
if (max < x)
{
max = x;
}
if (min>x)
{
min = x;
}
}
sum += x;
count++;
}
};
int main()
{
vector<double> ivec = { 1.2, 4.5, 2.5, 8.5, 5.8, 7.5 };
Print<double, void> p = for_each(ivec.begin(), ivec.end(), Print<double, void>());
cout << "Max: " << p.GetMax() << endl;
cout << "Min: " << p.GetMin() << endl;
cout << "Sum: " << p.GetSum() << endl;
}
2、查询
#include<iostream>
#include<string>
#include<vector>
#include<functional>
#include<algorithm>
using namespace std;
int main()
{
string str("i love you");
string str1 = "you";
vector<int>ivec{ 1, 2, 2, 3,3,4,5 };
//find()只能用于找数字或者单个字符,若需找字符串匹配需用str.find() 或者search() 没有str.search()这种形式
vector<int>::iterator ix = find(ivec.begin(), ivec.end(), 5);
if (ix != ivec.end())
cout << "find it" << endl;
else
cout << "not find" << endl;
string::iterator iter=find(str.begin(), str.end(), 'y');
if (iter == str.end())
cout << "not find" << endl;
else
cout << "find it: " << iter - str.begin() << endl;
int pos=str.find(str1);
if (pos == string::npos)
cout << "not find" << endl;
else
cout << "find it: " <<pos<< endl;
iter = search(str.begin(), str.end(), str1.begin(), str1.end());
if (iter == str.end())
cout << "not find" << endl;
else
cout <<"find it: "<< iter - str.begin()<<endl;
//find_if() 没有str.find_if()
ix = find_if(ivec.begin(), ivec.end(), bind2nd(greater<int>(), 3));
if (ix != ivec.end())
cout << "find it" << endl;
else
cout << "not find" << endl;
//find_first_of()与str.find_first_of()用法区别
iter = find_first_of(str.begin(), str.end(), str1.begin(),str1.end());
if (iter == str.end())
cout << "not find" << endl;
else
cout << "find it: " << iter - str.begin() << endl;
pos = str.find_first_of("sdaadosf");
if (pos != string::npos)
cout << "find it: " << pos << endl;
else
cout << "not find" << endl;
//adjacent_find() 查找首次相邻的两个元素
ix = adjacent_find(ivec.begin(), ivec.end());
if (ix != ivec.end())
cout << "adjacent find it: " <<ix-ivec.begin()<<endl;
else
cout << "not find" << endl;
//search_n 查找首次出现两个3位置
ix = search_n(ivec.begin(), ivec.end(), 2, 3);
if (ix != ivec.end())
cout << "search_n find it: " << ix - ivec.begin() << endl;
else
cout << "not find" << endl;
}
3、计数
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
class Student
{
public:
int NO;
string name;
int grade;
Student(int NO, string name, int grade) :NO(NO), name(name), grade(grade) {}
bool operator==(int grade)
{
return this->grade == grade;
}
};
class Match
{
int grade;
public:
Match(int grade)
{
this->grade = grade;
}
bool operator()(Student &s)
{
return s.grade > grade;
}
};
int main()
{
vector<Student> v;
Student s1(1000, "张三", 80);
Student s2(1001, "李四", 85);
Student s3(1002, "王五", 80);
Student s4(1003, "赵六", 80);
v.push_back(s1);
v.push_back(s2);
v.push_back(s3);
v.push_back(s4);
int m = count(v.begin(), v.end(), 80);
cout << "80分有几人: " << m << endl;
int n = count_if(v.begin(), v.end(), Match(80));
cout << "大于80分有几人: " << n << endl;
}
4、比较
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int a1[] = { 3, 1, 4, 1, 5, 9, 3 };
int a2[] = { 3, 1, 4, 2, 8, 5, 7 };
int n = sizeof(a1) / sizeof(int);
cout << "相比后" << equal(a1, a1 + n, a2) << endl;
pair<int*, int*>result = mismatch(a1, a1 + n, a2);
cout << *(result.first) << " " << *(result.second) << endl;
}