behavior of a std::map I don't understand...

Discussion in 'C++' started by Heck, Nov 15, 2007.

  1. Heck

    Heck Guest

    I'm using Visual Studio 2005, but i don't think that's (primarily)
    where I've gone wrong.

    I was experimenting, trying, by the way, to move into writing my own
    iterator so that I can learn, ultimately, to write templates:
    std::map< int, char [12]> cm;
    char str0[12] = {'f','i','r','s','t',' ','t','e','x','t','\0'};

    cm[0] = str0;

    The compiler says about that assignment,
    left operand must be l-value

    What does this mean, what's happened here?

    It compiles without error when the value arg in the map is instead a
    char or a char *.
    std::map< int, char *> cm;

    Thanks for your help and knowledge.

    Harold
    Heck, Nov 15, 2007
    #1
    1. Advertising

  2. Heck

    red floyd Guest

    Heck wrote:
    > I'm using Visual Studio 2005, but i don't think that's (primarily)
    > where I've gone wrong.
    >
    > I was experimenting, trying, by the way, to move into writing my own
    > iterator so that I can learn, ultimately, to write templates:
    > std::map< int, char [12]> cm;
    > char str0[12] = {'f','i','r','s','t',' ','t','e','x','t','\0'};
    >
    > cm[0] = str0;
    >
    > The compiler says about that assignment,
    > left operand must be l-value
    >
    > What does this mean, what's happened here?
    >
    > It compiles without error when the value arg in the map is instead a
    > char or a char *.
    > std::map< int, char *> cm;
    >


    Arrays are not assignable.
    red floyd, Nov 15, 2007
    #2
    1. Advertising

  3. On 2007-11-16 00:12, Heck wrote:
    > I'm using Visual Studio 2005, but i don't think that's (primarily)
    > where I've gone wrong.
    >
    > I was experimenting, trying, by the way, to move into writing my own
    > iterator so that I can learn, ultimately, to write templates:
    > std::map< int, char [12]> cm;
    > char str0[12] = {'f','i','r','s','t',' ','t','e','x','t','\0'};
    >
    > cm[0] = str0;
    >
    > The compiler says about that assignment,
    > left operand must be l-value
    >
    > What does this mean, what's happened here?
    >
    > It compiles without error when the value arg in the map is instead a
    > char or a char *.
    > std::map< int, char *> cm;


    You can not store an array in a map (or any other collection for that
    matter).

    --
    Erik Wikström
    =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=, Nov 16, 2007
    #3
  4. Heck wrote:
    > char str0[12] = {'f','i','r','s','t',' ','t','e','x','t','\0'};


    Btw, you can save typing by simply writing:

    char str0[12] = "first text";
    Juha Nieminen, Nov 16, 2007
    #4
  5. Heck

    LR Guest

    Erik Wikström wrote:

    > You can not store an array in a map (or any other collection for that
    > matter).


    Sorry, I don't understand what you mean by collection. Could you please
    expand on that?

    TIA

    LR
    LR, Nov 16, 2007
    #5
  6. LR wrote:
    > Erik Wikström wrote:
    >
    >> You can not store an array in a map (or any other collection for that
    >> matter).

    >
    > Sorry, I don't understand what you mean by collection. Could you
    > please expand on that?


    Erik will hopefully give his POV, but AFAICT, Erik meant any standard
    container by "any other collection".

    Arrays do not satisfy the requirements for the contained items. The
    main two are Assignable and Copy-constructible. Arrays are neither.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Nov 16, 2007
    #6
  7. Heck

    Guest

    On Nov 15, 11:41 pm, Erik Wikström <> wrote:
    > On 2007-11-16 00:12, Heck wrote:
    >
    >
    >
    > > I'm using Visual Studio 2005, but i don't think that's (primarily)
    > > where I've gone wrong.

    >
    > > I was experimenting, trying, by the way, to move into writing my own
    > > iterator so that I can learn, ultimately, to write templates:
    > > std::map< int, char [12]> cm;
    > > char str0[12] = {'f','i','r','s','t',' ','t','e','x','t','\0'};

    >
    > > cm[0] = str0;

    >
    > > The compiler says about that assignment,
    > > left operand must be l-value

    >
    > > What does this mean, what's happened here?

    >
    > > It compiles without error when the value arg in the map is instead a
    > > char or a char *.
    > > std::map< int, char *> cm;

    >
    > You can not store an array in a map (or any other collection for that
    > matter).
    >
    > --
    > Erik Wikström


    Wrap your array in a class and define the assignment operator

    Jeff

    http://theschmitzer.blogspot.com
    , Nov 16, 2007
    #7
  8. On 2007-11-16 16:58, LR wrote:
    > Erik Wikström wrote:
    >
    >> You can not store an array in a map (or any other collection for that
    >> matter).

    >
    > Sorry, I don't understand what you mean by collection. Could you please
    > expand on that?


    Sorry about that, I have been programming to much VBA (ugh!) lately.
    Just like Victor guessed I meant a standard container.

    --
    Erik Wikström
    =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=, Nov 16, 2007
    #8
  9. Heck

    Heck Guest

    Re: Re: behavior of a std::map I don't understand...

    Erik Wikström!
    >On 2007-11-16 00:12, Heck wrote:
    >> I'm using Visual Studio 2005, but i don't think that's (primarily)
    >> where I've gone wrong.
    >>
    >> I was experimenting, trying, by the way, to move into writing my own
    >> iterator so that I can learn, ultimately, to write templates:
    >> std::map< int, char [12]> cm;
    >> char str0[12] = {'f','i','r','s','t',' ','t','e','x','t','\0'};
    >>
    >> cm[0] = str0;
    >>
    >> The compiler says about that assignment,
    >> left operand must be l-value
    >>
    >> What does this mean, what's happened here?
    >>
    >> It compiles without error when the value arg in the map is instead a
    >> char or a char *.
    >> std::map< int, char *> cm;

    >
    >You can not store an array in a map (or any other collection for that
    >matter).


    OK, thanks (and thank you all). Is your answer consistent with the
    compiler telling me the map element, in this case, cm[0], is not an
    l-value? It is an l-value, isn't it? It's the array that's not an
    r-value, right?
    Heck, Nov 18, 2007
    #9
  10. Heck

    Heck Guest

    Re: Re: behavior of a std::map I don't understand...

    !
    >On Nov 15, 11:41 pm, Erik Wikström <> wrote:
    >> On 2007-11-16 00:12, Heck wrote:
    >>
    >>
    >>
    >> > I'm using Visual Studio 2005, but i don't think that's (primarily)
    >> > where I've gone wrong.

    >>
    >> > I was experimenting, trying, by the way, to move into writing my own
    >> > iterator so that I can learn, ultimately, to write templates:
    >> > std::map< int, char [12]> cm;
    >> > char str0[12] = {'f','i','r','s','t',' ','t','e','x','t','\0'};

    >>
    >> > cm[0] = str0;

    >>
    >> > The compiler says about that assignment,
    >> > left operand must be l-value

    >>
    >> > What does this mean, what's happened here?

    >>
    >> > It compiles without error when the value arg in the map is instead a
    >> > char or a char *.
    >> > std::map< int, char *> cm;

    >>
    >> You can not store an array in a map (or any other collection for that
    >> matter).
    >>
    >> --
    >> Erik Wikström

    >
    >Wrap your array in a class and define the assignment operator


    I'm getting to that, yes. Thanks very much.
    Heck, Nov 18, 2007
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Jansson
    Replies:
    5
    Views:
    6,254
    Ivan Vecerina
    Mar 17, 2005
  2. camotito

    QuickSort . I don't understand this behavior

    camotito, Apr 24, 2006, in forum: C Programming
    Replies:
    2
    Views:
    272
    Michael Mair
    Apr 24, 2006
  3. Replies:
    1
    Views:
    401
    red floyd
    Dec 21, 2008
  4. Thomas J. Gritzan
    Replies:
    6
    Views:
    991
    James Kanze
    Dec 22, 2008
  5. James Kanze
    Replies:
    0
    Views:
    1,974
    James Kanze
    Dec 21, 2008
Loading...

Share This Page