介绍
algorithm算法库拥有非常多算法,这可以节省不少力气,这些库主要是针对容器库的
不修改序列的操作
all_of(),any_of(),none_of()
这些函数测试序列是否满足某一条件
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> a = {2, 4, 6, 8, 10};
bool b = all_of(a.begin(), a.end(), [](int i) {
return i % 2 == 0;
});
bool c = any_of(a.begin(), a.end(), [](int i) {
return i % 2 == 0;
});
bool d = none_of(a.begin(), a.end(), [](int i) {
return i % 2 == 0;
});
cout << b << "\n";
cout << c << "\n";
cout << d;
return 0;
}
返回结果是
1
1
0
for_each()
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> a = {2, 4, 6, 8, 10};
for_each(a.begin(), a.end(), [](int i) {
cout << i << " ";
});
return 0;
}
查找函数
find()
查找第一个关键字
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> a = {2, 4, 6, 8, 10};
int b[2] = {8, 10};
auto it = find(a.begin(), a.end(), 10);
cout << *it;
return 0;
}
find_if(),find_if_not()
查找第一个满足条件的元素
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> a = {2, 4, 6, 8, 10};
int b[2] = {8, 10};
auto it = find_if(a.begin(), a.end(), [](int i) {
return i == 10;
});
cout << *it;
return 0;
}
find_end()
查找最后满足的子序列,有子序列则返回子序列第一个元素的迭代器,否则返回0
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> a = {2, 4, 6, 8, 10};
int b[2] = {8, 10};
auto it = find_end(a.begin(), a.end(), b, b + 2);
cout << *it;
return 0;
}
find_first_of()
find_first_of:查找A序列中第一个与序列B中任一元素值相等的元素位置
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> a = {2, 4, 6, 8, 10};
int b[2] = {8, 10};
auto it = find_first_of(a.begin(), a.end(), b, b + 2);
cout << *it;
return 0;
}
adjacent_find()
查找相等的相邻元素
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> a = {2, 4, 6, 8, 8};
int b[2] = {8, 10};
auto it = adjacent_find(a.begin(), a.end());
cout << *it;
return 0;
}
有则返回第一对相同元素的迭代器,无相邻相同元素则返回0
count()
计算某一个值的元素数
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> a = {2, 4, 6, 8, 8, 9, 9};
int b[2] = {8, 10};
int c = count(a.begin(), a.end(), 8);
cout << c;
return 0;
}
count_if()
查找满足条件的元素的个数
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> a = {2, 4, 6, 8, 8, 9, 9};
int b[2] = {8, 10};
int c = count_if(a.begin(), a.end(), [](int i) {
return i % 2 == 0;
});
cout << c;
return 0;
}
mismatch()
查找两个序列第一个不匹配的位置
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> a = {2, 4, 6, 8, 8, 9, 9};
int b[4] = {2, 4, 7, 8};
pair<vector<int>::iterator, int*> p = mismatch(a.begin(), a.end(), b);
cout << *p.first<<" ";
cout << *p.second;
return 0;
}
返回结果
6 7
equal()
判断范围内是否相等
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> a = {2, 4, 6, 8, 8, 9, 9};
int b[4] = {2, 4, 6, 8};
bool c = equal(a.begin(), a.begin() + 4, b, b + 4);
cout << c;
return 0;
}
is_permutation()
判断一个序列是否是另一个序列的排列
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> a = {2, 4, 6, 8};
int b[4] = {4, 2, 8, 6};
bool c = is_permutation(a.begin(), a.end(), b);
cout << c;
return 0;
}
search()
搜索子序列
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> a = {2, 4, 6, 8};
int b[2] = {4, 6};
auto it = search(a.begin(), a.end(), b, b + 2);
cout << *it;
return 0;
}
search_n()
搜索若干个值
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> a = {2, 4, 6, 6};
auto it = search_n(a.begin(), a.end(), 2, 6);
cout << *it;
return 0;
}
??正文结束??
本文深入探讨了C++标准库中的算法部分,包括用于测试序列条件的all_of()、any_of()、none_of()函数,以及序列遍历、查找、计数等功能。详细介绍了find()、find_if()、count()等函数的应用场景和使用方法。
11万+

被折叠的 条评论
为什么被折叠?



