String to int/double conversion and parsing

Discussion in 'C++' started by kaede, Sep 3, 2004.

  1. kaede

    kaede Guest

    Hi all,

    Given the following data format: { "(1,2), "(2.5, 3.5)", .... }

    I would like to define a vector<string> to stored this data:

    v[0] = "(1,2)", v[1] = "(2.5, 3.5"), ....

    and would like to extract the integer/double string value to real
    int/double and use it in computation. What is the best way to do it?
    Currently, I am using C routine atoi() and wrote a litter paring
    function to extract the int/double value. I would like to know if
    there is a better way to do in using STL and maybe its algorithm?

    THanks,
    Kaede
     
    kaede, Sep 3, 2004
    #1
    1. Advertising

  2. kaede

    David Hilsee Guest

    "kaede" <> wrote in message
    news:...
    > Hi all,
    >
    > Given the following data format: { "(1,2), "(2.5, 3.5)", .... }
    >
    > I would like to define a vector<string> to stored this data:
    >
    > v[0] = "(1,2)", v[1] = "(2.5, 3.5"), ....
    >
    > and would like to extract the integer/double string value to real
    > int/double and use it in computation. What is the best way to do it?
    > Currently, I am using C routine atoi() and wrote a litter paring
    > function to extract the int/double value. I would like to know if
    > there is a better way to do in using STL and maybe its algorithm?


    Ditch atoi() and use something that has more reasonable behavior when passed
    invalid input, like a stringstream. Other than that, the best solution
    would probably involve iterating over the std::vector and parsing each
    element (e.g. std::pair<double,double> parseDoubles(const std::string& s)).
    You might be able to come up with something that uses <algorithm>, like
    std::transform, but the code would probably look unnatural.

    --
    David Hilsee
     
    David Hilsee, Sep 4, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sydex
    Replies:
    12
    Views:
    6,647
    Victor Bazarov
    Feb 17, 2005
  2. Schnoffos
    Replies:
    2
    Views:
    1,252
    Martien Verbruggen
    Jun 27, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,711
    Old Wolf
    Jan 20, 2004
  4. el_boricua
    Replies:
    7
    Views:
    392
    elviin
    Aug 2, 2005
  5. Shriramana Sharma
    Replies:
    8
    Views:
    295
    Gerhard Fiedler
    Jun 18, 2013
Loading...

Share This Page