Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use basic_istream when you have a std::string

Tags:

c++

I have a function that uses basic_istream as an argument, and I have a std::string with the data I need to pass it. How do I do that?

like image 452
Dave Brooks Avatar asked Jan 12 '11 17:01

Dave Brooks


People also ask

How do I convert a string to an int in C++?

Using the stoi() function The stoi() function converts a string data to an integer type by passing the string as a parameter to return an integer value. The stoi() function contains an str argument. The str string is passed inside the stoi() function to convert string data into an integer value.

How do I read Istream?

Other ways to read a std::istreamTo read a line of input, try the getline() function. For this, you need #include <string> in addition to #include <iostream> . To read a single char: use the get() method. To read a large block of characters, either use get() with a char[] as the argument, or use read() .

How does stoi work in C++?

In C++, the stoi() function converts a string to an integer value. The function is shorthand for “string to integer,” and C++ programmers use it to parse integers out of strings. The stoi() function is relatively new, as it was only added to the language as of its latest revision (C++11) in 2011.

Is string the same as std :: string?

There is no functionality difference between string and std::string because they're the same type.


1 Answers

You can put the string data into a stream:

std::string x;
std::stringstream ss(x); // put string into stream

function_taking_stream(ss);
like image 52
GManNickG Avatar answered Oct 29 '22 15:10

GManNickG