简体   繁体   中英

check if value already exists in multimap c++

How do i find out that a Multimap already contains a specific value, and how to find the total number of items containing same key?

std::multimap<float,int> obj;
obj.insert ( std::pair<char,int>('a',100) );
obj.insert ( std::pair<char,int>('a',100) );

For example if i want to check if given multimap obj contains 100 value already how do i check it and how to get that number of items containing the character a is 2?

This is a short code that answers both of your questions:

#include <iostream>
#include <map>

int main()
{
    // The map
    std::multimap<char,int> obj;
    obj.insert ( std::pair<char,int>('a',100) );
    obj.insert ( std::pair<char,int>('a',100) );
    obj.insert ( std::pair<char,int>('b',100) );
    obj.insert ( std::pair<char,int>('b',200) );
    obj.insert ( std::pair<char,int>('b',300) );

    // Count occurrences of a key
    std::cout << "a: " << obj.count('a') << "\nb: " << obj.count('b')
              << "\nc: " << obj.count('c') << "\n";

    // Count occurrences of a value
    int val = 100, count = 0;
    for (const auto& entry : obj)
        if (entry.second == val)
            count++;

    std::cout << "Value " << val << " occurred " << count << " times." << std::endl;
}

I made the map a little bigger for demonstration, also - your initial multimap type was not correct - you need to make it <char, int> instead of <float, int> - it compiled and "worked" because of conversions but you definitely don't want that in your program.

The part that counts occurrence of a key uses a multimap function count that literally counts occurrences of a given key in the multimap.

You can look up all available functions for a given container and see which one works for you, if any. I use the https://en.cppreference.com/w/ website which I prefer over the http://www.cplusplus.com/ one. So for a multimap on that website you get a list of everything that is directly related to a multimap. One of the benefits of looking up is that you'll also see new options in newest standards. Finally, this website often has a built in online compiler/code section in the documentation part that you can run and edit (and run under different compilers and standards). This gives an opportunity to better understand the functions and to quickly check if any variations you came up with work.

The second part counts occurrence of a given value in the map - this has to be done "manually" by checking every multimap entry and its value. There surely are other ways but this to me is the simplest. It uses a more recommended range for loop with constant references (since the entries it is looping through are not supposed to change).

This checks for only one fixed value, but you can get the occurrences of all values by creating a regular map with keys being values from your multimap and values being their counts. This is not difficult and will be a good exercise.

I think, you could count only std::pair('a',100) items. See http://www.cplusplus.com/reference/map/multimap/count/ .

If you want to count just 'a' or 100, you shold manually iterate all multimap items.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM