map-count

////////////////////////////////////////
//      2018/05/01 14:14:57
//      map-count

// returns the number of elements

#include <iostream>
#include <map>
#include <list>
#include <numeric>

using namespace std;

int main(){
    list<int> L1(3), L2(3);
    iota(L1.begin(), L1.end(), 1);
    iota(L2.begin(), L2.end(), 4);

    typedef map<int, list<int>> M;
    M m;
    m.insert(M::value_type(1, L1));
    m.insert(M::value_type(2, L2));

    M::iterator it;
    list<int>::iterator Li;

    for (it = m.begin(); it != m.end(); it++){
        cout << "map " << it->first << ":";
        for (Li = it->second.begin(); Li != it->second.end(); Li++){
            cout << *Li << " ";
        }
        cout << endl;
    }

    int n = m.count(2);
    cout << "count of element with '2'(0 or 1) is "
        << n << endl;
    return 0;
}

/*
OUTPUT:
    map 1:1 2 3
    map 2:4 5 6
    count of element with '2'(0 or 1) is 1
*/ 
posted @ 2018-05-01 15:15  老耗子  阅读(82)  评论(0编辑  收藏  举报