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

Small problems formatting maps (<map>)

Hey,

consider the following chunk of code

[code]
#define CONSTANT 5

void func (const char *Code)
{
map <pChar, pChar> Codes;
Codes["A100"] = "SOMETHING\n";
Codes["A101"] = "ANOTHER SOMETHING", CONSTANT, "\n";

if (Codes.find(Code) != Codes.end())
fprintf_s (stderr, Codes
Expand|Select|Wrap|Line Numbers
  1. );
  2.     else fprintf_s (stderr, "SOMETHING" );
  3. }
  4.  
Alright this is swell but...
my problem is that whenever i call

Expand|Select|Wrap|Line Numbers
  1. func ("A101");
  2.  
I can only see "ANOTHER SOMETHING " as my output without printing "5" since thats what CONSTANT is.....

How can i print out everythign in A101?

thanks --

DK --
Aug 31 '07 #1
3 1493
*SOLUTION*

Alright i was thinking about this for a long time and figured out what i can do.... here is what i did....

Expand|Select|Wrap|Line Numbers
  1. #define Number 8
  2. typedef char* pChar;
  3.  
  4. // converts a string into a pChar
  5. pChar StrpChar (string InStr)
  6. {
  7.     return _strdup(InStr.c_str());
  8. }
  9.  
  10. // Converts integer into a pChar
  11. pChar StrInt (__int64 InNum)
  12. {
  13. ostringstream oStr;
  14. oStr << InNum;
  15.  
  16. return StrpChar(oStr.str());
  17. }
  18.  
  19. void func (const pChar Code)
  20. {
  21. string sNumber = StrInt(Number);
  22. map <pChar, string> Codes;
    Codes["A100"] = "text " + sNumber + " more text\n";
    if (Codes.find(Code) != Codes.end())
    fprintf_s (stderr, StrpChar(ErrorCodes[ErrorCode]));
    else;
  23.  
  24. Quit;
  25. }
  26.  
alright there now you can use maps just like couts and still using fprintf_s to do it...

P.S. im creating an errorcodes table in order to be able to see and modify error messages in my program easier rather then hardcoding every error string in manually.... :)

DK--
Aug 31 '07 #2
RRick
463 Expert 256MB
When I first saw the map<char*, char*> I shuttered thinking about all the ways this could go wrong. Memory leaks, shallow copies, etc. etc. One simple tip: Use C++ strings not char * for you data and especially for your keys (see below).

The char* key comparison is an address comparison, not a value comparison. (Can someone else verify this?) When you access the key as "A101" you'll get an static address. If you put "A101" in a local string, then the address will change and key will fail. I don't think this is what you want.
Expand|Select|Wrap|Line Numbers
  1. string val = Codes["A101"];    //  Works
  2. char buff[5];  strncpy( buff, 5, "A101");
  3. string val2 = Codes[buff];      // Fails, possibly crash & burn
  4.  
Aug 31 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
The char* key comparison is an address comparison, not a value comparison. (Can someone else verify this?)
Yes. map is a key/value container. If your key is a char*, then the comparison is based on the address inside the char*. Not good. Your advice to ue C++ string objects was exactly correct.

Expand|Select|Wrap|Line Numbers
  1. map<string, string> mymap;
  2.  
Sep 2 '07 #4

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

Similar topics

11
by: Markus Hämmerli | last post by:
I have seen in the STL that the map is working with one key. Does everyboby know if there is a possibility to have two key. Do you have a little example. Thanks Markus
1
by: Florian Liefers | last post by:
"Hello World\n", i have the following problem: One of my headerfiles for a lib is including <vector>. When i compile the lib, everything is done well. In my application another file is...
8
by: Pierre Couderc | last post by:
I am looking for a "special" kind of map : - it is read like a map - if the searched element exists, it is given back imediately - if the searched element does not exist, an initialise() is...
14
by: laurence | last post by:
I am implementing a comprehensive image-map generator utility, so have been studying W3C HTML 4.01 Specification (http://www.w3.org/TR/html4/struct/objects.html#h-13.6) on image maps (among other...
3
by: Evgeny | last post by:
Hi, all! I didn't find yet solution for this problem! Somebody knows where is a catch? Looks like "operator =" or copy constructor not implemented in one of internal templates.... Thanks in...
13
by: Steve Edwards | last post by:
Hi, Given a map: typedef map<long, string, greater<long> > mapOfFreq; Is there a quicker way to find the rank (i.e. index) of the elememt that has the long value of x? At the moment I'm...
5
by: Mike Copeland | last post by:
I'm having difficulty updating map objects. In the code I've excerpted below, I store map objects without difficulty, but my attempts to modify elements of the stored data don't work. struct...
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
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: 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
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
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
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.