Thread: maping!

  1. #1
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117

    maping!

    Im making a little app for myself and inside it i use map<string,float>

    Is there a way to output all value inside map even if i dont know the names of the values?

    (the user input the name of each value)

    ?

    Luigi

  2. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>Is there a way to output all value inside map even if i dont know the names of the values?
    Sure! :-)
    Code:
    void show_me(map<string, float>& mymap)
    {
      map<string, float>::const_iterator iter;
    
      for (iter = mymap.begin(); iter != mymap.end(); ++iter)
      {
        cout<< iter->first <<' '<< iter->second <<endl;
      }
    }
    *Cela*

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    how does this map<string,float> work?

  4. #4
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>how does this map<string,float> work?
    It's like a hash table, the first part of the map, in this case string, is the key and the second part is the value. You use the key to search for the value by saying
    Code:
    mymap["blah"] = 123.456; // New entry
    
    string key = "blah";
    
    cout<< mymap[key] <<endl; // Prints 123.456
    A map probably isn't implemented as an actual hash table, but that doesn't change how you use it :-)
    *Cela*

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    when is this usefull to use?

  6. #6
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>when is this usefull to use?
    When you need a dynamic data structure that needs something other than integer indicies, unlike a vector, and can be searched quickly, unlike a list. :-)
    *Cela*

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    never seen this in a book b4 can u show uss another example please?

  8. #8
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. maping an unsigned char ofer a structure
    By spank in forum C Programming
    Replies: 6
    Last Post: 11-08-2007, 10:36 AM