Thread: Using the STL map for one's own class

  1. #1
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141

    Using the STL map for one's own class

    What operations need to be defined to make a map (or preferably, hashmap), of <const string, ownClass*>? I know something like a Comparator in java must be written to test for equality and to order them.

    Are there any websites with a tutorial of doing such? I found this to be a good reference

    http://www.yrl.co.uk/~phil/stl/stl.htmlx

    But it did not go into any detail about what is needed to have non primative as the values for the map.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    If I understand the question you're asking for associative containers. And here's what I dug up from MSDN:

    6. Associative Containers
    Associative containers store and retrieve their objects according to an association with each other. In other words, the data is stored and retrieved in a sorted order. Therefore, associative containers rely heavily upon comparison operators. There are four different associative containers defined in the STL. They are: set, multiset, map and multimap.
    6.1 Set and Multiset
    Code:
    #include <set>
    set<TYPE, PREDICATE, ALLOCATOR>
    A set is optimized for fast associative lookup. For lookup purposes, objects are matched using operator==. Objects are ordered within the set according to the user-defined comparator object (the second template argument). The comparator object is typically "less<TYPE>", which causes the data to be sorted in ascending order. Each object in a set has a unique value, as duplicate objects are not allowed. A multiset is similar to a set, except that a multiset can contain objects with duplicate values.
    The following function declares a set of ints, fills it with 10 random values, then prints them:
    Code:
         typedef set<int, less<int>, allocator<int> > INTSET;
    
         void somefunct()
         {
                    INTSET mySet;   // declare a set of ints
                    INTSET::iterator mySetPtr;      // and an iterator for the set
                    int tmpVal;
    
                    srand(13);      // seed the rand function
              // fill the set with 10 random ints - print them as they are added.
                    for(int i = 0; i < 9; i++){
                            tmpVal = rand();
                            cout << tmpVal << ", ";
                            mySet.insert(tmpVal);
                    }
                    cout << endl;
    
                    // print the contents of the set (note the values are sorted)
                    for(mySetPtr = mySet.begin(); mySetPtr != mySet.end();
                        mySetPtr++)
                            cout << *mySetPtr << ", ";
    
                    cout << endl;
         }
    Output of somefunct:
    81, 16376, 24096, 20348, 11872, 30076, 16059, 26999, 28493,
    81, 11872, 16059, 16376, 20348, 24096, 26999, 28493, 30076,


    See APPENDIX B for the set and multiset class definitions.
    See APPENDIX C for a table of set and multiset methods.
    See APPENDIX D for an explanation of the allocator class (third template parameter above).


    6.2 Map and Multimap
    Code:
    #include <map>
    map<KEY, TYPE, PREDICATE, ALLOCATOR>
    A map holds a set of ordered key/value pairs. The pairs are ordered by key, based upon the user-defined comparator object (3rd template parameter). A map defines a one-to-one relationship, allowing only one value to be associated with a particular key; a multimap defines a one-to-many relationship, allowing many values to be associated with a particular key.
    The following declares a map of ints to strings, fills it with 10 key/value pairs and prints them:
    Code:
         typedef map<int, string, less<int>, allocator<string> > INT2STRING;
         void somefunct()
         {
                     INT2STRING myMap;
                     INT2STRING::iterator i2sPtr;
    
          // Fill myMap with the digits 9 - 0 in reverse order, 
              // each mapped to its character string counterpart
                 myMap.insert(INT2STRING::value_type(9,"Nine"));
                 myMap.insert(INT2STRING::value_type(8,"Eight"));
                 myMap.insert(INT2STRING::value_type(7,"Seven"));
                 myMap.insert(INT2STRING::value_type(6,"Six"));
                 myMap.insert(INT2STRING::value_type(5,"Five"));
                 myMap.insert(INT2STRING::value_type(4,"Four"));
                 myMap.insert(INT2STRING::value_type(3,"Three"));
                 myMap.insert(INT2STRING::value_type(2,"Two"));
                 myMap.insert(INT2STRING::value_type(1,"One"));
                 myMap.insert(INT2STRING::value_type(0,"Zero"));
    
              // Now print the pairs - note they are now sorted
              // into ascending order...
                     for(i2sPtr = myMap.begin(); i2sPtr != myMap.end(); i2sPtr++)
                             cout << "(" << (*i2sPtr).first << ", "
                                  << (*i2sPtr).second << “)” << endl;
      }
    Output of somefunct:
    (0, Zero)
    (1, One)
    (2, Two)
    (3, Three)
    (4, Four)
    (5, Five)
    (6, Six)
    (7, Seven)
    (8, Eight)
    (9, Nine)

    See APPENDIX B for the map and multimap class definitions
    See APPENDIX C for a table of map and multimap methods
    See APPENDIX D for an explanation of the allocator class (fourth template parameter above)

    I hope this is what you were looking for.

  3. #3
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    I wanted info on map not set .

    Still, I'll check MSDN for info.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. matrix class without STL
    By Avraham in forum C++ Programming
    Replies: 1
    Last Post: 04-29-2009, 05:41 AM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Need help to build network class
    By weeb0 in forum C++ Programming
    Replies: 0
    Last Post: 02-01-2006, 11:33 AM
  4. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM