STL_count() vs count_if()

//

01 STL 通用函数

    01 count( C.begin(),C.end(),value );
    02 count_if( C.begin(),C.end(),judge );

02 C.count( value ); // 除 vector 外的容器都有此函数

    !!! map 使用 count_if 时 judge 的参数类型是 pair< T1,T2 > // 和 cmpS 不同

// vector
#include<bits/stdc++.h>
using namespace std;

vector<int> v;

int main()
{
    int n;
    while( cin>>n )
    {
        v.clear();
        while( n-- ) v.push_back( n%7 );
        cout<<count( v.begin(),v.end(),0 )<<endl;
        // not v.count() !!!
    }
    return 0;
}

// map count()
#include<bits/stdc++.h>
using namespace std;

const double PI=acos(-1);
map< double,int > mp;

int main()
{
    int n,i;
    while( cin>>n )
    {
        for( i=0;i<n;i++ ) mp[(1<<i)%7*PI]++;
        cout<<mp.count( PI )<<endl;
    }
    return 0;
}

// map count_if()
#include<bits/stdc++.h>
using namespace std;

const double PI=acos(-1);
map< double,int > mp;
bool f( const pair< double,int >& x ) { return x.first > PI ; }

int main()
{
    int n,i;
    while( cin>>n )
    {
        for( i=0;i<n;i++ ) mp[(1<<i)%7*PI]++;
        cout<<count_if( mp.begin(),mp.end(),f )<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_63173957/article/details/124386403