public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: map woes
@ 2001-07-17 23:13 Michael Veksler
  0 siblings, 0 replies; 2+ messages in thread
From: Michael Veksler @ 2001-07-17 23:13 UTC (permalink / raw)
  To: Srikanth Vedire; +Cc: gcc-help

From http://www.sgi.com/tech/stl/AssociativeContainer.html:
Immutability of keys :
                    Every element of an Associative Container has an
immutable key.
                    Objects may be inserted and erased, but an element in
an
                    Associative Container
                    **may not be modified in such a way as to change its
key**

But you effectively modify the key by modifying buff (which contains
the value of a key).
The way you define the comparison function, the value of the key is not
the pointer to buff, but its contents.



Srikanth Vedire <srvedire@yahoo.com> on 18-07-2001 00:54:42

Please respond to Srikanth Vedire <srvedire@yahoo.com>

To:   gcc-help@gcc.gnu.org
cc:
Subject:  map woes




Hello,

I seem to have a simple problem.. Why are the results
after running this program different than expected?

I get
[1] = (null) // instead of 100
[2] = 200
[3] = 300

Hopefully someone can tell me what I'm doing wrong.
I'm using 2.95.3 20010315 release of gcc.

Srikanth

#include <iostream>
#include <map>

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


typedef map<const char *, const char *, ltstr>
CharCharMap;

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

    CharCharMap temp2;

    buff[0] = '1';
    buff[1] = '\0';
    temp2[(const char *)buff] = "100";
    buff[0] = '2';
    buff[1] = '\0';
    temp2[buff] = "200";
    temp2["3"] = "300";

    cout << "[1] = " << temp2["1"] << endl;
    cout << "[2] = " << temp2["2"] << endl;
    cout << "[3] = " << temp2["3"] << endl;

}




__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail
http://personal.mail.yahoo.com/



^ permalink raw reply	[flat|nested] 2+ messages in thread

* map woes
@ 2001-07-17 14:54 Srikanth Vedire
  0 siblings, 0 replies; 2+ messages in thread
From: Srikanth Vedire @ 2001-07-17 14:54 UTC (permalink / raw)
  To: gcc-help

Hello,

I seem to have a simple problem.. Why are the results
after running this program different than expected?

I get
[1] = (null) // instead of 100
[2] = 200 
[3] = 300

Hopefully someone can tell me what I'm doing wrong.
I'm using 2.95.3 20010315 release of gcc.

Srikanth

#include <iostream>
#include <map>

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


typedef map<const char *, const char *, ltstr>
CharCharMap;

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

    CharCharMap temp2;

    buff[0] = '1';
    buff[1] = '\0';
    temp2[(const char *)buff] = "100";
    buff[0] = '2';
    buff[1] = '\0';
    temp2[buff] = "200";
    temp2["3"] = "300";

    cout << "[1] = " << temp2["1"] << endl;
    cout << "[2] = " << temp2["2"] << endl;
    cout << "[3] = " << temp2["3"] << endl;
   
}




__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail
http://personal.mail.yahoo.com/

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2001-07-17 23:13 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-07-17 23:13 map woes Michael Veksler
  -- strict thread matches above, loose matches on Subject: below --
2001-07-17 14:54 Srikanth Vedire

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).