473,396 Members | 1,756 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

STL map insert question

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

Jan 9 '06 #1
5 6419
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
Jan 9 '06 #2
change localmap['x']->insert(s); to
(*localmap)['x'] = s;

Jan 9 '06 #3
thanks a million, .. this works ====> (*localmap)['x'] = s;

Jan 9 '06 #4
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?
Jan 9 '06 #5
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

Jan 9 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bob Bedford | last post by:
I've a textarea and would like to save the content in a mysql table each time a user click on a form. How can I do for avoiding error when the user put a " or a ' in the message, or any other...
8
by: Sans Spam | last post by:
Greetings! I have a table that contains all of the function permissions within a given application. These functions are different sections of a site and each has its own permissions (READ, WRITE,...
4
by: soni29 | last post by:
hi, i have a small question regarding sql, there are two tables that i need to work with on this, one has fields like: Table1: (id, name, street, city, zip, phone, fax, etc...) about 20 more...
5
by: TThai | last post by:
HI, I'm trying to insert records to a table using bcp command. The problem is the input file to the bcp is a text file that looks like this: Text file data: 1234 abc def ghi jkl mno ...
11
by: Jean-Christian Imbeault | last post by:
I have a table with a primary field and a few other fields. What is the fastest way to do an insert into that table assuming that sometimes I might try to insert a record with a duplicate primary...
25
by: Andreas Fromm | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, Im building an user database with many tables keeping the data for the Address, Phone numbers, etc which are referenced by a table where I...
16
by: robert | last post by:
been ruminating on the question (mostly in a 390/v7 context) of whether, and if so when, a row update becomes an insert/delete. i assume that there is a threshold on the number of columns of the...
1
by: javedna | last post by:
Can PHP help with the following as I have tried in the MYSQL Forums and cant get any help Thanks Nabz ---------------------------------------- Hi I am developing a PHP MYSQL questionnaire...
10
by: Aditya | last post by:
Hi All, I would like to know how it is possible to insert a node in a linked list without using a temp_pointer. If the element is the first element then there is no problem but if it is in...
24
by: Henry J. | last post by:
My app needs to insert thousand value rows into a mostly empty table (data are read from a file). I can either use inserts, or use merge. The advantage of using merge is that in the few cases...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.