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()".