I've passed a map by pointer to a function, but now I'm unable to change the contents of the map because the code doesn't compile. I'm unsure whether I'm even allowed to pass the map as a pointer. For example:
#include <iostream>
#include <stdlib.h>
#include <map>
#include <string>
using namespace std;
void faa(map<int,string> *baa){
*baa[1] = "somethingnew";
}
int main(){
map<int,string> bar;
bar[1] = "one";
bar[2] = "two";
bar[3] = "three";
int ii;
for(ii=1;ii<4;ii++){
cout<<"bar["<<ii<<"]="<<bar[ii]<<endl;
}
faa(&bar);
cout<<"after"<<endl;
for(ii=1;ii<4;ii++){
cout<<"bar["<<ii<<"]="<<bar[ii]<<endl;
}
}
When I compile this I get the error:
error: no match for ‘operator*’ (operand type is ‘std::map >’)
*baa[1] = "somethingnew";
Is a function like faa possible? What is the syntax?
(*baa)[1]should work insteadconstness and what to do with unfound keys though.atandoperator[]are semantically different if the key is not found.