-1

If you have a std::map which takes a string and an int.

    std::map<std::string, int> exampleMap;

Is there any way to print the string if you have the correct int?

Let's say I insert the string "hello" and the int 0 into my map:

    exampleMap.insert(std::make_pair("hello", 0));

To print 0, we can use:

    exampleMap.find('hello')->second;

Is there any way to print the "hello" string?

  • 1
    `exampleMap.find('hello')->first` maybe? – πάντα ῥεῖ May 14 '19 at 17:24
  • @πάντα ῥε Say what? –  May 14 '19 at 17:28
  • Let's say we only have ints to help us, we don't know the strings. Could you loop through a number of ints and see if they have a string attached to them? –  May 14 '19 at 17:29
  • you need to iterate to find the entries having the right 'second' to print the 'first' – bruno May 14 '19 at 17:31
  • If you are asking "could you iterate over a bunch of ints that are values in the map, and find out if they have matching keys?" then yes, you can. –  May 14 '19 at 17:31
  • [Boost bimap](https://www.boost.org/doc/libs/1_70_0/libs/bimap/doc/html/index.html). – Some programmer dude May 14 '19 at 17:32
  • [Boost.Bimap](https://www.boost.org/doc/libs/1_70_0/libs/bimap/doc/html/index.html) might be of interest. – Shawn May 14 '19 at 17:33
  • @m_c So you're basically asking about finding a specific key in the map by using the value? That's not supported by `std::map` directly, you have to find out in a loop iterating over all elements. There are other implemetations like [`boost::bimap`](https://www.boost.org/doc/libs/1_67_0/libs/bimap/doc/html/index.html) though. – πάντα ῥεῖ May 14 '19 at 17:34

3 Answers3

2

You can sequentially loop through all the entries in the map until you find the one with the value:

for (auto &it : exampleMap)
{
    if (it.second == value)
        return it.first;
}

Although you need to decide what to do if there are multiple entries with the same int value and also what to do if the int is not found.

user11499890
  • 131
  • 5
1

Let's say I insert the string "hello" and the int 0 into my map:

 exampleMap.insert(std::make_pair("hello", 0));

Is there any way to print the "hello" string?

you need to iterate to find the entries having the expected value to print the key

#include <map>
#include <string>
#include <iostream>

int main()
{
  std::map<std::string, int> exampleMap;

  exampleMap.insert(std::make_pair("hello", 0));
  exampleMap.insert(std::make_pair("how", 1));
  exampleMap.insert(std::make_pair("are", 2));
  exampleMap.insert(std::make_pair("you", 0));

  int expected = 0;

  for (std::map<std::string, int>::const_iterator it = exampleMap.begin();
       it != exampleMap.end();
       ++it) {
    if (it->second == expected)
      std::cout << it->first << std::endl;
  }
}

Compilation and execution :

pi@raspberrypi:/tmp $ g++ -pedantic -Wall -Wextra m.cc
pi@raspberrypi:/tmp $ ./a.out
hello
you
pi@raspberrypi:/tmp $ 
bruno
  • 32,421
  • 7
  • 25
  • 37
1

You have a few choices...

  1. Create two maps (one going from string to int and one going the other way)
  2. You can use a "Bi-Directional" map, that will let you go both ways. Boost also has an implementation.
  3. You can use a function like this one to convert from one map to another map.

Which one you choose depends on how often you need to do both lookups. If it is rarely, I'd use the conversion function. If it isn't rare, I would use the bi-directional container.

Darrin Cullop
  • 1,170
  • 8
  • 14
  • 1
    The container has to enforce that, which isn't possible if you use Option 1. Option 1 isn't as good as an option as the others. – Darrin Cullop May 14 '19 at 17:39
  • 1
    Let me reiterate the question, where from OP's question you got the information that values are unique and you can even enforce that? – Slava May 14 '19 at 17:41