Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
ShoppingList
//g++ 7.4.0 //Shopping list: important map container functions in action //this code is created by Rezaul Hoque on August 01,2021;contact: jewelmrh@yahoo.com //note: codes shared by Rezaul Hoque on rextester are not for sale; they are created and shared to facilitate the algorithm learning process; many like Hoque use this platform to practice programming ;Rezaul hopes his contribution helps others to fine tune their learning; #include <iostream> #include <map> #include <string> using namespace std; int main(){ //here in creating the map object,3 parameters are used; first one is the key type,second one is the data type and the third one is the comparison type that will order the list from least to great keys; note this less<comparison type> is the default map<int,string,less<int>> list; // use of map "[ ]" operator list[3]="soap"; list[2]="toothpaste"; list[1]="comb"; //use of map emplace() and map emplace_hint() list.emplace(5,"cooking oil"); list.emplace(4,"milk"); list.emplace(6,"fish"); list.emplace(7,"lentils"); list.emplace(8,"snake gourd"); list.emplace(9,"potato"); auto it=list.end(); list.emplace_hint (it,10,"puffed rice"); list.emplace_hint(it,11,"peanut"); list.emplace_hint(it,12,"jaggery"); cout<< "The shopping list looks like:\n"; for(auto& x: list) cout<<x.first<<" : "<<x.second<<"\n"; //use of map size() cout<<"\nThere are "<<list.size()<<" elements left.\n"; cout<<"The list is based on priority of making a visit to the respective shop: first grocery, then kitchem market and finally the street snack vendor.\n"; cout<<"If the priority were reversed (first go to the snack vendor and finally to the grocery), the list would look like:\n"; //use of map rbegin() and rend() for(map<int,string>::reverse_iterator it=list.rbegin(); it!=list.rend();++it) cout<<it->first<<" => "<<it->second<<" \n"; cout<<"But let's stick to the original list and start our shopping.\n"; cout<<"\n After paying visit to grocery the list looks like:\n" ; //use of map erase() list.erase(1); list.erase(2); list.erase(3); for(auto& x: list) cout<<x.first<<" : "<<x.second<<"\n"; cout<<"\nThere are "<<list.size()<<" elements left.\n"; cout<<"After paying a visit to the vegetable market the list looks like:\n" ; //use of map lower_bound() and upper_bound() to delete a range map<int,string>::iterator low,up; low=list.lower_bound(4); up = list.upper_bound(9); list.erase (low,up); for(auto& x: list) cout<<x.first<<" : "<<x.second<<"\n"; cout<<"\There are "<<list.size()<<" elements left.\n"; cout<<"\nAfter paying visit to street snack vendor the list looks like:\n" ; list.erase(10); list.erase(11); list.erase(12); for(auto& x: list) cout<<x.first<<" : "<<x.second<<"\n"; cout<<"\There are "<<list.size()<<" elements left.\n"; return 0; }
run
|
edit
|
history
|
help
0
tasks
Equilateral triangle
Mr
Binary search2
Binary search on sorted array
C++ Car Racing game framework
C++ state machine prototype
silnia
property get set
check Prime