CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2009
    Posts
    1

    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!

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    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?

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  4. #4
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    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;
    }

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: seperate the string and store it in map

    You should probably add a check to verify that the c is always a comma.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured