-1

Let's say I'm having a map of integer key value!

map<int,string>a={{1,"one"},{2,"two"}}; 

How can I print a whole pair by just supplying a key value?

Let's say user entered 1; I want my code to print 1,one then,How can i do so?

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Manav Roy
  • 33
  • 6
  • 4
    Hint: You already know the key, print the value. – tadman Oct 23 '22 at 13:03
  • Check if the key value can be found in the map, and if so just do `cout << key << ", " << a[key] << endl;` – πάντα ῥεῖ Oct 23 '22 at 13:05
  • Actually,Let me explain my full task, I've a map and user has to supply a key value,If that key value is present in that map,Then I've add that whole "pair" in different map,Is it possible to do so? I mean additing a whole pair – Manav Roy Oct 23 '22 at 13:14
  • adding a whole pair from one map to another? – Manav Roy Oct 23 '22 at 13:15
  • 1
    @ManavRoy If you want to supply updates to the question, [edit the question](https://stackoverflow.com/help/privileges/edit) instead of writing them as comments. – kotatsuyaki Oct 23 '22 at 13:16

1 Answers1

1

I've a map and user has to supply a key value,If that key value is present in that map,Then I've add that whole "pair" in different map, Is it possible to do so?

Yes this is possible. You can use std::map::find to check if your input map contains a given key and then std::map::insert to add the key value pair into the output map as shown below:

int main()
{
    std::map<int,string> searchMap={{1,"one"},{2,"two"}}; //map in which key is to be searched 
    
    std::map<int, string> outputMap;
    
    int input =0; 
    if(std::cin >> input); //take input from user 
    {
        //search the map for the input key 
        auto findInput = searchMap.find(input);
        if(findInput!=searchMap.end())//check if the input was found inside the map 
        {
            std::cout << "The map contains the key: " << input << " entered by user" << std::endl;
            //add the key value pair to the outputmap 
            outputMap.insert({input,findInput->second});  //or just outputmap[input] = findInput->second
        }
        else 
        {
            std::cout <<"The map does not contain the key: " << input << " entered by user" << std::endl;
        }
    }
    
    //lets confirm that outputmap was filled with input 
    for(const auto&[key, value]:outputMap)
    {
        std::cout << key << "->" << value << std::endl;
    }
    
}

Working demo

Jason
  • 36,170
  • 5
  • 26
  • 60
  • @ManavRoy You're welcome :) If I may, i would also recommend using any of the books listed [here](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). They're also available as PDFs for free. – Jason Oct 23 '22 at 13:49
  • 1
    Sure,Thanks for the reccomadation:)) But i prefer yt tutorials and website's more as they do not consume much time,XD – Manav Roy Oct 23 '22 at 13:51
  • 1
    @ManavRoy But keep in mind that this doesn't come for free - most of those tutorials and websites are created by non-professional programmers and teach how to write really bad and buggy code. – Evg Oct 23 '22 at 14:40