C++语法基础--泛型算法(generic algorithm)--只读算法accumulate(),find_first_of(),count(),count_if()

      注:泛型算法实在太多,太繁杂,全都掌握也不现实,也没有必要。所以暂时只研究一些常用的算法。 



  *accumulate(在numeric头文件中定义)
   Computes the sum of the given value init and the elements in the range [first, last). 
   The first version uses operator+ to sum up the elements, the second version uses the given binary function op.
  原型:
    template
   T accumulate ( InputIterator first, InputIterator last, T init );
template
   T accumulate ( InputIterator first, InputIterator last, T init,
                  BinaryOperation binary_op )

  解析:
  first,last:指定要累加的范围
  init:指定初始值
  binary_op:累加的方法

  
  该算法等效于:
  template
   T accumulate ( InputIterator first, InputIterator last, T init )
{
  while ( first!=last )
    init = init + *first++;  // or: init=binary_op(init,*first++) for the binary_op version
  return init;
}



  Examples:
   int main () {
  int init = 10;
  int numbers[] = {1,2,3};


  //"using default accumulate: 
  cout << accumulate(numbers,numbers+3,init);
//16
  cout << endl;


  //using custom function:
  cout << accumulate (numbers, numbers+3, init, myfunction );//13
  cout << endl;


 
  return 0;
}


 
 *find_first_of():
  *元素可以存储在不同类型的容器中,只要求元素之间可以比较即可
  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.
  原型:
  equality :
          template
          ForwardIterator1 find_first_of (ForwardIterator1 first1, ForwardIterator1 last1,
                                   ForwardIterator2 first2, ForwardIterator2 last2);
 predicate :
         template
         ForwardIterator1 find_first_of (ForwardIterator1 first1, ForwardIterator1 last1,
                                   ForwardIterator2 first2, ForwardIterator2 last2,
                                   BinaryPredicate pred)

  该算法等效于
  template
  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,*first1)) for version (2)
        return first1;
    }
    ++first1;
  }
  return last1;
}





  Examples:
 //判断元素是否相等
 bool  equal(int a, int b)
 {
  return a==(b-1);
 }


int main () 
{
  int  arr1[] = {1,2,3,4,5};
  vector vec1 (arr1,arr1+5);
  vector::iterator it;


  int arr2[] = {2,4,6};


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


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


  // using predicate comparison:
  it = find_first_of (vec1.begin(), vec1.end(),
 arr2, arr2+3, equal);//比较时,equal函数令arr2的每一个元素都减1


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


  return 0;
}




*count()
  Returns the number of elements in the range [first,last) that compare equal to val.
  The function uses operator== to compare the individual elements to val.
 
原型:
template
  typename iterator_traits::difference_type
    count (InputIterator first, InputIterator last, const T& val)

 
该算法等效于:
  template
  typename iterator_traits::difference_type
    count (InputIterator first, InputIterator last, const T& val)
{
  typename iterator_traits::difference_type ret = 0;
  while (first!=last) {
    if (*first == val) ++ret;
    ++first;
  }
  return ret;
}



 Examples:

int main ()

 {

  // counting elements in array:
  int myints[] = {1,1,3,2,2,3,1,3};   // 8 elements
  int mycount = count (myints, myints+8, 1);
  cout << "1 appears " << mycount << " times.\n";//1 appears 3 times.


  // counting elements in container:
  std::vector myvector (myints, myints+8);
  mycount = count (myvector.begin(), myvector.end(), 2);
  cout << "2 appears " << mycount  << " times.\n";//2 appears 2 times.


  return 0;
}



 *count_if
    Return number of elements in range satisfying condition
   Returns the number of elements in the range [first,last) for which pred is true.

 原型:
 template

  typename iterator_traits::difference_type
    count_if (InputIterator first, InputIterator last, UnaryPredicate pred)

 该算法等效于:
 
template
  typename iterator_traits::difference_type
    count (InputIterator first, InputIterator last, UnaryPredicate pred)
{
  typename iterator_traits::difference_type ret = 0;
  while (first!=last) {
    if (pred(*first)) ++ret;
    ++first;
  }
  return ret;
}

 解析:
 
first, last:
         InputIterator to the initial and final positions of the sequence of elements.
pred      :
         Unary function that accepts an element in the range as argument, and returns a value convertible to bool

Example:
 bool IsEven (int i) { return ((i%2)==0); }

int main ()
{
  vector myvector;
  for (int i=1; i<10; i++) 
  {
 myvector.push_back(i);// myvector: 1 2 3 4 5 6 7 8 9
  }


    int mycount = count_if (myvector.begin(), myvector.end(), IsEven);
  cout << "myvector contains " << mycount  << " Even values.\n";
  //myvector contains 4 Even values.


     return 0;
}






 

 

你可能感兴趣的:(c++语法基础总结笔记)