stringstream

c++ execute function any time a stream is written to

自古美人都是妖i 提交于 2019-12-09 19:01:14
问题 I have a simple GUI program that uses a custom stringstream to redirect output from the console to a text field in the GUI (under some circumstances). currently. the window redraws any time I hit enter, but it's possible that output could be generated at other times. Is there a way to register a function with the stringstream that gets executed every time the << operator is used on the stream? NOTE I should have pointed out that I cannot use C++11 in my solution. the machines on which this

how to read stringstream with dynamic size?

南笙酒味 提交于 2019-12-09 15:37:38
问题 I wanted to experiment with stringstream for an assignment, but I'm a little confused on how it works. I did a quick search but couldn't find anything that would answer my question. Say I have a stream with a dynamic size, how would I know when to stop writing to the variable? string var = "2 ++ asdf 3 * c"; stringstream ss; ss << var; while(ss){ ss >> var; cout << var << endl; } and my output would be: 2 ++ asdf 3 * c c I'm not sure why I get that extra 'c' at the end, especially since _M_in

Why is stringstreams rdbuf() and str() giving me different output?

大憨熊 提交于 2019-12-09 13:24:48
问题 I have this code, int main() { std::string st; std::stringstream ss; ss<<"hej hej med dig"<<std::endl; std::getline(ss,st,' '); std::cout <<"ss.rdbuf()->str() : " << ss.rdbuf()->str(); std::cout <<"ss.rdbuf() : " << ss.rdbuf(); return 0; } Giving me this output ss.rdbuf()->str() : hej hej med dig ss.rdbuf() : hej med dig But why is that? Is that because of ostreams definition of operator<str() gives me different output. In my eyes the output should be the same even if I have used getline. 回答1

concatenate stringstream in c++

前提是你 提交于 2019-12-08 15:58:44
问题 How can I concatenate two stringstreams? #include <stdio.h> #include <stdlib.h> #include <string.h> #include <iostream> #include <fstream> #include <sstream> #include <string> #include "types.h" int main () { char dest[1020] = "you"; char source[7] = "baby"; stringstream a,b; a << source; b << dest; a << b; /*HERE NEED CONCATENATE*/ cout << a << endl; cout << a.str() << endl; return 0; } The output is the following in both tries: 0xbf8cfd20 baby0xbf8cfddc The desired output is babyyou . 回答1:

C++: Why re-input stringstream doesn't work? the content remains the same

牧云@^-^@ 提交于 2019-12-08 09:52:31
问题 I am trying to understand the details of the stringstream in C++. But I find a trouble when I tried to re-input ("<<") the stringstream object. (i.e. the content of the obj still remains the same.) My code is as follows. Outputs are shown in comments. #include <iostream> #include <string> #include <sstream> using namespace std; int main() { stringstream sso; cout <<"orginal: " << sso.str() <<endl; //orginal:"empty" sso << "A" << " " << "B" << " " << "C"; cout <<"flowin:" << sso.str() <<endl;

stringstream overflow at 4GB

自闭症网瘾萝莉.ら 提交于 2019-12-07 17:31:51
问题 I'm having trouble getting beyond 4GB limitation for stringstream, even though it is running on a 64bit linux box with enough memory. The test code below (revised after reading your comments) core dump after 4GB. From the gdb trace, stringstream uses the default std::char_traits where int_type is set to 32bit int, rather than 64bit size_t. Any suggestion to work around? #include <stdint.h> #include <iostream> #include <sstream> #include <math.h> using namespace std; int main(int narg, char**

std::stringstream as parameter to a function

岁酱吖の 提交于 2019-12-07 12:55:04
问题 I have a std::vector<std::string> temp_results and I wish to use std::for_each to go through this vector and concatenate a string, so I concocted the following construction: std::stringstream ss; std::string res = std::for_each(temp_results.begin(), temp_results.end(), boost::bind(addup, _1, ss)); std::string addup(std::string str, std::stringstream ss) { ss << str; ss << ";"; return ss.str; } I get the following error, which is beyond my understanding: error C2475: 'std::basic_stringstream<

C++ stringstreams with std::hex

雨燕双飞 提交于 2019-12-07 11:28:27
问题 I am looking into code at work. I am having following code. In following code what is the meaning of the last statement? bOptMask = true; std::string strMask; strMask.append(optarg); std::stringstream(strMask) >> std::hex >> iMask >> std::dec; In addition to the above question: I have string input and I need to know how to convert it to an integer using C++ streams as above instead of atoi() . The problem I am facing is if I give input strOutput.append(optarg); cout << "Received option for

How do I use write with stringstream?

不打扰是莪最后的温柔 提交于 2019-12-07 09:00:48
问题 I have a vector<char> of data which I want to write into std::stringstream . I tried: my_ss.write(vector.data(), vector.size()); ...but it seems to put nothing into my_ss which I declared as follows: std::stringstream my_ss( std::stringstream::binary); Why write is not working (app does not crash and compiles with 0 errors, 0 warnings)? 回答1: For the "how do I do it" you can use a std::ostream_iterator : std::copy(vector.begin(), vector.end(), std::ostream_iterator<char>(my_ss)); Complete

cannot overwrite stringstream variable with a new value

♀尐吖头ヾ 提交于 2019-12-07 08:17:02
问题 string whatTime(int seconds) { string h,m,s,ans; stringstream ss; ss << (seconds/3600); seconds -= (3600*(seconds/3600)); ss >> h; ss.str(""); ss << (seconds/60); seconds -= (60*(seconds/60)); ss >> m; ss.str(""); ss << seconds; ss >> s; return (h + ":" + m + ":" + s ); } Output for above program is coming in this format "some_value::" I have also tried ss.str(std::string()) and ss.str().clear() but even that doesn't work. Could somebody please suggest any ways how to tackle this problem? 回答1