How to read multiple doubles that are on a space-delimited string in C ++?

I need to read a line like:

string str = "3 - 90.2 85.54 93.0";
      

Run codeHide result


... in this format. The first number shows the number of doubles, followed by "=", and then many pairs, separated by spaces). I know how to read the first number as an int to find the number of pairs. However, what worries me is reading doubles. I tried using the stod function.

string str = "90.2 85.54 93.0";
size_t sz;

double x = stod(str,&sz);
double y = stod(str.substr(sz));
double z = stod(str.substr(sz));
      

Run codeHide result


However, this can only read up to two doubles, since str.substr (sz) will read to the end of the line, and doubles can be with different decimal places (it could be 89.54 or 89 or 89.6), so I don't can use substr as it requires me to know how many characters to read from the starting index.

Is there a way to read paired numbers with an unknown number of decimal digits in these unknown counts? Is there any other method like substr to read up to a specific index or character? I just want them to be read in one pass. I can later save it to a double array (which I know how to do).

+3


source to share


6 answers


you can use stringstream



string str = "3 - 90.2 85.54 93.0";
stringstream ss(str);
int num;   ss >> num; //read 3
char c;    ss >> c;    // read "-"
double d1; ss >> d1;   // read 1
double d2; ss >> d2;   // read 2
double d2; ss >> d2;   // read 3

      

+2


source


Use istream_iterator

s.

std::vector<double> xyzzy{std::istream_iterator<double>{std::istringstream{str}}, std::istream_iterator{}};

      



Note that streams do not have move constructors in GCC, so you need to declare yours istringstream

and pass a reference to it if you are using GCC, for example:

std::istringstream temp{str};
std::vector<double> xyzzy{std::istream_iterator<double>{temp}, std::istream_iterator{}};

      

+1


source


The strtok () function is very useful for parsing things from larger strings. Once you have the pch pointing to each double, then convert it using the atof () call.

  char str[] ="- This, a sample string.";
  char * pch;
  printf ("Splitting string \"%s\" into tokens:\n",str);
  pch = strtok (str," ");
  while (pch != NULL)
  {
     printf ("%s\n",pch);
     pch = strtok (NULL, " ");
  }

      

0


source


You can just repeat the code:

double x = stod(str, &sz);
str = str.substr(sz);
double y = stod(str, &sz);
str = str.substr(sz);
double z = stod(str, &sz);
str = str.substr(sz);
double x1 = stod(str, &sz);

      

See the documentation:

double stod(const string &  str, size_t * idx = 0);

      

  • str

    : a String object representing a floating point number.
  • idx

    : Pointer to an object of type size_t, the value of which is specified by the function parameter to the position of the next character str

    after the numerical value. This parameter can also be a null pointer, in which case is not used.
0


source


You can even split the line using a while loop without limiting the number of floats in the line, as this will end at the end of the line:

int i=0, p=0;
float fdigit=0;
char string[]="9.0 5.4 2.6", floats[10];
while (floats[i]!='\0')
{
while (floats[i]!=' ')
{
floats[p]=floats[i];
p++;
i++;
floats[p]='\0';
fdigit=atof(floats);
cout<<fdigit<<"\n";
}
if (floats[i]==' ')
i++;
}

      

Also don't forget to put stdlib.h since atof is in stdlib.h library, atof () just converts string floats to float input, hope this helps u if you have any questions then ask me feel free to ask me, brother ...

EDIT: Also don't forget to +1 vote on my question in case it helps you ...

-1


source


std::string str = "3 - 90.2 85.54 93.0";
std::stringstream ss(str);
int num;   ss >> num;
char c;    ss >> c;
std::vector arr;
arr.reserve(num);
for (int i = 0; i < num; ++i)
{
    double d; ss >> d;
    arr.push_back(d);
}

      

-1


source







All Articles