Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting number of same values in map

Tags:

c++

count

map

is there any command that counts the number of same values in a map?

like:

map<int, string> m;
m[1] = "A";
m[22] = "A";
m[53] = "C";
m[12] = "A";
m[6] = "A";

int count = m.count("A");// 4

or should i just write it myself, since its not too hard?

like image 544
calccrypto Avatar asked Apr 01 '11 18:04

calccrypto


People also ask

How count duplicate values in HashMap?

For example, If put("001", "DM"); into the hash map and put("010", "DM"); as well, how can count if there are two values int the ArrayList section of the Hashmap. For example, the output would look something like this: DM:2 as I 'put' two DM values into the Hashmap. You can't put("001", "DM"); .

Can Map contain duplicates?

Duplicate keys are not allowed in a Map. Basically, Map Interface has two implementation classes HashMap and TreeMap the main difference is TreeMap maintains an order of the objects but HashMap will not. HashMap allows null values and null keys. Both HashSet and HashMap are not synchronized.

Can Map have multiple values for same key?

The Map interface stores the elements as key-value pairs. It does not allow duplicate keys but allows duplicate values. HashMap and LinkedHashMap classes are the widely used implementations of the Map interface. But the limitation of the Map interface is that multiple values cannot be stored against a single key.

What will happen if we put two values with the same key in Map?

However, none of the existing Java core Map implementations allow a Map to handle multiple values for a single key. As we can see, if we try to insert two values for the same key, the second value will be stored, while the first one will be dropped.


1 Answers

You can use the count_if algorithm with a custom predicate function object:

template <typename Pair>
struct second_equal_to
    : std::unary_function<const Pair&, bool>
{
    second_equal_to(const typename Pair::second_type& value)
        : value_(value) { }

    bool operator()(const Pair& p) const
    {
        return p.second == *value_;
    }

private:
    typename Pair::second_type value_;
};

Usage:

typedef std::map<int, std::string> Map;
typedef Map::value_type MapEntry;
std::count_if(m.begin(), m.end(), second_equal_to<MapEntry>("A"));

Or, for a more generic solution, you can write an apply_to_second predicate transformer:

template <typename Pair, typename Predicate>
struct apply_to_second_f
    : std::unary_function<const Pair&, bool>
{
    apply_to_second_f(const Predicate& p)
        : predicate_(p) { }

    bool operator()(const Pair& p) const
    {
        return predicate_(p.second);
    }

    Predicate predicate_;
};

template <typename Pair, typename Predicate>
apply_to_second_f<Pair, Predicate> apply_to_second(const Predicate& p)
{
    return apply_to_second_f<Pair, Predicate>(p);
}

Usage:

std::count_if(m.begin(), m.end(), 
    apply_to_second<MapEntry>(std::bind2nd(std::equal_to<std::string>(), "A")));

If you have a compiler that supports lambda expressions, you don't need any custom predicate functor at all; you can use a much simpler lambda:

std::count_if(m.begin(), m.end(), [](const MapEntry& e) { 
    return e.second == "A";
});
like image 109
James McNellis Avatar answered Oct 10 '22 10:10

James McNellis