Click here to Skip to main content
16,011,436 members
Home / Discussions / ATL / WTL / STL
   

ATL / WTL / STL

 
Generalmap erase issue Pin
Yu Zhiquan12-Oct-03 23:55
Yu Zhiquan12-Oct-03 23:55 
GeneralRe: map erase issue Pin
Abebe13-Oct-03 0:46
Abebe13-Oct-03 0:46 
GeneralRe: map erase issue Pin
Michael Dunn13-Oct-03 6:36
sitebuilderMichael Dunn13-Oct-03 6:36 
GeneralRe: map erase issue Pin
Joaquín M López Muñoz13-Oct-03 9:17
Joaquín M López Muñoz13-Oct-03 9:17 
GeneralRe: map erase issue Pin
miropl14-Oct-03 9:38
miropl14-Oct-03 9:38 
GeneralRe: map erase issue Pin
Joaquín M López Muñoz14-Oct-03 10:11
Joaquín M López Muñoz14-Oct-03 10:11 
GeneralRe: map erase issue Pin
miropl15-Oct-03 7:15
miropl15-Oct-03 7:15 
GeneralRe: map erase issue Pin
ZoogieZork15-Oct-03 8:03
ZoogieZork15-Oct-03 8:03 
miropl wrote:
const char* key = str.c_str(); // init that's ok
cout<<m[ key="" ]<<endl;="" now="" is="" correct<="" i="">

That shouldn't even compile if the type of m is map<char*,int>, since key is a const char* and not a char* (as referenced in the template).

In fact, on VS.NET 2k3 and GCC 3.2.3, the following code:

#include <map>
#include <iostream>

using namespace std;

typedef map<const char*,int> mymap_t;

int main(int argc, char* argv[]) {

    mymap_t m;

    m["January"]=1;
    string str="January";

    const char* key = str.c_str();
    cout << m[key] << endl;

    return 0;
}


Prints out 0 instead of 1.

The reason why is the crux of what Joaquín was saying -- it doesn't matter what the char* is pointing to (whether it's a zero-terminated C string or a big buffer of random char crud), the key is treated as a simple char pointer, and thus takes on pointer comparison semantics when used in the map.

Here's a simpler example of how things can go wrong with char*:

#include <conio.h>
#include <map>
#include <iostream>

using namespace std;

typedef map<char*,int> mymap_t;

int _tmain(int argc, _TCHAR* argv[]) {

    mymap_t m;

    m["January"]=1;
    char *str = strdup( "January" );

    cout << m[str] << endl;  // Prints out 0, not 1.

    free(str);

    return 0;
}



- Mike

GeneralRe: map erase issue Pin
souldog15-Oct-03 21:39
souldog15-Oct-03 21:39 
GeneralRe: map erase issue Pin
f229-Jun-06 20:49
f229-Jun-06 20:49 
GeneralactiveX message handler Pin
Mr_Byte10-Oct-03 22:19
Mr_Byte10-Oct-03 22:19 
Generaltroubles with boost::regex and STLport Pin
Andreas Saurwein10-Oct-03 6:11
Andreas Saurwein10-Oct-03 6:11 
GeneralWTL trouble with Common Controls Manifest Pin
BubbleGum8-Oct-03 23:23
BubbleGum8-Oct-03 23:23 
GeneralRe: WTL trouble with Common Controls Manifest Pin
Steve S9-Oct-03 0:53
Steve S9-Oct-03 0:53 
GeneralRe: WTL trouble with Common Controls Manifest Pin
BubbleGum9-Oct-03 3:41
BubbleGum9-Oct-03 3:41 
GeneralRe: WTL trouble with Common Controls Manifest Pin
umeca7410-Oct-03 1:48
umeca7410-Oct-03 1:48 
QuestionATL Edit field validation? Pin
bryces8-Oct-03 21:32
bryces8-Oct-03 21:32 
AnswerRe: ATL Edit field validation? Pin
Steve S8-Oct-03 21:54
Steve S8-Oct-03 21:54 
GeneralRe: ATL Edit field validation? Pin
Anonymous9-Oct-03 1:45
Anonymous9-Oct-03 1:45 
GeneralRe: ATL Edit field validation? Pin
bryces9-Oct-03 1:52
bryces9-Oct-03 1:52 
GeneralRe: ATL Edit field validation? Pin
Steve S9-Oct-03 2:13
Steve S9-Oct-03 2:13 
GeneralEvent notification from checkbox within TreeControl (from WTL) Pin
shivonkar8-Oct-03 2:20
shivonkar8-Oct-03 2:20 
GeneralRe: Event notification from checkbox within TreeControl (from WTL) Pin
algol14-Oct-03 21:15
algol14-Oct-03 21:15 
QuestionHow can my control download the file located on the remote server automatically? Pin
stanley guan8-Oct-03 0:22
stanley guan8-Oct-03 0:22 
Generalvectors. Pin
WREY7-Oct-03 14:32
WREY7-Oct-03 14:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.