473,287 Members | 1,447 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,287 software developers and data experts.

Problem with map in C++

Hey
Heres what i want to do:

I want to create a map which has <string, vector<vector<int>>>.
The string is always unique.
What i need to do is, as i read each string from a file, i compare it with existing map keys to find if that string is already present.
for that i use map.find(stringname). I store the return value in an iterator

THen i check if iter == map.end() If yes then this key is not present.

if (key is not present) {
insert the key;
put the value;
}

else {
get the value;
update the value in a temp variable;
erase the previous key using map.erase(key);
insert the updated element;
map.insert();
}

The problem is :(I wrote a dummy program to find the problem)

I hav a loop which runs thrice
I have 2 stings.
In the 1st iteration i do map.find(string1)
then i compare it with map.end().
The comparison is true (thats correct)
Then i insert a key and a value.

In the 2nd iteration, I do map.find(string1)
x = (iter == map.end() )
the value of x turns out to be 0. (thats correct)

I get the value as val = map[string1]

val is a vector of vectors.
I update the value of val, and do map.erase(string1) and insert it back again using map.insert().

In the 3rd iteration, i do map.find(string2) (note this is the second string)
x = (iter == map.end() )

Surprisingly the value of x is 0 (wrong)

When i print the value of that iterator i,e, (*iter).first, it contains the second string.

How the hell did the second string get into the map??
I hav no idea abt it.

Can sum1 help??
Jul 24 '07 #1
3 2380
Your pseudocode seems quite complicated...
To update a value, there is no need to erase the previous one from the map and insert a new one. Take a look to operator [].
Also, with iterators, be careful to not use one instance that has been previously erased.

Hey
Heres what i want to do:

I want to create a map which has <string, vector<vector<int>>>.
The string is always unique.
What i need to do is, as i read each string from a file, i compare it with existing map keys to find if that string is already present.
for that i use map.find(stringname). I store the return value in an iterator

THen i check if iter == map.end() If yes then this key is not present.

if (key is not present) {
insert the key;
put the value;
}

else {
get the value;
update the value in a temp variable;
erase the previous key using map.erase(key);
insert the updated element;
map.insert();
}

The problem is :(I wrote a dummy program to find the problem)

I hav a loop which runs thrice
I have 2 stings.
In the 1st iteration i do map.find(string1)
then i compare it with map.end().
The comparison is true (thats correct)
Then i insert a key and a value.

In the 2nd iteration, I do map.find(string1)
x = (iter == map.end() )
the value of x turns out to be 0. (thats correct)

I get the value as val = map[string1]

val is a vector of vectors.
I update the value of val, and do map.erase(string1) and insert it back again using map.insert().

In the 3rd iteration, i do map.find(string2) (note this is the second string)
x = (iter == map.end() )

Surprisingly the value of x is 0 (wrong)

When i print the value of that iterator i,e, (*iter).first, it contains the second string.

How the hell did the second string get into the map??
I hav no idea abt it.

Can sum1 help??
Jul 24 '07 #2
Thanks
The operator [ ] has the same functionality as map.insert()
I have tried using both. Moreover, if a key is not present, map[ key ] will insert the key into the map.

I realize that i dont need to erase the previous value. I m still facing the same problem.

HEre s a simpler version of my pseudocode:
j = 0;
while (j < 2) {
if (i == 0) {
iter = map.find(string1);
copy string1 to str;
x = (iter == map.end()); //x = 1 after this
}
else {
iter = map.find(string2);
copy string2 into str;
x = (iter == map.end()); //x = 1 after this -but i m getting 0
}

if (x) {
//Calculate i;
}
else {
int i = (*iter).second;
//update i;
}

map[str] = i;
j++;
}

This is my dummy code to check the basic operations.
The problem is, the first time x = 1
String 1 is inserted.
Second time, when i try to find string 2, i find tat it already exists in the map, and that its position is where string1 was inserted.

I.e. in the else part where i search for string2 , if i print (*iter).first, I get string2 instead of string1.

Also could u elaborate more on the iterators part.

Thanks

Your pseudocode seems quite complicated...
To update a value, there is no need to erase the previous one from the map and insert a new one. Take a look to operator [].
Also, with iterators, be careful to not use one instance that has been previously erased.
Jul 24 '07 #3
Thanks to every1 who looked into the problem
I figured out the solution
The map should have been of type <string, int>
instead of <const char *, int>
Thanks

Thanks
The operator [ ] has the same functionality as map.insert()
I have tried using both. Moreover, if a key is not present, map[ key ] will insert the key into the map.

I realize that i dont need to erase the previous value. I m still facing the same problem.

HEre s a simpler version of my pseudocode:
j = 0;
while (j < 2) {
if (i == 0) {
iter = map.find(string1);
copy string1 to str;
x = (iter == map.end()); //x = 1 after this
}
else {
iter = map.find(string2);
copy string2 into str;
x = (iter == map.end()); //x = 1 after this -but i m getting 0
}

if (x) {
//Calculate i;
}
else {
int i = (*iter).second;
//update i;
}

map[str] = i;
j++;
}

This is my dummy code to check the basic operations.
The problem is, the first time x = 1
String 1 is inserted.
Second time, when i try to find string 2, i find tat it already exists in the map, and that its position is where string1 was inserted.

I.e. in the else part where i search for string2 , if i print (*iter).first, I get string2 instead of string1.

Also could u elaborate more on the iterators part.

Thanks
Jul 24 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Kostatus | last post by:
I have a virtual function in a base class, which is then overwritten by a function of the same name in a publically derived class. When I call the function using a pointer to the derived class...
117
by: Peter Olcott | last post by:
www.halting-problem.com
18
by: Ian Stanley | last post by:
Hi, Continuing my strcat segmentation fault posting- I have a problem which occurs when appending two sting literals using strcat. I have tried to fix it by writing my own function that does the...
28
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass();...
6
by: Ammar | last post by:
Dear All, I'm facing a small problem. I have a portal web site, that contains articles, for each article, the end user can send a comment about the article. The problem is: I the comment length...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
2
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
0
by: =?Utf-8?B?am8uZWw=?= | last post by:
Hello All, I am developing an Input Methop (IM) for PocketPC / Windows Mobile (PPC/WM). On some devices the IM will not start. The IM appears in the IM-List but when it is selected from the...
1
by: sherifbk | last post by:
Problem description ============== - I have 4 clients and 1 server (SQL server) - 3 clients are Monitoring console 1 client is operation console - Monitoring console collects some data from...
9
by: AceKnocks | last post by:
I am working on a framework design problem in which I have to design a C++ based framework capable of solving three puzzles for now but actually it should work with a general puzzle of any kind and I...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.