const char* type and string missmatch?

Started by
19 comments, last by Tradone 18 years, 1 month ago

multimap<const char*, int, ltstr> multimap_order_total;

multimap_order_total.insert(pair<const char* const, int>( const_cast < char* >( ReadPosition(sort_name, Menu_Cfg).c_str() ), i));
ReadPosition(string, string) returns a string. `?? :- `?? :- `?? :-`?? :- instead of strings I get these kind of things stored. ------------------------------------------ here's another case.

string test_str="thisisatest";
multimap_order_total.insert(pair<const char* const, int>( const_cast < char* >( test_str.c_str() ), i));
this works. ------------------------------------------ now here's another example..

string test_str="thisisatest";
test_str=ReadPosition(sort_name, Menu_Cfg);
cout << test_str;

multimap_order_total.insert(pair<const char* const, int>( const_cast < char* >( test_str.c_str() ), i));
this is no good. but the cout outputs what it's supposed to output. [Edited by - Tradone on March 2, 2006 7:15:40 PM]
Advertisement
Umm, std::string::c_str() returns a temporary string, that will be invalidated when the string goes out of scope or resizes.

Why not have the multimap have std::string as the index type?
Quote:Original post by rip-off
Umm, std::string::c_str() returns a temporary string, that will be invalidated when the string goes out of scope or resizes.

Why not have the multimap have std::string as the index type?


I'm not so good in using maps.
When I change to std::string I get an error that I don't know how to fix.
Quote:Original post by Tradone
Quote:Original post by rip-off
Umm, std::string::c_str() returns a temporary string, that will be invalidated when the string goes out of scope or resizes.

Why not have the multimap have std::string as the index type?


I'm not so good in using maps.
When I change to std::string I get an error that I don't know how to fix.


Post it then...
int nocase_cmp(string & s1, string& s2) {  string::const_iterator it1=s1.begin();  string::const_iterator it2=s2.begin();  //stop when either string's end has been reached  while ( (it1!=s1.end()) && (it2!=s2.end()) )   {     if(::toupper(*it1) != ::toupper(*it2)) //letters differ?     // return -1 to indicate smaller than, 1 otherwise      return (::toupper(*it1)  < ::toupper(*it2)) ? -1 : 1;     //proceed to the next character in each string    ++it1;    ++it2;  }  size_t size1=s1.size(), size2=s2.size();// cache lengths   //return -1,0 or 1 according to strings' lengths    if (size1==size2)       return 0;    return (size1<size2) ? -1 : 1;}struct ltstr{  bool operator()(string s1, string s2) const  {//    return strcmp(s1, s2) < 0;    return nocase_cmp(s1, s2) < 0;  }};......multimap<string, int, ltstr> multimap_order_total;........for( int order_i=(int)ToInt(GetCfg("total", "system/users/orders/total.cgi", SystemVar_String, SystemVar_Count)); order_i >= 1000; order_i-- ){	if(ifstream(Menu_Cfg.c_str())){		Order_TotalOrders++;multimap_order_total.insert( ReadPosition(sort_name, Menu_Cfg) , order_i );	}}


something like this, but I get a type mismatch on the bool i suspect it's from
ltstr....

there might be more to this ... let me find more sources..
Try making your strings const, and making the iterators const_iterators.

If that doesnt work , post the compiler errors.

Also, use [ source ] and [ / source ] (no spaces) to make you code pretty...

EDIT : Oh yes, you could make ltstr return an int too.
Quote:Original post by Tradone
would that require me to change the output of ReadPosition(sort_name, Menu_Cfg) to a const string also?


No, as the map takes std::string as its key, not const std::strings.

The const thing is so that your compare functor doesnt mess with the strings that the map hands them.

Imagine the effects if your compare function ( by accident ) edited the strings in the map's index. Since we wouldn't want that, making the strings const makes everyone happy...
I see what you're trying to say!
That makes a lot of sense!
sorry for the late reply, I'm gonna get on this right now.
by the way, what's the difference between char* and string?
and which is better?
Quote:Original post by Tradone
by the way, what's the difference between char* and string?
and which is better?


char * is a char array, or c-string, of unspecified location. It could dissappear from under you if it is a local array, like this:

char str[100];

Or if it is mallocated, or newed, then if someone deletes it you will have problems.

At worst, it could be a pointer to an individual character, accidentally being used as a string.

There are many other problems, such as ensuring that the string is ended in the nul character ('\0'), so that c-string functions know its size. Also, there is no way to find out how much free sapce is at the end of the string for appending things to it.

std::string is an object that is responsible for managing an array of characters. Under the hood, it works with char arrays. But it presents an easy to understand interface to us, we really don't care what it does underneath, but from above things like copying, passing to functions, testing equality and returning them work as expected.

This topic is closed to new replies.

Advertisement