C++的count和cout_if函数

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

count函数原型如下:
[cpp]  view plain  copy
 print ?
  1. template<class InputIterator, class T> inline  
  2.    size_t count(  
  3.       InputIterator First,  
  4.       InputIterator Last,  
  5.       const T& Value  
  6.    )  

前两个参数如上,只是第三个参数是一个T类型的常量,相比之下,count_if具有更大的灵活性。

count_if例子:

[cpp]  view plain  copy
 print ?
  1. // countif.cpp  
  2. // compile with: /EHsc  
  3. //  
  4. // Functions:  
  5. //  
  6. //   count_if  - Count items in a range that satisfy a predicate.  
  7. //  
  8. //   begin     - Returns an iterator that points to the first element in  
  9. //               a sequence.  
  10. //  
  11. //   end       - Returns an iterator that points one past the end of a  
  12. //               sequence.  
  13.   
  14. // disable warning C4786: symbol greater than 255 character,  
  15. // okay to ignore  
  16. #pragma warning(disable: 4786)  
  17.   
  18. #include <iostream>  
  19. #include <algorithm>  
  20. #include <functional>  
  21. #include <string>  
  22. #include <vector>  
  23.   
  24. using namespace std;  
  25.   
  26.   
  27. // Return true if string str starts with letter 'S'  
  28. int MatchFirstChar( const string& str)  
  29. {  
  30.     string s("S") ;  
  31.     return s == str.substr(0,1) ;   //返回字符串中从0开始的,长度为1的字符串
  32. }  
  33.   
  34. int main()  
  35. {  
  36.     const int VECTOR_SIZE = 8 ;  
  37.   
  38.     // Define a template class vector of strings  
  39.     typedef vector<string > StringVector ;  
  40.   
  41.     //Define an iterator for template class vector of strings  
  42.     typedef StringVector::iterator StringVectorIt ;  
  43.   
  44.     StringVector NamesVect(VECTOR_SIZE) ;   //vector containing names  
  45.   
  46.     StringVectorIt start, end, it ;  
  47.   
  48.     ptrdiff_t result = 0 ;   // stores count of elements  
  49.                        // that match value.  
  50.   
  51.     // Initialize vector NamesVect  
  52.     NamesVect[0] = "She" ;  
  53.     NamesVect[1] = "Sells" ;  
  54.     NamesVect[2] = "Sea" ;  
  55.     NamesVect[3] = "Shells" ;  
  56.     NamesVect[4] = "by" ;  
  57.     NamesVect[5] = "the" ;  
  58.     NamesVect[6] = "Sea" ;  
  59.     NamesVect[7] = "Shore" ;  
  60.   
  61.     start = NamesVect.begin() ;   // location of first  
  62.                                   // element of NamesVect  
  63.   
  64.     end = NamesVect.end() ;       // one past the location  
  65.                                   // last element of NamesVect  
  66.   
  67.     // print content of NamesVect  
  68.     cout << "NamesVect { " ;  
  69.     for(it = start; it != end; it++)  
  70.         cout << *it << " " ;  
  71.     cout << " }\n" << endl ;  
  72.   
  73.     // Count the number of elements in the range [first, last +1)  
  74.     // that start with letter 'S'  
  75.     result = count_if(start, end, MatchFirstChar) ;   //第三个参数叫做谓词
  76.   
  77.     // print the count of elements that start with letter 'S'  
  78.     cout << "Number of elements that start with letter \"S\" = "  
  79.         << result << endl  ;  
  80. }  
count例子:
[cpp]  view plain  copy
 print ?
  1. // count.cpp  
  2. // compile with: /EHsc  
  3. //  
  4. // Functions:  
  5. //  
  6. //    count  - Count items in a range that match a value.  
  7.   
  8. // disable warning C4786: symbol greater than 255 character,  
  9. // okay to ignore  
  10. #pragma warning(disable: 4786)  
  11.   
  12. #include <iostream>  
  13. #include <algorithm>  
  14. #include <functional>  
  15. #include <string>  
  16. #include <vector>  
  17.   
  18. using namespace std;  
  19.   
  20.   
  21. int main()  
  22. {  
  23.     const int VECTOR_SIZE = 8 ;  
  24.   
  25.     // Define a template class vector of strings  
  26.     typedef vector<string > StringVector ;  
  27.   
  28.     //Define an iterator for template class vector of strings  
  29.     typedef StringVector::iterator StringVectorIt ;  
  30.   
  31.     StringVector NamesVect(VECTOR_SIZE) ;   //vector containing names  
  32.   
  33.     string value("Sea") ;  // stores the value used  
  34.                            // to count matching elements  
  35.   
  36.     StringVectorIt start, end, it ;  
  37.   
  38.     ptrdiff_t result = 0 ;   // stores count of elements  
  39.                        // that match value.  
  40.   
  41.     // Initialize vector NamesVect  
  42.     NamesVect[0] = "She" ;  
  43.     NamesVect[1] = "Sells" ;  
  44.     NamesVect[2] = "Sea" ;  
  45.     NamesVect[3] = "Shells" ;  
  46.     NamesVect[4] = "by" ;  
  47.     NamesVect[5] = "the" ;  
  48.     NamesVect[6] = "Sea" ;  
  49.     NamesVect[7] = "Shore" ;  
  50.   
  51.     start = NamesVect.begin() ;   // location of first  
  52.                                   // element of NamesVect  
  53.   
  54.     end = NamesVect.end() ;       // one past the location  
  55.                                   // last element of NamesVect  
  56.   
  57.     // print content of NamesVect  
  58.     cout << "NamesVect { " ;  
  59.     for(it = start; it != end; it++)  
  60.         cout << *it << " " ;  
  61.     cout << " }\n" << endl ;  
  62.   
  63.     // Count the number of elements in the range [first, last +1)  
  64.     // that match value.  
  65.     result = count(start, end, value) ;  
  66.   
  67.     // print the count of elements that match value  
  68.     cout << "Number of elements that match \"Sea\" = "  
  69.         << result << endl  ;  
  70. }  


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值