
-----------------------------------
wtd
Mon Oct 04, 2004 12:07 am

[Tutorial] Extracting data from a string
-----------------------------------
The standard stream classes we use for dealing with classes have well known capabilities to extract data (integers, floating-point numbers, etc.).

#include 

int main()
{
   int i;
   double d;

   std::cin >> i >> d;

   return 0;
}

But what if you want to perform a similar operation on a string?

The istringstream class allows that very thing.

#include 
#include 
#include 

int main()
{
	int i;
	double d;

	std::string str = "32 42.67";
	std::istringstream input(str);

	input >> i >> d;

	return 0;
}

Additionally, it's possible to check for errors with this class.

#include 
#include 
#include 

int main()
{
	double d;

	std::string str = "hello";
	std::istringstream input(str);

	input >> d;

	if (input.fail())
	{
		std::cerr 