CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    May 2000
    Location
    England
    Posts
    574

    How to use an STL map ???

    Hi
    Has somebody got an easy example of how to use a STL map ?
    really simple one to get me started please

    thanks

    P

  2. #2
    Join Date
    Aug 2002
    Location
    Cluj-Napoca,Romania
    Posts
    3,496
    Code:
    #include <map>
    using namespace std;
    
    typedef map< int, float> TMapInt2Float;
    
    void main()
    {
        TMapInt2Float mapTest;
    
        mapTest.insert(TMapInt2Float::value_type(0,1.1));
        mapTest.insert(TMapInt2Float::value_type(1,1.2));
        mapTest.insert(TMapInt2Float::value_type(2,2.3));
        mapTest.insert(TMapInt2Float::value_type(3,3.5));
        mapTest.insert(TMapInt2Float::value_type(4,3.4));
        mapTest.insert(TMapInt2Float::value_type(5,7.8));
    
        //find a value
        TMapInt2Float::iterator it = mapTest( 2);
        if( it != mapTest.end())
        {
            float f = it->second;
        }
    
        //iterate the map
       for( it = mapTest.begin(); it != mapTest.end(); ++it)
       {
           int key       = it->first; 
           float value = it->second;
       }
    }

    Note: the example is written in Codeguru's message editor. it might contain errors.
    Har Har

  3. #3
    Join Date
    Aug 2002
    Location
    Cluj-Napoca,Romania
    Posts
    3,496
    One more thing. you should check the Microsoft Developer Network ( MSDN) as it contains a lot of samples and documentation for STl ( at least for beginner level).
    Har Har

  4. #4
    Join Date
    May 2000
    Location
    England
    Posts
    574
    Fantastic !

    Thanks PadexArt just what i needed .

    have a good day


  5. #5
    Join Date
    May 2000
    Location
    England
    Posts
    574
    Unfotunately some of your code doesn't work when i try to use it ?
    i get an exception error from
    float f = it->second

    and also an initialising error from

    TMapInt2Float::iterator it = maptest( 2);

    any ideas ?
    also i cannot find any help in the msdn to do with STL maps ??

    i have created the map ok i just want to know access a certain value by its key ...

    thanks

    P

  6. #6
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    Well this line :

    Code:
    TMapInt2Float::iterator it = mapTest( 2);
    should be :

    Code:
    TMapInt2Float::iterator it = mapTest.find(2);

  7. #7
    Join Date
    May 2000
    Location
    England
    Posts
    574
    cool !

  8. #8
    Join Date
    Jan 2001
    Posts
    145
    Btw, i am wondering what's the complexity for STL's map?

  9. #9
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    for find() , the complexity is O(logn)

    Usually it is implemented as a red-black tree.

  10. #10
    Join Date
    Jan 2001
    Posts
    145
    Thanks Philip,

    In that case, i guess insert() and erase() will also take O(log(n)) if it is implemented with a red-black tree.

    It's a pity and i dont understand why in C++'s STL I can not find a structure which takes the complexity of O(1), like a hashtable.


    cheers

  11. #11
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    Just about every STL implementation has a hash_map
    container, its just not in the standard yet (but it
    certainly will be in the next iteration of the standard).

    For example, I think that VC.net has a hash_map. I know
    that the upgrade from Dinkumware for VC 5&6 has one.

    For a standard map() , inserting is O(logn) ... there is
    a version of insert() where you can give a "hint" on
    where to start the search, but to be honest, I have
    never seen a good pratical example of this.

    Remove is O(1) if you have the iterator position that
    you want to remove. But generally, you would need
    to do a find() to get that iterator, which is O(logn).

  12. #12
    Join Date
    Jan 2001
    Posts
    145
    OK. Now i understood why i only heard the hash_map but can not find it. Thanks philip.

    But if it still need the find(), which is on O(log(n)), to locate the items, then it is not a big change, and then what the "hash" does in this case?

  13. #13
    Join Date
    Sep 2003
    Location
    STL LAND
    Posts
    15
    MSDN is a terrible source for info on the STL. There are mistakes in the documentation!!!

    http://www.sgi.com/tech/stl/
    DEFENDER OF ALL THINGS STL!!!

  14. #14
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266
    Originally posted by STL MAN
    MSDN is a terrible source for info on the STL. There are mistakes in the documentation!!!

    http://www.sgi.com/tech/stl/
    I don't know who wrote the STL documentation provided with VC but I doubt it is Microsoft. Yet if there are mistakes, they want to know about it. They will fix it if you report the mistakes.

    I have not had success getting answers using any STL documentation available using the internet, and i have tried using the SGI documentation.

    Whoever you are, you seem to be here only to make trouble.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  15. #15
    Join Date
    Sep 2003
    Location
    STL LAND
    Posts
    15
    STL MAN does not know how providing a link for a site devoted to
    the STL is causing trouble. It is true that there are no really good
    sites devoted to the STL, but probably the worst is MSDN.
    Ultimately the best option is to invest in a good book.
    The C++ Standard Library by Nicolai Josuttis
    DEFENDER OF ALL THINGS STL!!!

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured