STL map insert question

Discussion in 'C++' started by vin b, Jan 9, 2006.

  1. vin b

    vin b Guest

    Hi,

    How would I invoke a method on a reference to an STL object (STL map in
    this
    case)

    In this contrived example, I want to invoke the insert() method on
    localmap?

    Thanks, Vin

    #include <iostream>
    #include <string>
    #include <map>

    using namespace std;

    map < char, string >* localmap;

    void insmap( map < char, string > &m)
    {


    string s = "xxxxx";


    #if 0
    // normal implementation - this works , ... life would be too easy
    m[s[0]] = s;

    #endif





    localmap = &m;
    localmap['x']->insert(s); //doesn't work


    }


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

    // Here is the declaration of the map

    map < char, string > mymap;
    map < char, string >::iterator iter;

    // Here are example strings that I will add to an empty map

    string s1 = "This";
    string s2 = "is";
    string s3 = "an";
    string s4 = "example";
    string s5 = "string";

    // Here we add the strings to the map

    mymap[s1[0]] = s1;
    mymap[s2[0]] = s2;
    mymap[s3[0]] = s3;
    mymap[s4[0]] = s4;
    mymap[s5[0]] = s5;


    // Here is the output of the map in order from beginning to end
    insmap(mymap);

    cout << "This is the output of the map in order" << endl;

    for (iter = mymap.begin (); iter != mymap.end (); iter++)
    {
    cout << (*iter).
    first << " is the first character in the word " << iter->
    second << endl;
    }

    return 0;
    }
     
    vin b, Jan 9, 2006
    #1
    1. Advertising

  2. vin b wrote:
    > How would I invoke a method on a reference


    Actually, you have a _pointer_...

    > to an STL object (STL map in
    > this
    > case)
    >
    > In this contrived example, I want to invoke the insert() method on
    > localmap?


    For a pointer to an object, you always use -> to invoke a member.

    >
    > Thanks, Vin
    >
    > #include <iostream>
    > #include <string>
    > #include <map>
    >
    > using namespace std;
    >
    > map < char, string >* localmap;
    >
    > void insmap( map < char, string > &m)
    > {
    >
    >
    > string s = "xxxxx";
    >
    >
    > #if 0
    > // normal implementation - this works , ... life would be too easy
    > m[s[0]] = s;
    >
    > #endif
    >
    >
    >
    >
    >
    > localmap = &m;
    > localmap['x']->insert(s); //doesn't work


    If 'localmap' is a pointer, you need to dereference it first:

    (*localmap)['x'] = s;

    but in fact, if you _have_to_ use 'insert', the syntax is different.
    RTFM about 'insert' member and what argument it needs.

    localmap->insert(...

    >
    >
    > }
    >
    > [...irrelevant code snipped...]


    V
     
    Victor Bazarov, Jan 9, 2006
    #2
    1. Advertising

  3. vin b

    Daevaorn Guest

    change localmap['x']->insert(s); to
    (*localmap)['x'] = s;
     
    Daevaorn, Jan 9, 2006
    #3
  4. vin b

    vin b Guest

    thanks a million, .. this works ====> (*localmap)['x'] = s;
     
    vin b, Jan 9, 2006
    #4
  5. vin b

    red floyd Guest

    vin b wrote:
    > Hi,
    >
    > How would I invoke a method on a reference to an STL object (STL map in
    > this
    > case)
    >
    > In this contrived example, I want to invoke the insert() method on
    > localmap?
    >
    > Thanks, Vin
    >
    > #include <iostream>
    > #include <string>
    > #include <map>
    >
    > using namespace std;
    >
    > map < char, string >* localmap;
    > [redacted]


    At the risk of asking the obvious, is there a reason you're using a
    pointer to a map instead of the map itself?
     
    red floyd, Jan 9, 2006
    #5
  6. vin b

    vin b Guest

    good question, ...

    the example is a contrived way to represent some existing code i need
    to modify.

    in that case, a pointer to a map is passed into the init function of a
    handler class and is stored / used for all subsequent operations on
    that object (withing the scope of validity).

    given a choice, not the way i would design things but that's the way
    this framework is set up
     
    vin b, Jan 9, 2006
    #6
    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. Replies:
    2
    Views:
    658
    John Harrison
    Nov 17, 2005
  2. Replies:
    2
    Views:
    578
    klaus hoffmann
    Feb 22, 2006
  3. Replies:
    1
    Views:
    481
  4. Mike Copeland

    STL map insert Options

    Mike Copeland, Jul 5, 2011, in forum: C++
    Replies:
    8
    Views:
    927
    Jorgen Grahn
    Jul 19, 2011
  5. Luca Risolia

    STL map to STL vector

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

Share This Page