STL algorithm算法find_first_of(18)

原文地址:http://www.cplusplus.com/reference/algorithm/find_first_of/
function template
<algorithm>

std::find_first_of

equality (1)
template <class InputIterator, class ForwardIterator>
   ForwardIterator1 find_first_of (InputIterator first1, InputIterator last1,
                                   ForwardIterator first2, ForwardIterator last2);
predicate (2)
template <class InputIterator, class ForwardIterator, class BinaryPredicate>
   ForwardIterator1 find_first_of (InputIterator first1, InputIterator last1,
                                   ForwardIterator first2, ForwardIterator last2,
                                   BinaryPredicate pred);
Find element from set in range
Returns an iterator to the first element in the range [first1,last1) that matches any of the elements in [first2,last2). If no such element is found, the function returns last1.

返回一个迭代器,指向范围[first1,last1)中第一个匹配到[first2,last1)中的任一(并不要求全部匹配)元素。如果没有匹配的,则返回last1.

例子:

#include <iostream>
#include <vector>
#include <array>
#include <algorithm>
using namespace std;
void findfirstof()
{
    vector<int> v1{1,2,3,4,5,6,3,4,9,8};
    int arr[]={4,8};
    array<double,2> ai{77,88};

    cout<<"v1=";
    for(int &i:v1)
        cout<<i<<"  ";
    cout<<endl;

    cout<<"arr=";
    for(int &i:arr)
        cout<<i<<"  ";

    cout<<endl<<"ai=";
    for(double &i:ai)
        cout<<i<<"  ";

    cout<<endl<<"auto it=find_first_of(v1.begin(),v1.end(),arr,arr+2);"<<endl;
    auto it=find_first_of(v1.begin(),v1.end(),arr,arr+2);
    cout<<"*(it-1)="<<*(it-1)<<endl;
    cout<<"*it="<<*it<<endl;
    cout<<"*(it+1)="<<*(it+1)<<endl;


    cout<<endl<<"auto it2=find_first_of(v1.begin(),v1.end(),ai.begin(),ai.end());"<<endl;
    auto it2=find_first_of(v1.begin(),v1.end(),ai.begin(),ai.end());
    cout<<"*(it2-1)="<<*(it2-1)<<endl;
    cout<<"*it2="<<*it2<<endl;
    cout<<"*(it2+1)="<<*(it2+1)<<endl;

}

例子:


可以看到,只要匹配到一个元素符号要求,就返回。

The elements in [first1,last1) are sequentially compared to each of the values in [first2,last2) using operator== (orpred, in version (2)), until a pair matches.

[first1,last1)中的每一个元素依次和[first2,last2)中的每一个元素匹配,直到一个匹配出现。

The behavior of this function template is equivalent to:

(仔细看下面的代码就能看出来)

1
2
3
4
5
6
7
8
9
10
11
12
13
template<class InputIterator, class ForwardIterator>
  InputIterator find_first_of ( InputIterator first1, InputIterator last1,
                                ForwardIterator first2, ForwardIterator last2)
{
  while (first1!=last1) {
    for (ForwardIterator it=first2; it!=last2; ++it) {
      if (*it==*first1)          // or: if (pred(*it,*first)) for version (2)
        return first1;
    }
    ++first1;
  }
  return last1;
}



Parameters

first1, last1
Input iterators to the initial and final positions of the searched sequence. The range used is [first1,last1), which contains all the elements between first1 and last1, including the element pointed by first1 but not the element pointed by last1.

first2, last2

Forward iterators to the initial and final positions of the element values to be searched for. The range used is[first2,last2).
For (1), the elements in both ranges shall be of types comparable using operator== (with the elements of the first range as left-hand side operands, and those of the second as right-hand side operands).
pred
Binary function that accepts two elements as arguments (one of each of the two sequences, in the same order), and returns a value convertible to bool. The value returned indicates whether the elements are considered to match in the context of this function.
The function shall not modify any of its arguments.
This can either be a function pointer or a function object.

Return value

An iterator to the first element in [first1,last1) that is part of [first2,last2).
If no matches are found, the function returns last1.
If [first2,last2) is an empty range, the result is unspecified.

返回一个迭代器,指向范围[first1,last1)中第一个匹配到[first2,last1)中的任一(并不要求全部匹配)元素。如果没有匹配的,则返回last1.


Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// find_first_of example
#include <iostream>     // std::cout
#include <algorithm>    // std::find_first_of
#include <vector>       // std::vector
#include <cctype>       // std::tolower

bool comp_case_insensitive (char c1, char c2) {
  return (std::tolower(c1)==std::tolower(c2));
}

int main () {
  int mychars[] = {'a','b','c','A','B','C'};
  std::vector<char> haystack (mychars,mychars+6);
  std::vector<char>::iterator it;

  int needle[] = {'A','B','C'};

  // using default comparison:
  it = find_first_of (haystack.begin(), haystack.end(), needle, needle+3);

  if (it!=haystack.end())
    std::cout << "The first match is: " << *it << '\n';

  // using predicate comparison:
  it = find_first_of (haystack.begin(), haystack.end(),
                      needle, needle+3, comp_case_insensitive);

  if (it!=haystack.end())
    std::cout << "The first match is: " << *it << '\n';

  return 0;
}


Output:
The first match is: A
The first match is: a

Complexity

Up to linear in count1*count2 (where countX is the distance between firstX and lastX): Compares elements until a match is found.

Data races

Some (or all) of the objects in both ranges are accessed (once at most in the case of [first1,last1), and possibly more than once in [first2,last2)).

Exceptions

Throws if any element comparison (or pred) throws or if any of the operations on iterators throws.
Note that invalid arguments cause undefined behavior.



——————————————————————————————————————————————————————————————————

//写的错误或者不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。

转载请注明出处:http://blog.csdn.net/qq844352155

author:天下无双

Email:coderguang@gmail.com

2014-9-12

于GDUT

——————————————————————————————————————————————————————————————————



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值