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.