Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
MapGrocery
//g++ 7.4.0 //Grocery List: use of map functions //map is a container like array that stores elements come in a pair of key & data(aka mapped value); key is used to sort and identify the data element; there can’t be two similar keys; elements are stored here in a ordered way based on the key; so map container does the sorting work for you and helps to create an ordered list; here uses of important map functions are demonstrated; //this code is created by Rezaul Hoque on July 26,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(){ //creating a map object map<char,int> grocery; //inserting data with emplace(); note here key 'C' is entered before key 'A' and key 'B'; notice the difference when the list is displayed grocery.emplace('C',10); grocery.emplace( 'B',20); grocery.emplace( 'A',30); // inserting data with emplace_hint() auto it= grocery.end(); grocery.emplace_hint(it,'P',10); grocery.emplace_hint(it,'Q',14); grocery.emplace_hint(it,'R',12); cout<<"A grocery list:\n"; for(auto& x: grocery){ cout<<x.first<<":"<<x.second<<"\n"; } //use of map at() to assign values; note here "LLL" is entered before "BBB" and "DDD"; map<string,int> list={{"LLL", 0}, {"BBB",0},{"DDD",0}}; list.at("LLL")= 55; list.at("BBB")= 25; list.at("DDD")= 35; cout<<"Another list created with map at():\n"; for(auto& x: list){ cout<<x.first<<":"<<x.second<<"\n"; } //use of map [] and = operators to assign values map<char,int> grocery2; grocery2['W']= 21; grocery2['X']=22; grocery2['Y']= 23; grocery2['Z']= grocery2['Y']; cout<<"Another list ,grocery2,created with map '[ ]'and '=':\n"; for(auto& x: grocery2){ cout<<x.first<<":"<<x.second<<"\n"; } //swapping grocery 2 and grocery grocery.swap(grocery2 ); cout<<"grocery list after swap:\n"; for(map<char,int>::iterator it=grocery.begin(); it != grocery.end();++it) cout<<it->first<<" =>"<<it->second<<"\n"; //use of map lower_bound () and upper_bound() to select a range and clear it with erase() map<char,int>::iterator low,up; low=grocery2.lower_bound('B'); up = grocery2.upper_bound('Q'); grocery2.erase (low,up); //use of map size() cout<<"After erase() grocery2 has "<<grocery2.size()<<" elements\n"; cout<<"And they are: \n"; for(auto& x: grocery2){ cout<<x.first<<":"<<x.second<<"\n"; } return 0; }
run
|
edit
|
history
|
help
0
Factoriel
Anagrams WIP
2021(M2)Simulare:S3:1
1234
My billing system
Roger Cheng
DeltaX campus recruitment
BLREDSET
4C test
Networked path_dp