seperate the string and store it in map
I am not sure how this works in VC6 as i am a new programmer...please help.
I have a string
"0.28813,0.30125,0.31813,0.45375,0.61313,0.84063,1.00938,1.16375,1.26"
i want to store each of the number into a map of < count, double>
Can someone tell me how to search the commas, read each of the numbers, store them as a double and push to store in a map?
Thank you!
Re: seperate the string and store it in map
You could used strtok and atof to split the string up and convert the pieces to double.
What do you mean by count as the map key?
Re: seperate the string and store it in map
Why are you using VC6? That's a dinosaur.
It's not clear what the "count" in your map is intended to be.
Re: seperate the string and store it in map
I also do not understand what the "count" is. To get the
double values: place the string into a stringstream and let
it do the parsing:
Code:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
string s = "0.28813,0.30125,0.31813,0.45375,0.61313,0.84063,1.00938,1.16375,1.26";
double value;
char c;
stringstream ss(s);
while (ss >> value)
{
cout << value << "\n";
ss >> c;
}
return 0;
}
Re: seperate the string and store it in map
You should probably add a check to verify that the c is always a comma.