Skip to content

Instantly share code, notes, and snippets.

@shitu13
Last active May 18, 2024 04:30
Show Gist options
  • Save shitu13/370fb39be3c934eae682e659b3572911 to your computer and use it in GitHub Desktop.
Save shitu13/370fb39be3c934eae682e659b3572911 to your computer and use it in GitHub Desktop.
map<int,int> mapInsert(int arr[],int n){
map<int,int>mp;
//Insert arr[i] as key and i as value
for(int i=0; i<n; i++){
mp[arr[i]] = i;
}
return mp;
}
void mapDisplay(map<int,int>mp){
//Print every key and value pair in a new line
for(auto &it: mp){
cout << it.first <<" "<<it.second <<endl;
}
}
void mapErase(map<int,int>&mp,int x){
//Write the if and else condition to erase x from map
auto it = mp.find(x);
if(it!=mp.end()){
mp.erase(it);
cout<<"erased "<<x;
}else{
cout<<"not found";
}
cout<<endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment