V
vin b
Hi,
How would I invoke a method on a reference to an STL object (STL map in
this
case)
In this contrived example, I want to invoke the insert() method on
localmap?
Thanks, Vin
#include <iostream>
#include <string>
#include <map>
using namespace std;
map < char, string >* localmap;
void insmap( map < char, string > &m)
{
string s = "xxxxx";
#if 0
// normal implementation - this works , ... life would be too easy
m[s[0]] = s;
#endif
localmap = &m;
localmap['x']->insert(s); //doesn't work
}
int
main (int argc, char *argv[])
{
// Here is the declaration of the map
map < char, string > mymap;
map < char, string >::iterator iter;
// Here are example strings that I will add to an empty map
string s1 = "This";
string s2 = "is";
string s3 = "an";
string s4 = "example";
string s5 = "string";
// Here we add the strings to the map
mymap[s1[0]] = s1;
mymap[s2[0]] = s2;
mymap[s3[0]] = s3;
mymap[s4[0]] = s4;
mymap[s5[0]] = s5;
// Here is the output of the map in order from beginning to end
insmap(mymap);
cout << "This is the output of the map in order" << endl;
for (iter = mymap.begin (); iter != mymap.end (); iter++)
{
cout << (*iter).
first << " is the first character in the word " << iter->
second << endl;
}
return 0;
}
How would I invoke a method on a reference to an STL object (STL map in
this
case)
In this contrived example, I want to invoke the insert() method on
localmap?
Thanks, Vin
#include <iostream>
#include <string>
#include <map>
using namespace std;
map < char, string >* localmap;
void insmap( map < char, string > &m)
{
string s = "xxxxx";
#if 0
// normal implementation - this works , ... life would be too easy
m[s[0]] = s;
#endif
localmap = &m;
localmap['x']->insert(s); //doesn't work
}
int
main (int argc, char *argv[])
{
// Here is the declaration of the map
map < char, string > mymap;
map < char, string >::iterator iter;
// Here are example strings that I will add to an empty map
string s1 = "This";
string s2 = "is";
string s3 = "an";
string s4 = "example";
string s5 = "string";
// Here we add the strings to the map
mymap[s1[0]] = s1;
mymap[s2[0]] = s2;
mymap[s3[0]] = s3;
mymap[s4[0]] = s4;
mymap[s5[0]] = s5;
// Here is the output of the map in order from beginning to end
insmap(mymap);
cout << "This is the output of the map in order" << endl;
for (iter = mymap.begin (); iter != mymap.end (); iter++)
{
cout << (*iter).
first << " is the first character in the word " << iter->
second << endl;
}
return 0;
}