stringstream

Tricks to pass from a ostringstream to a istringstream

亡梦爱人 提交于 2020-01-05 10:27:44
问题 I try to make a module of compression/decompression and then use istringstream for compression and ostringstream for decompression. My problem is that after filling my istringstream with compression datas, I'm not able to convert this stream into an ostringstream. I try : iss.rdbuf(oss.rdbuf()); as the in and out type match but it doesn't work. Do you have any idea ? Thank in advance. 回答1: stringstream::rdbuf doesn't have an overload that takes a parameter. It does, however, inherit the base

Tricks to pass from a ostringstream to a istringstream

时间秒杀一切 提交于 2020-01-05 10:27:15
问题 I try to make a module of compression/decompression and then use istringstream for compression and ostringstream for decompression. My problem is that after filling my istringstream with compression datas, I'm not able to convert this stream into an ostringstream. I try : iss.rdbuf(oss.rdbuf()); as the in and out type match but it doesn't work. Do you have any idea ? Thank in advance. 回答1: stringstream::rdbuf doesn't have an overload that takes a parameter. It does, however, inherit the base

Simple string passing through nodes in Bison/Yacc

会有一股神秘感。 提交于 2020-01-05 09:12:11
问题 I have to concatenate strings in semantic rules of my yacc file: %union { stringstream sstream; } %type<sstream> node1 node2 --- node1 : node2 { $$ << $1 << " goodbye" } node2 : final { $$ << "hello" } However, as stringstream or even string are not allowed in unions, I don't find any easy way to mix char * , int , and make nodes transport a string that I can manipulate everywhere. How should I do it ? 回答1: I don't remember bison / yacc details, but you sure can use pointer and new it. Just

C++ stringstreams skipping a character

老子叫甜甜 提交于 2020-01-04 02:32:26
问题 I have a file which the first line reads ">FileName.txt". My goal is to read this line, and save "FileName.txt" to a variable called name. So I have: ifstream file; /* File opening stuff */ string line, name; getline(file,line); stringstream converter(line); converter >> name; This accomplishes saving ">FileName.txt" to the variable name , but I need to remove the '>' character. I am not sure if I should do that after this point, or if there is a way to skip over it entirely using the

Why do I have to clear std::stringstream here?

孤街浪徒 提交于 2020-01-03 10:04:34
问题 I wrote a short testprogram to see if I can append to a string using stringstream repeatedly. In the first version I got Output1 and I don't really understand why s1 stays empty. I found out that I have to do ss.clear() and then get the expected result in Output2. Can anybody explain why it doesn't work without the clear? I would have expected that, If I repeatedly enter numbers and fetch them back into a string, I should get always the number. I was unsure if the number gets appended, but

How to get characters out of stringstream without copy?

蓝咒 提交于 2020-01-03 09:24:08
问题 What is the proper c++11 way to extract a set of characters out of a stringstream without using boost? I want to do it without copying, if possible, because where this is used is in a critical data loop. It seems, though, std::string does not allow direct access to the data. For example, the code below performs a substring copy out of a stringstream: inline std::string left(std::stringstream ss, uint32_t count) { char* buffer = new char[count]; ss.get(buffer, count); std::string str(buffer);

istringstream in C++

笑着哭i 提交于 2020-01-02 20:44:38
问题 I'm sure I'm just doing something stupid here, but I can't quite figure out what it is. When I try to run this code: #include <iostream> #include <string> #include <sstream> using namespace std; int main(int argc, char *argv[]) { string s("hello"); istringstream input(s, istringstream::in); string s2; input >> s2; cout << s; } I get this error: malloc: *** error for object 0x100016200: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug The only thing I

How to use C++ String Streams to append int?

北战南征 提交于 2020-01-02 00:48:10
问题 could anyone tell me or point me to a simple example of how to append an int to a stringstream containing the word "Something" (or any word)? 回答1: stringstream ss; ss << "Something" << 42; For future reference, check this out. http://www.cplusplus.com/reference/iostream/stringstream/ 回答2: I'd probably do something on this general order: #include <string> #include <sstream> #include <iostream> int main() { std::stringstream stream("Something "); stream.seekp(0, std::ios::end); stream << 12345;

The role of std::ws (whitespace) when reading data

你。 提交于 2020-01-01 14:23:22
问题 Data saved in my file is (white spaces added at both beginning and end on purpose for this test): 1 2 3 Loading the data using the code below with or without "std::ws" does not cause any difference. So I am confused by the role of "std::ws" as I have seen code using it. Can someone explain a little bit? Thanks! void main () { ifstream inf; inf.open ("test.txt"); double x=0, y=0, z=0; string line; getline(inf, line); istringstream iss(line); //Using "std::ws" here does NOT cause any difference

C++: what benefits do string streams offer?

半世苍凉 提交于 2020-01-01 09:57:48
问题 could any one tell me about some practical examples on using string streams in c++, i.e. inputing and outputing to a string stream using stream insertion and stream extraction operators? 回答1: You can use string streams to convert anything that implements operator << to a string: #include <sstream> template<typename T> std::string toString(const T& t) { std::ostringstream stream; stream << t; return stream.str(); } or even template <typename U, typename T> U convert(const T& t) { std: