stringstream

stringstream temporary ostream return problem

倖福魔咒の 提交于 2019-11-29 09:39:45
问题 I'm creating a logger with the following sections: // #define LOG(x) // for release mode #define LOG(x) log(x) log(const string& str); log(const ostream& str); With the idea to do: LOG("Test"); LOG(string("Testing") + " 123"); stringstream s; LOG(s << "Testing" << 1 << "two" << 3); This all works as intended, but when I do: LOG(stringstream() << "Testing" << 1 << "two" << 3); It does not work: void log(const ostream& os) { std::streambuf* buf = os.rdbuf(); if( buf && typeid(*buf) == typeid

Reading getline from cin into a stringstream (C++)

和自甴很熟 提交于 2019-11-29 07:16:09
So I'm trying to read input like this from the standard input (using cin ): Adam English 85 Charlie Math 76 Erica History 82 Richard Science 90 My goal is to eventually store each data piece in its own cell in a data structure I have created, so basically I want to parse the input so each piece of data is individual. Since each row of input is inputted by the user one at a time, each time I get an entire row of input that I need to parse. Currently I am trying something like this: stringstream ss; getline(cin, ss); string name; string course; string grade; ss >> name >> course >> grade; The

Going from string to stringstream to vector<int>

↘锁芯ラ 提交于 2019-11-28 23:28:02
问题 I've this sample program of a step that I want to implement on my application. I want to push_back the int elements on the string separately, into a vector. How can I? #include <iostream> #include <sstream> #include <vector> using namespace std; int main(){ string line = "1 2 3 4 5"; //includes spaces stringstream lineStream(line); vector<int> numbers; // how do I push_back the numbers (separately) here? // in this example I know the size of my string but in my application I won't } 回答1: int

std::stringstream vs std::string for concatenating many strings

微笑、不失礼 提交于 2019-11-28 22:34:53
I know one of the advantages of std::stringstream is that it is a std::istream so it may accept input from any type that defines operator<< to std::istream , and also from primitives types. I am not going to use operator<< ; instead I am just going to concatenate many strings. Does the implementation of std::stringstream make it faster than std::string for concatenating many strings? Nicol Bolas There's no reason to expect std::string 's appending functions to be slower than stringstream 's insertion functions. std::string will generally be nothing more than a possible memory allocation/copy

Best way to empty stringstream?

孤街浪徒 提交于 2019-11-28 21:04:07
One of the possibilities is: somestringstream.str(""); But is it most optimal? Is there any way to preserve stringstream internal buffer, so that following operator<<() calls would not require to reserve memory again? Brian R. Bondy I've always done: s.clear();//clear any bits set s.str(std::string()); @litb gets into more detail about how to seekp to the start of the stream combined with std::ends you can keep your allocated size. 来源: https://stackoverflow.com/questions/834622/best-way-to-empty-stringstream

How to deal with last comma, when making comma separated string? [duplicate]

北城以北 提交于 2019-11-28 20:54:58
问题 This question already has answers here : Closed 8 years ago . Possible Duplicates: Don't print space after last number Printing lists with commas C++ #include <vector> #include <iostream> #include <sstream> #include <boost/foreach.hpp> using namespace std; int main() { vector<int> VecInts; VecInts.push_back(1); VecInts.push_back(2); VecInts.push_back(3); VecInts.push_back(4); VecInts.push_back(5); stringstream ss; BOOST_FOREACH(int i, VecInts) { ss << i << ","; } cout << ss.str(); return 0; }

Equivalent of %02d with std::stringstream?

北慕城南 提交于 2019-11-28 20:01:32
I want to output an integer to a std::stringstream with the equivalent format of printf 's %02d . Is there an easier way to achieve this than: std::stringstream stream; stream.setfill('0'); stream.setw(2); stream << value; Is it possible to stream some sort of format flags to the stringstream , something like (pseudocode): stream << flags("%02d") << value; You can use the standard manipulators from <iomanip> but there isn't a neat one that does both fill and width at once: stream << std::setfill('0') << std::setw(2) << value; It wouldn't be hard to write your own object that when inserted into

Incomplete type is not allowed: stringstream

偶尔善良 提交于 2019-11-28 18:30:30
Why does this line give the error Error: incomplete type is not allowed ? stringstream ss; #include <sstream> and use the fully qualified name i.e. std::stringstream ss; Please, add: #include <sstream> An incomplete type is when the compiler knows the identifier is a type, for instance because you have a forward-declaration of it (e.g. class stringstream; ), but it hasn't seen a full definition for it ( class stringstream { ... }; ). This could happen for a type that is only present through included header files -- when you've included header files that use the type, but not the header file

Discrepancy between istream's operator>> (double& val) between libc++ and libstdc++

你离开我真会死。 提交于 2019-11-28 12:11:40
With my recent upgrade to Mac OS X 10.9 the default standard C++ library changed from libstdc++ to libc++. Since then I observe unexpected behaviour of the stringstream operator>>(double) documented in the code example below. In summary the libc++ seems to have problems with extracting double values from stringstreams when the double value is followed by a letter. I already checked the standard (2003) but I can't find any specific information if extraction should work in this case or not. So I would be grateful for any input whether this is a bug in libc++ or libstdc++. #include <sstream>

Memory Error with std:ostringstream and -std=c++11? [duplicate]

試著忘記壹切 提交于 2019-11-28 11:57:54
问题 This question already has an answer here: stringstream, string, and char* conversion confusion 5 answers EDIT : Thanks to everyone who pointed out the problem, and that it was discussed on Stack Overflow. I cast the last close vote myself. A related question: neither CPP Reference on ostringstream or ostringstream::str state its a temporary. How did so many people know? Or is there different documentation I should have consulted? I'm having a lot of trouble with memory errors under Debian 7.3