0

I'm trying to figure out a way to search for a key in a map, return it in a message, get the value of the key found, and return it in another message. For example, the class below has a list of fruits found in a grocery store and I want to create an if than else statement to find the fruitname in the map, return its name in the message below, and then return its price in another output. How can I do this?

`

#include <iostream>
#include <string>
#include <set>
#include <map>
#include<utility>
using namespace std;



int main()
{

map<string,double> items;
items["apples"] = 1.56;
items["oranges"] = 2.34;
items["bananas"] = 3.00; 
items["limes"] = 4.45;       
items["grapefruits"] = 6.00;    

string fruit = "apples";

//If a fruit is in map
cout << "Your fruit is: " << "(fruitname value here)"
    <<"\n";
    << "your price is: " <<"(fruitname price here)" 
    << "\n";
 // return the fruitname and its price





  return 0;
}

So far I've only seen examples that show how to print entire maps out. The closest I've seen is the one posted at this link(see second post) :see if there is a key in a map c++ ,but I'm confused by the syntax,in particular, "buf.c_str()".

Community
  • 1
  • 1
Wormhole99
  • 105
  • 1
  • 4
  • 9

3 Answers3

2

Since the keys to the map are std::string, you don't have to use .c_str(). You could pass the std::string object itself:

auto it = items.find(fruit); //don't pass fruit.c_str()
if ( it != items.end())
   std::cout << "value = " << it->second << std::endl;
else
   std::cout << ("key '" + fruit + "' not found in the map") << std::endl;
Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • This technique is preferred over `operator[]`. The `operator[]` has the side effect of inserting the element if the element doesn't exist. – Thomas Matthews Feb 22 '13 at 20:38
  • @ThomasMatthews: It is not only preferred over `operator[]`, it is correct. Using `operator[]` to *find* element would be wrong! – Nawaz Feb 22 '13 at 20:39
  • FYI, I'm talking about the case of `value = items["orange"];`. – Thomas Matthews Feb 22 '13 at 20:43
  • @ThomasMatthews: That would be incorrect. So it is not about *preference* as such. – Nawaz Feb 22 '13 at 20:44
  • @Nawaz since each poster gave similar answer, I'm not quite sure who to give a green check mark to. Each one of them worked when tested but why is "auto" perferred over an iterator like the example that amaurea used? – Wormhole99 Feb 23 '13 at 01:07
  • @Wormhole99: Figure it out yourself : compare the solutions with and without `auto`. Do you see any difference? – Nawaz Feb 23 '13 at 02:53
  • @Wormhole99: This use of `auto` is a relatively new feature of `C++`. It basically means "store `it` as whatever type `find` returns. That type happens to be a `map::iterator`. So they are using an iterator, they are just being less explicit about it. Avoiding long typenames like the one for the iterator above is the main purpose of the new meaning of `auto`. – amaurea Feb 26 '13 at 10:17
  • Small correction, the value is accessed via: `it->second` – doo_dah Jun 13 '22 at 08:25
1

Very easy:

auto it = items.find(fruit);

if (it != items.end())
{
    std::cout << "Your fruit is " << it->first << " at price " << it->second ".\n";
}
else
{ 
    std::cout << "No fruit '" << fruit << "' exists.\n";
}
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
1

Use maps find member function.

map<string,double>::const_iterator i = items.find(fruit);
if(i != items.end())
    cout << "Your fruit is: " << i->first << "\n" << "your price is: " << i->second << "\n";
amaurea
  • 4,950
  • 26
  • 35
  • When I tried testing this function I got this error:"no match for operator!=" its refering to "if(fruit !=items.end()){". – Wormhole99 Feb 25 '13 at 18:43
  • @Wormhole88: Sorry, that should have been `i != items.end()`. I have updated the answer. – amaurea Feb 26 '13 at 10:13