STL algorithm算法count(10)

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

std::count

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

返回first,last之间和元素val相等的元素的个数。

例子:

#include <iostream>
#include <algorithm>
#include <vector>
#include <array>
using namespace std;
void count1(){
    vector<int> vi{1,5,7,8,9,9,8,5,9};
    array<int,7> ai{888,666,555,222,111,555,777};
    cout<<"vi=";
    for(int &i:vi)
        cout<<i<<" ";
    cout<<endl;

    int cv=count(vi.begin(),vi.end(),9);
    cout<<"9 在vi中出现了"<<cv<<"次"<<endl;

    cout<<"ai=";
    for(int &i:ai)
        cout<<i<<" ";
    cout<<endl;
    int ca=count(ai.begin(),ai.end(),555);
    cout<<"555 在ai中出现了"<<ca<<"次"<<endl;




}
运行结果:




The function uses operator== to compare the individual elements to val.

该函数使用operator==来比较各个元素和val是否相等。


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



Parameters

first, last
Input iterators to the initial and final positions of the sequence of elements. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
比较的范围。
val
Value to match.
T shall be a type supporting comparisons with the elements pointed by InputIterator using operator== (with the elements as left-hand side operands, and val as right-hand side).
要比较的值。

Return value

The number of elements in the range [first,last) that compare equal to val.
The return type (iterator_traits<InputIterator>::difference_type) is a signed integral type.

匹配的元素的个数。


Example

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 
// count algorithm example #include <iostream> // std::cout #include <algorithm> // std::count #include <vector> // std::vector int main () { // counting elements in array: int myints[] = {10,20,30,30,20,10,10,20}; // 8 elements int mycount = std::count (myints, myints+8, 10); std::cout << "10 appears " << mycount << " times.\n"; // counting elements in container: std::vector<int> myvector (myints, myints+8); mycount = std::count (myvector.begin(), myvector.end(), 20); std::cout << "20 appears " << mycount << " times.\n"; return 0; }
Edit & Run


Output:
10 appears 3 times. 20 appears 3 times. 

Complexity

Linear in the distance between first and last: Compares once each element.

Data races

The objects in the range [first,last) are accessed (each object is accessed exactly once).

Exceptions

Throws if either an element comparison or an operation on an iterator throws.

Note that invalid arguments cause undefined behavior.



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

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

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

author:天下无双

Email:[email protected]

2014-9-11

于GDUT

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







你可能感兴趣的:(Algorithm,算法,STL)