map of name/vector<float> access?

This is a discussion on map of name/vector<float> access? within the C++ Programming forums, part of the General Programming Boards category; I am declaring a type as typdef std::map<string,std::vector<float>> StringFloatsMap its purpose is to hold a name and vector of multiple ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    450

    map of name/vector<float> access?

    I am declaring a type as
    typdef std::map<string,std::vector<float>> StringFloatsMap

    its purpose is to hold a name and vector of multiple values, which I will use to determine the sum , mean for each name and for all names and values.

    my question is how to access individul vector values in the map would it be :
    name_values[name_key][vector_index]=value;

    my second question:
    Since I will not be keeping track of the number of values per name, I want to create an iterator to each vector to step through the values for calculation of sum and mean for each name here is kind of what I had in mind.
    // name_key_index is iterator to map
    vector::iterator value_index = name_value[name_key_index->first].begin();

    how would I do this properly and can I use the iterator in float division ie
    mean=sum/value_index;
    Last edited by curlious; 12-17-2003 at 11:51 PM.

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    I don't think my approach is going to work. After struggling for the last hour to get insertion right I have to change my initial conception of the data structure.

    From what I have been reading you must insert key, value pairs in a map and if the key already exists it will return an error upon an attempt to insert.

    I have made an attempt at getting an iterator the element at key name so that I could use the second element of the iterator which should be a vector and push the value onto it. Here is a code snippet
    Code:
    #include <iostream>
    #include <string>
    #include <map>
    #include <vector>
    using namespace std;
    
    
    int main()
    {
      typedef vector<float> FltVect;
      typedef map< string, FltVect > StringFloatsMap;
    
      StringFloatsMap name_value;
     
      // Give instructions:
      cout << "Enter a sequence of name value pairs (seperated by a space)."<<endl;
      cout << "Enter: END value to stop input and calculate sums and means."<<endl;
      cout << endl;
    
      string name_key;
      float temp_value;
      float sum_all_values;
      int count_values;
      
      cout << "Enter name value:";
      cin >> name_key >> temp_value;
      cout << endl;
    
      StringFloatsMap::iterator name_key_index;
    
      while (name_key != "END"){
        name_value.insert(name_key);    
        name_key_index=name_value.find(name_key);
        name_key_index->second.push_back(temp_value);
            
        sum_all_values += temp_value;
        ++count_values;
        
        cout << "Enter name value:";
        cin >> name_key >> temp_value;
        cout << endl;
      }
    Any suggestions on this idea which isn't working.

    Solved the problem by simply:
    name_value[name_key].push_back(temp_value);
    which will push onto the back of the value and or insert the name.
    Last edited by curlious; 12-18-2003 at 07:59 AM.

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Took me awhile before I realized that you solved your problem already.

    I did want to add that if you have lots of keys and only a few values per key, then a multimap might be worth considering (if you haven't already). With a map of vectors, each entry in the map has a vector with a certain default initial space reserved. If you are using only a small part of that space and your map has lots of keys, then there is a lot of unused memory being wasted.

    Of course, with a multimap it wouldn't be as easy to do operations on the list of values for a specific key. I assume with a map of vectors you would just use different algorithms to find the sum, average, count, etc, for the vector at each key. I don't think its that easy with a multimap unless you know the key beforehand and you can do an equal_range.

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Thanks for looking at my post. I did consider multimaps, but with reserve I think I can eliminate some of the wasted space. Also with a single unique key name I save space over a multimap. It turns out simpler to manipulate the map for the purposes of this exercise as well. This post may help other beginners expermenting with aggeragate data structures from the standard template library.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding a Map to a program
    By Shogun32 in forum C++ Programming
    Replies: 1
    Last Post: 05-04-2009, 09:42 AM
  2. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  3. Creating a map engine.
    By suzakugaiden in forum Game Programming
    Replies: 11
    Last Post: 06-21-2005, 05:06 AM
  4. How do I access data in a STL Map?
    By Dragoon_42 in forum C++ Programming
    Replies: 1
    Last Post: 04-09-2004, 07:32 PM
  5. Searching STL Map Inside STL Map Object :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 11-14-2002, 08:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21