Using count_if with stl Map container class

Posted on 2007-10-12
Last Modified: 2008-01-09

I'm quite new to C++ and and could not find anything on the net via Google that can help me answer this question.

Suppose I have a map container declared as:  map<int, state>
where state is an enum of three possible values.  

I was wondering if I can use count_if to return the total number count for all pairs in the map where state = SOME_VALUE

Of course, I can create a trivial function to count this myself, but wanted to use a pre-existing algorithm from STL.


- Dan
Question by:dkf360
    LVL 15

    Accepted Solution

    Yes, you can do that.

    You will have to pass count_if an iterator to the beginning of the map, an iterator to the end of the map, and a predicate.  You can get the iterators from the map's begin() and end() functions.

    The predicate is more tricky.  count_if has to be able to call it like a function, with a map element as a parameter and a bool return value.  So if you only wanted to count the elements with some one value you could hard-code, you could just code that value in a function, like this:

    bool predicate(const pair<int, state>& inputPair)
        return inputPair.second == SOME_VALUE;

    With this definition, you would just pass "predicate" (without the quote marks) as the third parameter to count_if.

    If you need the value checked to be variable at run-time, it's more complicated, but still possible.  You can pass a function object that has the value stored in it at run_time.  For example:

    struct statePredicate
          //  The constructor saves the value to be checked.
          statePredicate(state inputState) : m_state(inputState) {}
          //  The operator() function lets an object of this class be called like a function.
          bool operator() (const pair<int, state>& pr)
                //  Report whether the map element has the value previously passed to the constructor.
                return pr.second == m_state;
          state m_state;

    In this case, as the third parameter to count_if, you would pass a temporary statePredicate object constructed with the value you wanted counted.  For example:

    size_type count = count_if(amap.begin(), amap.end(), statePredicate(SOME_VALUE));


    Author Comment

    Thanks.  It was exactly what I needed to know.
    LVL 53

    Expert Comment

    And for your reference, take a look at this for the STL map :


    and more specifically, for count_if :


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

      Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
    This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now