boost::count

提供: C++入門
移動: 案内検索
スポンサーリンク


読み方

boost::count
ぶーすと かうんと

概要

std::vector の中に、ある値が何個存在するか確認する方法として、boost::countが利用できます。

std::vectorの値を数える例

ソースコード boost_count_vector.cpp

#include <iostream>
#include <boost/foreach.hpp>
#include <vector>
 
#include <boost/range/algorithm.hpp>
 
using namespace std;
 
void
dump(vector<int>& v)
{
        BOOST_FOREACH(int x, v) {
                cout << x << endl;
        }
}
 
int
main (int argc, char *argv[])
{
        vector<int> v;
 
        v.push_back ( 3 );
        v.push_back ( 4 );
        v.push_back ( 1 );
        v.push_back ( 2 );
        v.push_back ( 1 );
        v.push_back ( 1 );
        v.push_back ( 2 );
 
        cout << "Dump vector" << std::endl;
        dump (v);
 
        int find1 = boost::count(v, 1);
        int find2 = boost::count(v, 2);
 
        cout << "Found 1: " << find1 << std::endl;
        cout << "Found 2: " << find2 << std::endl;
        return 0;
}

コンパイル

g++  -I/usr/local/include boost_count_vector.cpp -o boost_count_vector

実行例

% ./boost_count_vector
Dump vector
3
4
1
2
1
1
2
Found 1: 3
Found 2: 2


関連項目




スポンサーリンク