How to use basic_istream if you have std :: string

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

+3
source share
4 answers

You can put string data in a stream:

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

function_taking_stream(ss);
+9
source

I have a function that uses basic_istream as an argument ...

So, I assume that you have a function that takes a variable of type specialized in the basic_istream pattern.

... and I have std :: string with the data I need to pass.

So, I assume that you want to pass this data to the istream argument.

How should I do it?

No. You cannot feed output to the input stream!

0

, OP , basic_istream<E> <<21 > , std::basic_string<E>.

, basic_string, std::string. , , , , UTF-8 UTF-16.

, ASCII, - :

std::basic_string<E> strTemp;
is >> strTemp;
std::string str( strTemp.begin(), strTemp.end() );

basic_ostream<E>, basic_string<E> std::string, .

"" , , E char, . , "".

0

, , json boost property_tree:

#include <iostream>
#include <string>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
using boost::property_tree::ptree;

int main ()
{
  std::string jsonString ("{ \"my key\": \"my value\" }");
  ptree pt;
  std::stringstream ss(jsonString);
  read_json(ss, pt);
  std::cout << pt.get<std::string>("my key") << std::endl;
  return 0;
}
0

Source: https://habr.com/ru/post/1785008/


All Articles