【STL】标准库中count与count_if函数说明与例子

count_if函数原型如下:

template<class _InIt, class _Pr> inline
    typename iterator_traits<_InIt>::difference_type
        count_if(_InIt _First, _InIt _Last, _Pr _Pred);
前两个参数是iterator(迭代器),表示查找 半闭合区间 的前后两个位置,第三个参数为一个用户定义的predicate function object,而 predicate意思就是说是一个返回值是bool型的仿函数(function object,也称functor)。

count函数原型如下:

template<class InputIterator, class T> inline
   size_t count(
      InputIterator First,
      InputIterator Last,
      const T& Value
   )
前两个参数如上,只是第三个参数是一个T类型的常量,相比之下,count_if具有更大的灵活性。

count_if例子:

// countif.cpp
// compile with: /EHsc
//
// Functions:
//
//   count_if  - Count items in a range that satisfy a predicate.
//
//   begin     - Returns an iterator that points to the first element in
//               a sequence.
//
//   end       - Returns an iterator that points one past the end of a
//               sequence.

// disable warning C4786: symbol greater than 255 character,
// okay to ignore
#pragma warning(disable: 4786)

#include <iostream>
#include <algorithm>
#include <functional>
#include <string>
#include <vector>

using namespace std;


// Return true if string str starts with letter 'S'
int MatchFirstChar( const string& str)
{
    string s("S") ;
    return s == str.substr(0,1) ;
}

int main()
{
    const int VECTOR_SIZE = 8 ;

    // Define a template class vector of strings
    typedef vector<string > StringVector ;

    //Define an iterator for template class vector of strings
    typedef StringVector::iterator StringVectorIt ;

    StringVector NamesVect(VECTOR_SIZE) ;   //vector containing names

    StringVectorIt start, end, it ;

    ptrdiff_t result = 0 ;   // stores count of elements
                       // that match value.

    // Initialize vector NamesVect
    NamesVect[0] = "She" ;
    NamesVect[1] = "Sells" ;
    NamesVect[2] = "Sea" ;
    NamesVect[3] = "Shells" ;
    NamesVect[4] = "by" ;
    NamesVect[5] = "the" ;
    NamesVect[6] = "Sea" ;
    NamesVect[7] = "Shore" ;

    start = NamesVect.begin() ;   // location of first
                                  // element of NamesVect

    end = NamesVect.end() ;       // one past the location
                                  // last element of NamesVect

    // print content of NamesVect
    cout << "NamesVect { " ;
    for(it = start; it != end; it++)
        cout << *it << " " ;
    cout << " }\n" << endl ;

    // Count the number of elements in the range [first, last +1)
    // that start with letter 'S'
    result = count_if(start, end, MatchFirstChar) ;

    // print the count of elements that start with letter 'S'
    cout << "Number of elements that start with letter \"S\" = "
        << result << endl  ;
}

count例子:

// count.cpp
// compile with: /EHsc
//
// Functions:
//
//    count  - Count items in a range that match a value.

// disable warning C4786: symbol greater than 255 character,
// okay to ignore
#pragma warning(disable: 4786)

#include <iostream>
#include <algorithm>
#include <functional>
#include <string>
#include <vector>

using namespace std;


int main()
{
    const int VECTOR_SIZE = 8 ;

    // Define a template class vector of strings
    typedef vector<string > StringVector ;

    //Define an iterator for template class vector of strings
    typedef StringVector::iterator StringVectorIt ;

    StringVector NamesVect(VECTOR_SIZE) ;   //vector containing names

    string value("Sea") ;  // stores the value used
                           // to count matching elements

    StringVectorIt start, end, it ;

    ptrdiff_t result = 0 ;   // stores count of elements
                       // that match value.

    // Initialize vector NamesVect
    NamesVect[0] = "She" ;
    NamesVect[1] = "Sells" ;
    NamesVect[2] = "Sea" ;
    NamesVect[3] = "Shells" ;
    NamesVect[4] = "by" ;
    NamesVect[5] = "the" ;
    NamesVect[6] = "Sea" ;
    NamesVect[7] = "Shore" ;

    start = NamesVect.begin() ;   // location of first
                                  // element of NamesVect

    end = NamesVect.end() ;       // one past the location
                                  // last element of NamesVect

    // print content of NamesVect
    cout << "NamesVect { " ;
    for(it = start; it != end; it++)
        cout << *it << " " ;
    cout << " }\n" << endl ;

    // Count the number of elements in the range [first, last +1)
    // that match value.
    result = count(start, end, value) ;

    // print the count of elements that match value
    cout << "Number of elements that match \"Sea\" = "
        << result << endl  ;
}

猜你喜欢

转载自blog.csdn.net/huangyimo/article/details/80347800