How to extract a string of float numbers into a vector of doubles in C++

1 Answer

0 votes
#include <iostream>
#include <string>
#include <sstream>
#include <iterator>
#include <vector>

using std::cout;
using std::endl;
using std::string;
using std::vector;

int main()
{
	string s = "23.34 87.243 976.1 5291.9 8.8721";

	std::istringstream iss(s);
	vector<double> tokens{ std::istream_iterator<double>{iss},
		                   std::istream_iterator<double>{} };

	for (double n : tokens)
		cout << n << endl;

	return 0;
}

/*
run:

23.34
87.243
976.1
5291.9
8.8721

*/

 



answered May 26, 2018 by avibootz

Related questions

1 answer 147 views
1 answer 193 views
1 answer 291 views
1 answer 648 views
1 answer 136 views
1 answer 169 views
...