STL map insert question

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;
}
 
V

Victor Bazarov

vin said:
How would I invoke a method on a reference

Actually, you have a _pointer_...
> to an STL object (STL map in
this
case)

In this contrived example, I want to invoke the insert() method on
localmap?

For a pointer to an object, you always use -> to invoke a member.
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

If 'localmap' is a pointer, you need to dereference it first:

(*localmap)['x'] = s;

but in fact, if you _have_to_ use 'insert', the syntax is different.
RTFM about 'insert' member and what argument it needs.

localmap->insert(...
}

[...irrelevant code snipped...]

V
 
R

red floyd

vin said:
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;
[redacted]

At the risk of asking the obvious, is there a reason you're using a
pointer to a map instead of the map itself?
 
V

vin b

good question, ...

the example is a contrived way to represent some existing code i need
to modify.

in that case, a pointer to a map is passed into the init function of a
handler class and is stored / used for all subsequent operations on
that object (withing the scope of validity).

given a choice, not the way i would design things but that's the way
this framework is set up
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Swich case problem??? 2
STL MAP, how to restrict 5
STL map and char * problems 3
STL::MAP: Printing values only once .. 9
Sorting an STL map 1
2 JK Circuit in VHDL 0
Modify STL multiset 2
Iterator not work 0

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top