STL map insert question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • vin b

    #1

    STL map insert question

    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;
    }

  • Victor Bazarov

    #2
    Re: STL map insert question

    vin b wrote:[color=blue]
    > How would I invoke a method on a reference[/color]

    Actually, you have a _pointer_...
    [color=blue]
    > to an STL object (STL map in
    > this
    > case)
    >
    > In this contrived example, I want to invoke the insert() method on
    > localmap?[/color]

    For a pointer to an object, you always use -> to invoke a member.
    [color=blue]
    >
    > 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[/color]

    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(...
    [color=blue]
    >
    >
    > }
    >
    > [...irrelevant code snipped...][/color]

    V

    Comment

    • Daevaorn

      #3
      Re: STL map insert question

      change localmap['x']->insert(s); to
      (*localmap)['x'] = s;

      Comment

      • vin b

        #4
        Re: STL map insert question

        thanks a million, .. this works ====> (*localmap)['x'] = s;

        Comment

        • red floyd

          #5
          Re: STL map insert question

          vin b wrote:[color=blue]
          > 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][/color]

          At the risk of asking the obvious, is there a reason you're using a
          pointer to a map instead of the map itself?

          Comment

          • vin b

            #6
            Re: STL map insert question

            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

            Comment

            Working...