template< class InputIt, class UnaryPredicate > bool all_of( InputIt first, InputIt last, UnaryPredicate p ); template< class InputIt, class UnaryPredicate > bool any_of( InputIt first, InputIt last, UnaryPredicate p ); template< class InputIt, class UnaryPredicate > bool none_of( InputIt first, InputIt last, UnaryPredicate p );
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
vector<int> v = { 1, 3, 5, 7, 9 };
auto isEven = [](int i){return i % 2 != 0;
bool isallOdd = std::all_of(v.begin(), v.end(), isEven);
if (isallOdd)
cout << "all is odd" << endl;
bool isNoneEven = std::none_of(v.begin(), v.end(), isEven);
if (isNoneEven)
cout << "none is even" << endl;
vector<int> v1 = { 1, 3, 5, 7, 8, 9 };
bool anyof = std::any_of(v1.begin(), v1.end(), isEven);
if (anyof)
cout << "at least one is even" << endl;
}
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
vector<int> v = { 1, 3, 5, 7, 9,4 };
auto isEven = [](int i){return i % 2 == 0;};
auto firstEven = std::find_if(v.begin(), v.end(), isEven);
if (firstEven!=v.end())
cout << "the first even is " <<* firstEven << endl;
//用find_if来查找奇数则需要重新写一个否定含义的判断式
auto isNotEven = [](int i){return i % 2 != 0;};
auto firstOdd = std::find_if(v.begin(), v.end(),isNotEven);
if (firstOdd!=v.end())
cout << "the first odd is " <<* firstOdd << endl;
//用find_if_not来查找奇数则无需新定义判断式
auto odd = std::find_if_not(v.begin(), v.end(), isEven);
if (odd!=v.end())
cout << "the first odd is " <<* odd << endl;
}
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
vector<int> v = { 1, 3, 5, 7, 9, 4 };
std::vector<int> v1(v.size());
//根据条件拷贝
auto it = std::copy_if(v.begin(), v.end(), v1.begin(), [](int i){return i%2!=0;});
//缩减vector到合适大小
v1.resize(std::distance(v1.begin(),it));
for(int i : v1)
{
cout<<i<<" ";
}
cout<<endl;
}
#include <numeric>
#include <array>
#include <vector>
#include <iostream>
using namespace std;
int main()
{
vector<int> v(4) ;
//循环遍历赋值来初始化数组
//for(int i=1; i<=4; i++)
//{
// v.push_back(i);
//}
//直接通过iota初始化数组,更简洁
std::iota(v.begin(), v.end(), 1);
for(auto n: v) {
cout << n << ' ';
}
cout << endl;
std::array<int, 4> array;
std::iota(array.begin(), array.end(), 1);
for(auto n: array) {
cout << n << ' ';
}
std::cout << endl;
}
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
// your code goes here
vector<int> v = { 1, 2, 5, 7, 9, 4 };
auto result = minmax_element(v.begin(), v.end());
cout<<*result.first<<" "<<*result.second<<endl;
return 0;
}
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
vector<int> v = { 1, 2, 5, 7, 9, 4 };
auto pos = is_sorted_until(v.begin(), v.end());
for(auto it=v.begin(); it!=pos; ++it)
{
cout<<*it<< " ";
}
cout<<endl;
bool is_sort = is_sorted(v.begin(), v.end());
cout<< is_sort<<endl;
return 0;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有