Non-modifying sequence operations

function template

std::for_each

<algorithm>
template <class InputIterator, class Function>
   Function for_each (InputIterator first, InputIterator last, Function f);
Apply function to range
Applies function  f  to each of the elements in the range  [first,last) .

The behavior of this template function is equivalent to:
1
2
3
4
5
6
template<class InputIterator, class Function>
  Function for_each(InputIterator first, InputIterator last, Function f)
  {
    for ( ; first!=last; ++first ) f(*first);
    return f;
  }

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
// for_each example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

void myfunction (int i) {
  cout << " " << i;
}

struct myclass {
  void operator() (int i) {cout << " " << i;}
} myobject;

int main () {
  vector<int> myvector;
  myvector.push_back(10);
  myvector.push_back(20);
  myvector.push_back(30);

  cout << "myvector contains:";
  for_each (myvector.begin(), myvector.end(), myfunction);

  // or:
  cout << "\nmyvector contains:";
  for_each (myvector.begin(), myvector.end(), myobject);

  cout << endl;

  return 0;
}


Output:
myvector contains: 10 20 30
myvector contains: 10 20 30

std::find

<algorithm>
template <class InputIterator, class T>
   InputIterator find ( InputIterator first, InputIterator last, const T& value );
Find value in range
Returns an iterator to the first element in the range  [first,last)  that compares equal to  value , or  last  if not found.

The behavior of this function template is equivalent to:
1
2
3
4
5
6
template<class InputIterator, class T>
  InputIterator find ( InputIterator first, InputIterator last, const T& value )
  {
    for ( ;first!=last; first++) if ( *first==value ) break;
    return first;
  }

std::find_if

<algorithm>
template <class InputIterator, class Predicate>
   InputIterator find_if ( InputIterator first, InputIterator last, Predicate pred );
Find element in range
Returns an iterator to the first element in the range  [first,last)  for which applying  pred  to it, is true.

The behavior of this function template is equivalent to:
1
2
3
4
5
6
template<class InputIterator, class Predicate>
  InputIterator find_if ( InputIterator first, InputIterator last, Predicate pred )
  {
    for ( ; first!=last ; first++ ) if ( pred(*first) ) break;
    return first;
  }

std::find_end

<algorithm>
template <class ForwardIterator1, class ForwardIterator2>
   ForwardIterator1 find_end ( ForwardIterator1 first1, ForwardIterator1 last1,
                               ForwardIterator2 first2, ForwardIterator2 last2 );

template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
   ForwardIterator1 find_end ( ForwardIterator1 first1, ForwardIterator1 last1,
                               ForwardIterator2 first2, ForwardIterator2 last2,
                               BinaryPredicate pred );
Find last subsequence in range
Searches the range  [first1,last1)  for the last occurrence of the sequence defined by  [first2,last2) , and returns an iterator to its first element.

The sequence of elements in  [first2,last2)  is compared to the possible subsequences of successive elements within  [first1,last1)  by either applying the  ==  comparison operator to each element, or the template parameter comp  (for the second version).

The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
template<class ForwardIterator1, class ForwardIterator2>
  ForwardIterator1 find_end ( ForwardIterator1 first1, ForwardIterator1 last1,
                              ForwardIterator2 first2, ForwardIterator2 last2)
{
  if (first2==last2) return last1;  // specified in C++11

  ForwardIterator1 ret = last1;

  while (first1!=last1)
  {
    ForwardIterator1 it1 = first1;
    ForwardIterator2 it2 = first2;
    while (*it1==*it2) {    // or: while (pred(*it1,*it2)) for the pred version
        ++it1; ++it2;
        if (it2==last2) { ret=first1; break; }
        if (it1==last1) return ret;
    }
    ++first1;
  }
  return ret;
}

std::find_first_of

<algorithm>
template <class ForwardIterator1, class ForwardIterator2>
   ForwardIterator1 find_first_of ( ForwardIterator1 first1, ForwardIterator1 last1,
                                    ForwardIterator2 first2, ForwardIterator2 last2 );

template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
   ForwardIterator1 find_first_of ( ForwardIterator1 first1, ForwardIterator1 last1,
                                    ForwardIterator2 first2, ForwardIterator2 last2,
                                    BinaryPredicate pred );
Find element from set in range
Returns an interator to the first occurrence in the range  [first1,last1)  of any of the elements in  [first2,last2) .

All the elements in  [first2,last2)  are compared to the each of the values in  [first,last)  until a match is found. The comparison is performed by either applying the  ==  operator, or the template parameter  comp  (for the second version).

The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
9
10
template<class ForwardIterator1, class ForwardIterator2>
  ForwardIterator1 find_first_of ( ForwardIterator1 first1, ForwardIterator1 last1,
                                   ForwardIterator2 first2, ForwardIterator2 last2)
{
  for ( ; first1 != last1; ++first1 )
    for (ForwardIterator2 it=first2; it!=last2; ++it)
      if (*it==*first1)          // or: if (comp(*it,*first)) for the pred version
        return first1;
  return 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
33
// find_first_of example
#include <iostream>
#include <algorithm>
#include <cctype>
#include <vector>
using namespace std;

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

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

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

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

  if (it!=myvector.end())
    cout << "first match is: " << *it << endl;

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

  if (it!=myvector.end())
    cout << "first match is: " << *it << endl;
  
  return 0;
}


Output:
First match is: A
First match is: a

std::count

<algorithm>
template <class InputIterator, class T>
  typename iterator_traits<InputIterator>::difference_type
    count ( ForwardIterator first, ForwardIterator last, const T& value );
Count appearances of value in range
Returns the number of elements in the range  [first,last)  that compare equal to  value .

The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
template <class InputIterator, class T>
  ptrdiff_t count ( InputIterator first, InputIterator last, const T& value )
{
  ptrdiff_t ret=0;
  while (first != last) if (*first++ == value) ++ret;
  return ret;
}

std::count_if

<algorithm>
template <class InputIterator, class Predicate>
  typename iterator_traits<InputIterator>::difference_type
    count_if ( ForwardIterator first, ForwardIterator last, Predicate pred );
Return number of elements in range satisfying condition
Returns the number of elements in the range  [first,last)  for which condition  pred  is true.

The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
template <class InputIterator, class Predicate>
  ptrdiff_t count_if ( InputIterator first, InputIterator last, Predicate pred )
{
  ptrdiff_t ret=0;
  while (first != last) if (pred(*first++)) ++ret;
  return ret;
}

std::mismatch

<algorithm>
template <class InputIterator1, class InputIterator2>
  pair<InputIterator1, InputIterator2>
    mismatch (InputIterator1 first1, InputIterator1 last1,
              InputIterator2 first2 );

template <class InputIterator1, class InputIterator2, class BinaryPredicate>
  pair<InputIterator1, InputIterator2>
    mismatch (InputIterator1 first1, InputIterator1 last1,
              InputIterator2 first2, BinaryPredicate pred );
Return first position where two ranges differ
Compares the elements in the range  [first1,last1)  against those in the range beginning at  first2  sequentially, and returns where the first mismatch happens.

The elements are compared by either applying the  ==  comparison operator to each pair of corresponding elements, or the template parameter  comp  (for the second version).

The function returns a  pair  of iterators to the first element in each range which differs in both sequences.

The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
template <class InputIterator1, class InputIterator2>
  pair<InputIterator1, InputIterator2>
    mismatch (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2 )
{
  while ( (first1!=last1) && (*first1==*first2) )  // or: pred(*first1,*first2), for the pred version
  { ++first1; ++first2; }
  return make_pair(first1,first2);
}

std::equal

<algorithm>
template <class InputIterator1, class InputIterator2>
  bool equal ( InputIterator1 first1, InputIterator1 last1,
               InputIterator2 first2 );

template <class InputIterator1, class InputIterator2, class BinaryPredicate>
  bool equal ( InputIterator1 first1, InputIterator1 last1,
               InputIterator2 first2, BinaryPredicate pred );
Test whether the elements in two ranges are equal
Compares the elements in the range  [first1,last1)  with those in the range beginning at  first2 , and returns  true  if the elements in both ranges are considered equal.

The elements are compared by either applying the  ==  comparison operator to each pair of corresponding elements, or the template parameter  pred  (for the second version).

The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
9
10
11
template <class InputIterator1, class InputIterator2>
  bool equal ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2 )
{
  while ( first1!=last1 )
  {
    if (!(*first1 == *first2))   // or: if (!pred(*first1,*first2)), for pred version
      return false;
    ++first1; ++first2;
  }
  return true;
}

std::search

<algorithm>
template <class ForwardIterator1, class ForwardIterator2>
   ForwardIterator1 search ( ForwardIterator1 first1, ForwardIterator1 last1,
                             ForwardIterator2 first2, ForwardIterator2 last2 );

template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
   ForwardIterator1 search ( ForwardIterator1 first1, ForwardIterator1 last1,
                             ForwardIterator2 first2, ForwardIterator2 last2.
                             BinaryPredicate pred );
Find subsequence in range
Searches the range  [first1,last1)  for the first occurrence of the sequence defined by  [first2,last2) , and returns an iterator to its first element.

The sequence of elements in  [first2,last2)  is compared to the possible subsequences of successive elements within  [first1,last1)  by either applying the  ==  comparison operator or the template parameter  pred  (for the second version).

The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
template<class ForwardIterator1, class ForwardIterator2>
  ForwardIterator1 search ( ForwardIterator1 first1, ForwardIterator1 last1,
                            ForwardIterator2 first2, ForwardIterator2 last2)
{
  if (first2==last2) return first1;  // specified in C++11
  
  while (first1!=last1)
  {
    ForwardIterator1 it1 = first1;
    ForwardIterator2 it2 = first2;
    while (*it1==*it2) {    // or: while (pred(*it1,*it2)) for the pred version
        ++it1; ++it2;
        if (it2==last2) return first1;
        if (it1==last1) return last1;
    }
    ++first1;
  }
  return last1;
}

std::search_n

<algorithm>
template <class ForwardIterator, class Size, class T>
   ForwardIterator search_n ( ForwardIterator first, ForwardIterator last,
                              Size count, const T& value );

template <class ForwardIterator, class Size, class T, class BinaryPredicate>
   ForwardIterator search_n ( ForwardIterator first, ForwardIterator last,
                              Size count, const T& value, BinaryPredicate pred );
Find succession of equal values in range
Searches the range  [first,last)  for a succession of  count  elements, each of them comparing equal (or some other predicate) to  value .

In the first version, the comparison is performed by applying the  ==  comparison operator between the elements being iterated and  value . In the second version,  pred  is applied to these, and a return value of  true  (or non-zero) is interpreted as an equivalence.

The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
template<class ForwardIterator, class Size, class T>
  ForwardIterator search_n ( ForwardIterator first, ForwardIterator last,
                             Size count, const T& value )
{
  ForwardIterator it, limit;
  Size i;

  limit=first; advance(limit,distance(first,last)-count);

  while (first!=limit)
  {
    it = first; i=0;
    while (*it==value)       // or: while (pred(*it,value)) for the pred version
      { ++it; if (++i==count) return first; }
    ++first;
  }
  return last;
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值