Can I implement a Default function in C++ Maps | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can I implement a Default function in C++ Maps

I redo all easy Code Coach Challenges with c++ at the moment and Test some functions. Does someone know a simple way to give "Coke" as a Kind of Default Case in that Code. https://code.sololearn.com/cSke88tdptn0/?ref=app

11th Jul 2022, 2:19 AM
Felix Alcor
Felix Alcor - avatar
5 Answers
+ 1
Felix You can add a function or lambda in the code. It basically searches the map for a key, returns the price of the relevant item when found, or the coke's price when not. You can also make the function/lambda to accept just the 4 items given in test, or make it accept a string container, then use loop to search the map for each item in the string container (more flexible than a fixed 4 parameters). I think you got this!
11th Jul 2022, 4:19 AM
Ipang
+ 2
No worries, I see you catching up pretty quick. Like I said, you got this, and I was right ... 😁
11th Jul 2022, 5:52 AM
Ipang
+ 1
Ipang I didn't entirely understand what you meant in the first part (just started c++ 4-6 hours ago 😉). For the second I didn't want to redo what I did with c# and Experiment a bit. So but now I'm finished I Made a search methode to search for the elements in the map else return value of Coke. https://code.sololearn.com/c73WEibmSJ7T/?ref=app
11th Jul 2022, 5:15 AM
Felix Alcor
Felix Alcor - avatar
+ 1
Felix That was also what I had in mind with the function. Good job! 👍
11th Jul 2022, 5:43 AM
Ipang
+ 1
Needed a bit longer to understand the find function.
11th Jul 2022, 5:50 AM
Felix Alcor
Felix Alcor - avatar