std::vector<int> vc;
vc.emplace_back(1);fill(vc,1,9);print("vc: ", vc);auto it = std::find(vc.begin(), vc.end(),1);
std::cout <<"the first element equals 1 is No."<< std::distance(vc.begin(), it)<< std::endl;auto it1 = std::find(vc.begin(), vc.end(),10);if(it1 != vc.end()){
std::cout <<"the first element equals 10 is No."<< std::distance(vc.begin(), it1)<< std::endl;}else{
std::cout <<"no element equals 10 in vc."<< std::endl;}
结果
vc:1123456789
the first element equals 1 is No.0
no element equals 10 in vc.
template<classInputIt,classUnaryPredicate>
InputIt find_if( InputIt first, InputIt last, UnaryPredicate p );(1)(C++20 前)template<classInputIt,classUnaryPredicate>constexpr InputIt find_if( InputIt first, InputIt last, UnaryPredicate p );(2)(C++20 起)template<classExecutionPolicy,classForwardIt,classUnaryPredicate>
ForwardIt find_if( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last,
UnaryPredicate p );(3)(C++17 起)
功能
查找第一个 使 UnaryPredicate p 返回 true 的元素
Demo
print("vc: ", vc);auto it = std::find_if(vc.begin(), vc.end(), even);
std::cout <<"the first even element is No."<< std::distance(vc.begin(), it)<< std::endl;
print("vc: ", vc);auto it = std::find_if_not(vc.begin(), vc.end(), even);
std::cout <<"the first odd element is No."<< std::distance(vc.begin(), it)<< std::endl;
结果
vc:1123456789
the first odd element is No.0
五 std::search_n
定义
template<classForwardIt,classSize,classT>
ForwardIt search_n( ForwardIt first, ForwardIt last, Size count,const T& value );(C++20 前)template<classForwardIt,classSize,classT>constexpr ForwardIt search_n( ForwardIt first, ForwardIt last, Size count,const T& value );(C++20 起)template<classExecutionPolicy,classForwardIt,classSize,classT>
ForwardIt search_n( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, Size count,const T& value );(C++17 起)template<classForwardIt,classSize,classT,classBinaryPredicate>
ForwardIt search_n( ForwardIt first, ForwardIt last, Size count,const T& value, BinaryPredicate p );(C++20 前)template<classForwardIt,classSize,classT,classBinaryPredicate>constexpr ForwardIt search_n( ForwardIt first, ForwardIt last, Size count,const T& value,
BinaryPredicate p );(C++20 起)template<classExecutionPolicy,classForwardIt,classSize,classT,classBinaryPredicate>
ForwardIt search_n( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, Size count,const T& value,
BinaryPredicate p );(C++17 起)
功能
查找前n个连续匹配值
Demo
print("vc: ", vc);auto it = std::search_n(vc.begin(), vc.end(),2,1);
std::cout <<"2 consecutive element equal 1 start at No."<< std::distance(vc.begin(), it)<< std::endl;// 由于不在早期的STL规范中,所以是binary predicate 而不是unary predicateauto it1 = std::search_n(vc.begin(), vc.end(),2,1, my_equal);
std::cout <<"2 consecutive element equal 1 start at No."<< std::distance(vc.begin(), it1)<< std::endl;
结果
2 consecutive element equal 1 start at No.02 consecutive element equal 1 start at No.0
std::list<int> l;fill(l,3,6);print("vc: ", vc);print("list: ", l);auto it = std::find_first_of(vc.begin(), vc.end(), l.begin(), l.end());
std::cout <<"the first 3 4 5 6 position in vc is No."<< std::distance(vc.begin(), it)<< std::endl;
结果
vc:11234567893456
list:3456
the first 3456 position in vc is No.3
九 std::adjacent_find
定义
template<classForwardIt>
ForwardIt adjacent_find( ForwardIt first, ForwardIt last );(1)(C++20 前)template<classForwardIt>constexpr ForwardIt adjacent_find( ForwardIt first, ForwardIt last );(1)(C++20 起)template<classExecutionPolicy,classForwardIt>
ForwardIt adjacent_find( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last );(2)(C++17 起)template<classForwardIt,classBinaryPredicate>
ForwardIt adjacent_find( ForwardIt first, ForwardIt last, BinaryPredicate p );(3)(C++20 前)template<classForwardIt,classBinaryPredicate>constexpr ForwardIt adjacent_find( ForwardIt first, ForwardIt last, BinaryPredicate p );(3)(C++20 起)template<classExecutionPolicy,classForwardIt,classBinaryPredicate>
ForwardIt adjacent_find( ExecutionPolicy&& policy,
ForwardIt first, ForwardIt last, BinaryPredicate p );(4)(C++17 起)
功能
查找两个连续且相等的元素
Demo
std::list<int> l;fill(l,3,6);fill(l,6,7);fill(l,7,8);print("list: ", l);auto it = std::adjacent_find(l.begin(), l.end());
std::cout
<<"the first two same consecutive element position in list is No."<< std::distance(l.begin(), it)<< std::endl;
结果
list:34566778
the first two same consecutive element position in list is No.3