473,411 Members | 2,059 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,411 software developers and data experts.

querry related to STL map..

I have a map<char *,int it does not work consistently. I have used
similar sort of implementation as below:

map<char *,intm1;
map<char *,int>::iterator i;

m1["January"] = 31;
m1["February"] = 28;
m1["March"] = 31;
m1["April"] = 30;
m1["May"] = 31;
m1["June"] = 30;
m1["July"] = 31;

This is similar to the map that i have used. Now accessing this yields
different results as below:

Option1:

char *p = "May";
int it = m1[p]; // Works properly returns 31.

Option2:
char *p = new char[3];
strcpy(p, "May");
int it = m1[p]; // returns 0 .. instead of 31 ....

Can anybody help me to resolve this issue?

Thanks.

Sep 27 '06 #1
7 1859
"ravips" <ra*******@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
>I have a map<char *,int it does not work consistently.
Don't use char* as a key type; it is very unlikely to work as you expect.
Use std::string instead.

Sep 27 '06 #2
ravips wrote:
I have a map<char *,int it does not work consistently. I have used
similar sort of implementation as below:

map<char *,intm1;
map<char *,int>::iterator i;
You should prefer std::string over pointers to char.
m1["January"] = 31;
m1["February"] = 28;
m1["March"] = 31;
m1["April"] = 30;
m1["May"] = 31;
m1["June"] = 30;
m1["July"] = 31;

This is similar to the map that i have used. Now accessing this yields
different results as below:

Option1:

char *p = "May";
int it = m1[p]; // Works properly returns 31.
Then you got very bad luck. Your compiler probably found out that the
literal "May" is there several times and added it to the program only once.
Option2:
char *p = new char[3];
strcpy(p, "May");
int it = m1[p]; // returns 0 .. instead of 31 ....

Can anybody help me to resolve this issue?
Remember that std::map keeps its items sorted by key. For that, it comares
key values. It also uses such a comparison to find specific items. In your
case, the key type is a pointer, so you get a pointer comparison, and
nothing more.
You either need to use a string class like std::string, which will do a real
string comparison, or you have to write your own comparison function that
the map can use.
Sep 27 '06 #3

ravips wrote:
I have a map<char *,int it does not work consistently. I have used
similar sort of implementation as below:

map<char *,intm1;
map<char *,int>::iterator i;

m1["January"] = 31;
m1["February"] = 28;
m1["March"] = 31;
m1["April"] = 30;
m1["May"] = 31;
m1["June"] = 30;
m1["July"] = 31;

This is similar to the map that i have used. Now accessing this yields
different results as below:

Option1:

char *p = "May";
int it = m1[p]; // Works properly returns 31.

Option2:
char *p = new char[3];
strcpy(p, "May");
int it = m1[p]; // returns 0 .. instead of 31 ....

Can anybody help me to resolve this issue?
You have two choices.

Make it a std::map<std::string, intinstead of a std::map<char*, int>.
Then it will work fine. That is the preferred solution.

If you insist on using char* instead (why?), then do the following:

struct ltstr
{
bool operator()(const char* s1, const char* s2) const
{
return strcmp(s1, s2) < 0;
}
};

and then make it a std::map<const char*, int, ltstrinstead of a
std::map<char*, int>. That way you are providing a functor to do the
proper comparison between char*.

Best regards,

Tom

Sep 27 '06 #4
ravips wrote:
Option2:
char *p = new char[3];
Buffer overflow? "May" needs 4 chars (zero terminated string!)
strcpy(p, "May");
int it = m1[p]; // returns 0 .. instead of 31 ....

Can anybody help me to resolve this issue?
- Martin

Sep 27 '06 #5
ravips wrote:
I have a map<char *,int it does not work consistently. I have used
similar sort of implementation as below:

map<char *,intm1;
map<char *,int>::iterator i;

m1["January"] = 31;
m1["February"] = 28;
m1["March"] = 31;
m1["April"] = 30;
m1["May"] = 31;
m1["June"] = 30;
m1["July"] = 31;

This is similar to the map that i have used. Now accessing this yields
different results as below:

Option1:

char *p = "May";
int it = m1[p]; // Works properly returns 31.

Option2:
char *p = new char[3];
strcpy(p, "May");
int it = m1[p]; // returns 0 .. instead of 31 ....

Can anybody help me to resolve this issue?
Use map< string, int instead: when using char* the map will store a the
address of the first character and use that address for comparison. It will
not compare the actual C-strings. That is, if you use a different address
pointing to a C-string that happens to read alike some stored in the map,
the map entry will not be found. In that case, the map allocates a new
entry and initialized the value with the default int (i.e., 0). This is
what you observed.
Best

Kai-Uwe Bux

Sep 27 '06 #6
In article <11**********************@i42g2000cwa.googlegroups .com>,
"ravips" <ra*******@gmail.comwrote:
I have a map<char *,int it does not work consistently. I have used
similar sort of implementation as below:

map<char *,intm1;
map<char *,int>::iterator i;

m1["January"] = 31;
m1["February"] = 28;
m1["March"] = 31;
m1["April"] = 30;
m1["May"] = 31;
m1["June"] = 30;
m1["July"] = 31;

This is similar to the map that i have used. Now accessing this yields
different results as below:

Option1:

char *p = "May";
int it = m1[p]; // Works properly returns 31.

Option2:
char *p = new char[3];
strcpy(p, "May");
int it = m1[p]; // returns 0 .. instead of 31 ....

Can anybody help me to resolve this issue?
You can resolve this in one of two ways. Best would be to have the map
hold the string by value instead of pointer...

map<string, intm1;

Second up would be to create a comparison function for char*s:

bool leftBeforeRight( const char* lhs, const char* rhs ) {
return strcmp( lhs, rhs ) < 0;
}

map<char*, int,
pointer_to_binary_function<const char*, const char*, bool
m1( ptr_fun( &leftBeforeRight ) );

--
There are two things that simply cannot be doubted, logic and perception.
Doubt those, and you no longer*have anyone to discuss your doubts with,
nor any ability to discuss them.
Sep 27 '06 #7
Option2:
char *p = new char[3];
strcpy(p, "May");
int it = m1[p]; // returns 0 .. instead of 31 ....
"May" has a trailing '\0'. strcpy() copies that trailing '\0',
but you've only allocated space for 3 characters.

/Glen
Sep 28 '06 #8

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

Similar topics

4
by: Eric Kincl | last post by:
Hey everyone, I know this isn't a SQL group, but I'm on my colleges news server and they don't offer one. Hopefully someone here will be able to help me. I have a database that is normalized...
2
by: Eric Kincl | last post by:
Hello, I have an array of data in PHP. I would like to insert each member of the array into it's own row in SQL. The array is of variable length, so it would have to be dynamic code. How would...
0
by: Costa Lino | last post by:
Hi All, I have a DataSet with xml file and I want to make a querry like this DataView dv = new DataView(mytable); dv.RowFilter = " Impression < ( MaxImpressions) "; Impression et...
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...
20
by: Lalatendu Das | last post by:
hi let's say i have a structure struct test { int A; char B; int C; }; this above structure defination always going to take 16 byte in memeory in whatever manner we align the member variables...
5
by: Clownfish | last post by:
OK, I'm having a brain freeze. I have a table like this: Office Name Phone ---------------------------------- SG Larry 555-1212 SG Moe 553-4444 SG Curly ...
1
Steve Kiss
by: Steve Kiss | last post by:
Hi. I am developping a site for which one of the pages uses querry strings to pass some parameters. I can use the querry strings if I call the page from a plain html anchor. However, when I add the...
1
by: nj2md | last post by:
Can some one assist with a querry. I need to know the code to querry a database to find the number of female and males that make over 50K a year and how to get capital gains and loses from the same...
0
by: getmeidea | last post by:
I have the following tables, 1> employee_master(emp_id int primary key, emp_name varchar(100)); 2> employee_salary_payment(salary_rid int primary key, emp_id int, sal_date date, paid_amt int); ...
2
by: dipalichavan82 | last post by:
i came across a article, where it was mentioned if we want a dynamic querry to fire then use parameterized querry e.g. string inputcity=textbox.text; SqlCommand cmd = new SqlCommand("select * from...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.