Std Map .. Help

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

    Std Map .. Help

    Hi,

    I'm getting started with STL, and am stuck at creating a map
    container. I checked one of the texts and found a code in there. To
    make it simple, i wrote the following:

    #include <iostream.h>
    #include <string.h>
    #include <map>

    typedef std::map<string ,string,std::le ss<string>> mymap;

    int main()
    {
    mymap somemap;
    return 0;
    }


    This gives me 6 error messages, an a warning. Could anyone point me
    the mistake....i've been to grasp the topic for two days now!

    Initially i thought i might not be using string properly, so i changed
    the code typedef to

    typedef std::map<int,in t, std::less<int>> mymap;

    This give me an error saying :
    C:\Documents and Settings\Ash\De sktop\Maps\Muli Map\Main.cpp(4) : error
    C2146: syntax error : missing ',' before identifier 'mymap'
    C:\Documents and Settings\Ash\De sktop\Maps\Muli Map\Main.cpp(4) : :
    error C2065: 'mymap' : undeclared identifier
    C:\Documents and Settings\Ash\De sktop\Maps\Muli Map\Main.cpp(4) : error
    C2143: syntax error : missing '>' before ';'
    C:\Documents and C:\Documents and
    Settings\Ash\De sktop\Maps\Muli Map\Main.cpp(4) : warning C4091:
    'typedef ' : ignored on left of 'class std::map' when no variable is
    declared


    Thanks,

    Ash
  • John Harrison

    #2
    Re: Std Map .. Help


    <ash> wrote in message news:6hva805ff5 l984slf2m9ogsjo 8i5c2f7k5@4ax.c om...[color=blue]
    > Hi,
    >
    > I'm getting started with STL, and am stuck at creating a map
    > container. I checked one of the texts and found a code in there. To
    > make it simple, i wrote the following:
    >
    > #include <iostream.h>[/color]

    Non-standard header file, use <iostream> (without a .h)
    [color=blue]
    > #include <string.h>[/color]

    This is a valid header file, but it doesn't declare the std::string class
    which is what you are thinking it does, instead it declares the C string
    handling routines. Use <string> instead (again without a .h)
    [color=blue]
    > #include <map>
    >
    > typedef std::map<string ,string,std::le ss<string>> mymap;[/color]

    std::less is unecessary, but string is wrong, it should be std::string. Try
    this

    typedef std::map<std::s tring,std::stri ng> mymap;
    [color=blue]
    >
    > int main()
    > {
    > mymap somemap;
    > return 0;
    > }
    >
    >
    > This gives me 6 error messages, an a warning. Could anyone point me
    > the mistake....i've been to grasp the topic for two days now![/color]

    Don't know what book you are reading, doesn't it have any syntactically
    valid programs in it?

    You need to use the correct header files (no C++ header files have .h in
    them)
    You need to remember std::
    You should forget about std::less (at least for now).

    john


    Comment

    • Artie Gold

      #3
      Re: Std Map .. Help

      ash wrote:[color=blue]
      > Hi,
      >
      > I'm getting started with STL, and am stuck at creating a map
      > container. I checked one of the texts and found a code in there. To
      > make it simple, i wrote the following:
      >
      > #include <iostream.h>[/color]
      #include <iostream>[color=blue]
      > #include <string.h>[/color]
      #include <string>

      [<string.h> is a C header that contains the prototypes for things like
      strcmp(), strcpy(), and the like -- *not* C++ std::string]
      [color=blue]
      > #include <map>
      >
      > typedef std::map<string ,string,std::le ss<string>> mymap;[/color]
      typedef std::map<std::s tring,
      std::string,
      std::less<std:: string> > mymap;

      [the space between the enclosing `>'s is necessary][color=blue]
      >
      > int main()
      > {
      > mymap somemap;
      > return 0;
      > }
      >
      >
      > This gives me 6 error messages, an a warning. Could anyone point me
      > the mistake....i've been to grasp the topic for two days now!
      >
      > Initially i thought i might not be using string properly, so i changed
      > the code typedef to
      >
      > typedef std::map<int,in t, std::less<int>> mymap;
      >
      > This give me an error saying :
      > C:\Documents and Settings\Ash\De sktop\Maps\Muli Map\Main.cpp(4) : error
      > C2146: syntax error : missing ',' before identifier 'mymap'
      > C:\Documents and Settings\Ash\De sktop\Maps\Muli Map\Main.cpp(4) : :
      > error C2065: 'mymap' : undeclared identifier
      > C:\Documents and Settings\Ash\De sktop\Maps\Muli Map\Main.cpp(4) : error
      > C2143: syntax error : missing '>' before ';'
      > C:\Documents and C:\Documents and
      > Settings\Ash\De sktop\Maps\Muli Map\Main.cpp(4) : warning C4091:
      > 'typedef ' : ignored on left of 'class std::map' when no variable is
      > declared
      >[/color]

      HTH,
      --ag

      --
      Artie Gold -- Austin, Texas

      Comment

      Working...