Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > STL map insert question

Reply
Thread Tools

STL map insert question

 
 
vin b
Guest
Posts: n/a
 
      01-09-2006
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;
}

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-09-2006
vin b wrote:
> 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
 
Reply With Quote
 
 
 
 
Daevaorn
Guest
Posts: n/a
 
      01-09-2006
change localmap['x']->insert(s); to
(*localmap)['x'] = s;

 
Reply With Quote
 
vin b
Guest
Posts: n/a
 
      01-09-2006
thanks a million, .. this works ====> (*localmap)['x'] = s;

 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      01-09-2006
vin b wrote:
> 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?
 
Reply With Quote
 
vin b
Guest
Posts: n/a
 
      01-09-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
STL map insert Options Mike Copeland C++ 8 07-19-2011 03:20 PM
insert in std::map does not insert the object simon.elbaz@free.fr C++ 1 11-04-2007 11:42 PM
a stl map which use stl pair as the key Allerdyce.John@gmail.com C++ 2 02-22-2006 07:25 AM
STL: Map of maps possible, but no multi-map of maps? Workarounds? Marcus C++ 2 12-09-2005 06:34 AM
How to insert immutable object into the stl map yccheok@gmail.com C++ 2 11-17-2005 07:56 AM



Advertisments