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

Map problems

I have this snippet of code. Let me know if I need to give more for
feedback:

typedef std::map<const char*, float> AttitudeRecord;

// player_info.GetAttitude should return an AttitudeRecord with
// key: roll value: 34
// key: pitch value: 598
// key: attitude value: 34
AttitudeRecord atttiude = player_info.GetAttitude();

AttitudeRecord::iterator iter;
iter = attitude.find("pitch");

// This prints out a completely wrong value! Why?
std::cout << "Value of pitch: " << attitude->second;

// This loop prints what seems to be the correct values
for (iter = attitude.begin(); iter != attitude.end(); ++iter)
{
std::cout << "\nKey: " << iter->first << " Value: " << iter->second;
}


Output:

Value of pitch: 3893.33 (Wrong!)
Key: roll Value: 34
Key: pitch Value: 598
Key: heading Value: 34
What I really want to do is easily get to the value of a specific key. But
I can't seem to find a way to do it.
Jul 22 '05 #1
4 1437

"Joe Laughlin" <Jo***************@boeing.com> wrote in message
news:Hz********@news.boeing.com...
I have this snippet of code. Let me know if I need to give more for
feedback:

typedef std::map<const char*, float> AttitudeRecord;

// player_info.GetAttitude should return an AttitudeRecord with
// key: roll value: 34
// key: pitch value: 598
// key: attitude value: 34
AttitudeRecord atttiude = player_info.GetAttitude();

AttitudeRecord::iterator iter;
iter = attitude.find("pitch");

// This prints out a completely wrong value! Why?
std::cout << "Value of pitch: " << attitude->second;


Presumably you mean iter->second.

The problem is that you have a map whose keys are pointers. So lookup is
done by comparing pointers, not by comparing strings, which I think is what
you are assuming. Presumably in this case the pointer you use to lookup the
record is not *precisely* the same pointer you used to add the record.

Simple answer is to use real strings

typedef std::map<std::string, float> AttitudeRecord;

which apart from solving your specific problem also has other advantages
namely making memory management much easier.

But if for some reason you must stick with pointers then you must provide a
comparison functor

struct StringLessThan
{
bool operator()(const char* a, const char* b) const
{
return strcmp(a, b) < 0;
}
};

typedef std::map<std::string, float, StringLessThan> AttitudeRecord;
john
Jul 22 '05 #2

But if for some reason you must stick with pointers then you must provide a comparison functor

struct StringLessThan
{
bool operator()(const char* a, const char* b) const
{
return strcmp(a, b) < 0;
}
};

typedef std::map<std::string, float, StringLessThan> AttitudeRecord;


Sorry, cut and paste error, I meant this

typedef std::map<const char*, float, StringLessThan> AttitudeRecord;

john
Jul 22 '05 #3
John Harrison wrote:
"Joe Laughlin" <Jo***************@boeing.com> wrote in
message news:Hz********@news.boeing.com...
I have this snippet of code. Let me know if I need to
give more for feedback:

typedef std::map<const char*, float> AttitudeRecord;

// player_info.GetAttitude should return an
AttitudeRecord with // key: roll value: 34
// key: pitch value: 598
// key: attitude value: 34
AttitudeRecord atttiude = player_info.GetAttitude();

AttitudeRecord::iterator iter;
iter = attitude.find("pitch");

// This prints out a completely wrong value! Why?
std::cout << "Value of pitch: " << attitude->second;
Presumably you mean iter->second.


Doh, I did.

The problem is that you have a map whose keys are
pointers. So lookup is done by comparing pointers, not by
comparing strings, which I think is what you are
assuming. Presumably in this case the pointer you use to
lookup the record is not *precisely* the same pointer you
used to add the record.

Simple answer is to use real strings

typedef std::map<std::string, float> AttitudeRecord;

I'll try that. I was just going off some examples I found, and they used
const char*. When I first started, I used std::string, but I ran into some
troubles that using const char* worked instead.
which apart from solving your specific problem also has
other advantages namely making memory management much
easier.

But if for some reason you must stick with pointers then
you must provide a comparison functor

struct StringLessThan
{
bool operator()(const char* a, const char* b) const
{
return strcmp(a, b) < 0;
}
};

typedef std::map<std::string, float, StringLessThan>
AttitudeRecord;
john


Jul 22 '05 #4
Joe Laughlin wrote:
John Harrison wrote:
"Joe Laughlin" <Jo***************@boeing.com> wrote in
message news:Hz********@news.boeing.com...
I have this snippet of code. Let me know if I need to
give more for feedback:

typedef std::map<const char*, float> AttitudeRecord;

// player_info.GetAttitude should return an
AttitudeRecord with // key: roll value: 34
// key: pitch value: 598
// key: attitude value: 34
AttitudeRecord atttiude = player_info.GetAttitude();

AttitudeRecord::iterator iter;
iter = attitude.find("pitch");

// This prints out a completely wrong value! Why?
std::cout << "Value of pitch: " << attitude->second;


Presumably you mean iter->second.


Doh, I did.

The problem is that you have a map whose keys are
pointers. So lookup is done by comparing pointers, not by
comparing strings, which I think is what you are
assuming. Presumably in this case the pointer you use to
lookup the record is not *precisely* the same pointer you
used to add the record.

Simple answer is to use real strings

typedef std::map<std::string, float> AttitudeRecord;


I'll try that. I was just going off some examples I
found, and they used const char*. When I first started,
I used std::string, but I ran into some troubles that
using const char* worked instead.


Doh, my problems were that I didn't include the string header. I hate
computers.
Jul 22 '05 #5

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

Similar topics

0
by: Jerome Lefebvre | last post by:
Hello, Hope this will interest a few. I been working with a friend on the problems given out during the "International Collegiate Programming Contest" (ICPC) http://icpc.baylor.edu/icpc/ ....
14
by: Jim Hubbard | last post by:
Are you up to speed on the difficulties in using the 1.1 .Net framework? Not if you are unaware of the 1,596 issues listed at KBAlertz (http://www.kbalertz.com/technology_3.aspx). If you are...
1
by: 3f | last post by:
Hello; We have made a web application that people can download from our web site and installed on: Windows XP Windows 2000 Professional Windows 2003 Server Windows 2000 Server
5
by: Corky | last post by:
This works: db2 SELECT DISTINCT PROBLEM_OBJECTS.PROBLEM_ID FROM PROBLEM_OBJECTS INNER JOIN PROBLEMS ON PROBLEM_OBJECTS.PROBLEM_ID = PROBLEMS.PROBLEM_ID WHERE INTEGER(DAYS(CURRENT DATE) -...
2
by: Ellen Graves | last post by:
I am having a lot of problems with DB2 8.3.1 on RH Linux AS2.1. Installing and running stored procedures is problematic. Stored procedures I have used for years on V7 on WinNT are now failing...
19
by: Jim | last post by:
I have spent the past few weeks designing a database for my company. The problem is I have started running into what I believe are stack overflow problems. There are two tab controls on the form...
10
by: BBFrost | last post by:
We just recently moved one of our major c# apps from VS Net 2002 to VS Net 2003. At first things were looking ok, now problems are starting to appear. So far ... (1) ...
19
by: Dales | last post by:
I have a custom control that builds what we refer to as "Formlets" around some content in a page. These are basically content "wrapper" sections that are tables that have a colored header and...
2
by: Brian | last post by:
NOTE ALSO POSTED IN microsoft.public.dotnet.framework.aspnet.buildingcontrols I have solved most of my Server Control Collection property issues. I wrote an HTML page that describes all of the...
0
by: Sergistm | last post by:
Hello World, :D I have a problem that it is making me crazy, I hope you can help me. I'm trying to execute a .exe file with the Procces.Start, and there is no problem when the file is on my...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.