getting reference to a STL map element

Discussion in 'C++' started by Jan, Dec 12, 2005.

  1. Jan

    Jan Guest

    Hi there,
    yes im a STL newbie, but thanks for replying.
    I want to get a reference to an item included in a STL map. e.g.:

    map<type1, type2> myMap;
    .....
    type2& aName = myMap[type1];
    .....

    what's wrong with this ;-) ? I don't want to have a copy of type2. only
    a
    reference to it (and if type1 is not in the map, it should be
    automatically be
    created)

    thanks,
    Jan
     
    Jan, Dec 12, 2005
    #1
    1. Advertising

  2. Jan wrote:
    > yes im a STL newbie, but thanks for replying.
    > I want to get a reference to an item included in a STL map. e.g.:
    >
    > map<type1, type2> myMap;
    > ....
    > type2& aName = myMap[type1];
    > ....
    >
    > what's wrong with this ;-) ?


    You cannot use 'type1' in the expression, that's what's wrong. Read
    the FAQ, especially 5.8.

    > I don't want to have a copy of type2. only
    > a
    > reference to it (and if type1 is not in the map, it should be
    > automatically be
    > created)


    Try your best to write your program, then if you get errors, post back
    with them (read the suggested FAQ).

    V
     
    Victor Bazarov, Dec 12, 2005
    #2
    1. Advertising

  3. Jan

    Marcus Kwok Guest

    Jan <> wrote:
    > Hi there,
    > yes im a STL newbie, but thanks for replying.
    > I want to get a reference to an item included in a STL map. e.g.:
    >
    > map<type1, type2> myMap;
    > ....
    > type2& aName = myMap[type1];
    > ....
    >
    > what's wrong with this ;-) ?


    You cannot index into a map with just a type. For example, the
    following works for me:

    #include <iostream>
    #include <map>

    typedef int type1;
    typedef char type2;

    int main()
    {
    std::map<type1, type2> myMap;
    type1 key = 5;

    // use explicit key
    type2& aName = myMap[key];
    aName = 'c';
    std::cout << "myMap[key] = " << myMap[key] << '\n';

    // use default value of type1 as the key (in this case, 0)
    type2& anotherName = myMap[type1()];
    anotherName = 'b';
    std::cout << "myMap[type1()] = " << myMap[type1()] << '\n';

    return 0;
    }



    > I don't want to have a copy of type2. only
    > a
    > reference to it (and if type1 is not in the map, it should be
    > automatically be
    > created)


    --
    Marcus Kwok
     
    Marcus Kwok, Dec 12, 2005
    #3
    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. Marcus
    Replies:
    2
    Views:
    622
    Marcus
    Dec 9, 2005
  2. Steve Edwards
    Replies:
    11
    Views:
    25,663
    Mike Wahler
    Feb 23, 2006
  3. Replies:
    2
    Views:
    575
    klaus hoffmann
    Feb 22, 2006
  4. kl
    Replies:
    7
    Views:
    1,353
    James Kanze
    Jan 1, 2008
  5. Luca Risolia

    STL map to STL vector

    Luca Risolia, Jan 13, 2014, in forum: C++
    Replies:
    32
    Views:
    437
    Seungbeom Kim
    Jan 18, 2014
Loading...

Share This Page