iostream

stringstream unsigned conversion broken?

…衆ロ難τιáo~ 提交于 2019-12-01 19:18:16
Consider this program: #include <iostream> #include <string> #include <sstream> #include <cassert> int main() { std::istringstream stream( "-1" ); unsigned short n = 0; stream >> n; assert( stream.fail() && n == 0 ); std::cout << "can't convert -1 to unsigned short" << std::endl; return 0; } I tried this on gcc (version 4.0.1 Apple Inc. build 5490) on OS X 10.5.6 and the assertion is true; it fails to convert -1 to an unsigned short. In Visual Studio 2005 (and 2008) however, the assertion fails and the resulting value of n is the same as what you would expect from an compiler generated

Getting input directly into a vector in C++

冷暖自知 提交于 2019-12-01 18:58:52
Consider the following code piece: ... int N,var; vector<int> nums; cin >> N; while (N--) { cin >> var; nums.push_back(var); } ... Is it possible to do this without using an auxillary variable, in this case var ? Assuming you have already read the initial N , there is a nice trick using istream_iterator : std::vector<int> nums; nums.reserve(N); std::copy(std::istream_iterator<int>(std::cin), std::istream_iterator<int>(), std::back_inserter(nums)); The back_inserter object turns itself into an iterator that adds elements to the vector at the end. Iterator streams can be parameterized by the

redirect std::cout to QTextEdit

喜你入骨 提交于 2019-12-01 18:23:40
Is it possible (and more importantly -how-) to redirect an output stream to a QTextBox. So that if I write std::cout << "test" anywhere in the application it gets redirected to a textbox I defined? I tried the obvious (where ui.textEdit is a pointer to the text edit box): std::cout.rdbuf(ui.textEdit); std::cout << "test"; However this doesn't work. (obviously). - Nor does redirecting cout to qDebug work (or even direction a qDebug to a textfield). I'm using qt4.8 btw... EDIT: So I tried the solution posted in the mailing list.. However now an access violation shows up. class MainInterface :

Read and write using std::hexfloat

有些话、适合烂在心里 提交于 2019-12-01 18:21:41
问题 This piece of code printed 0 on my machine, but I expected 0.3 . What's wrong? I'm using g++ 6.3.1 on latest Arch Linux. Compilation flags seem unrelevent. #include <iostream> #include <sstream> int main() { std::stringstream s; s << std::hexfloat << 0.3 << std::endl; double d = -1.0; while(s >> std::hexfloat >> d) std::cout << d << std::endl; } 回答1: Use double d = std::strtod(s.str().c_str(), NULL); as a workaround. It seems like a bug. 来源: https://stackoverflow.com/questions/42604596/read

Introduction To C++ IO Streams

别说谁变了你拦得住时间么 提交于 2019-12-01 18:05:27
I got a snippet of code from this article and I'm confused as to how it works? The snippet starts by saying: You can detect that a particular read or write operation failed by testing the result of the read. For example, to check that a valid integer is read from the user, you can do this: int x; if ( cin >> x ) { cout << "Please enter a valid number" << endl; } This works because the read operation returns a reference to the stream. I understand that the cin >> x operation returns a reference to cin but I'm still confused as to how evaluating the reference to the standard input stream object

How to read numeric data as uint8_t [duplicate]

有些话、适合烂在心里 提交于 2019-12-01 17:25:47
This question already has an answer here: istringstream decimal integer input to 8-bit type 4 answers I have some human-readable numeric data in an istream . The values range from 0-255, and I want to store them in uint8_t . Unfortunately, if I try something like uint8_t a, b; stringstream data("124 67"); data >> a >> b; then I end up with a == '1' and b == '2' . I understand that this is the desired behavior in many situations, but I want to end up with a == 124 and b == 67 . My current workaround is to stream the data into int s, then copy them to the uint8_t s. uint8_t a, b; int a_, b_;

Getting input directly into a vector in C++

时光毁灭记忆、已成空白 提交于 2019-12-01 17:24:24
问题 Consider the following code piece: ... int N,var; vector<int> nums; cin >> N; while (N--) { cin >> var; nums.push_back(var); } ... Is it possible to do this without using an auxillary variable, in this case var ? 回答1: Assuming you have already read the initial N , there is a nice trick using istream_iterator : std::vector<int> nums; nums.reserve(N); std::copy(std::istream_iterator<int>(std::cin), std::istream_iterator<int>(), std::back_inserter(nums)); The back_inserter object turns itself

Why does endl get used as a synonym for “\\n” even though it incurs significant performance penalties?

杀马特。学长 韩版系。学妹 提交于 2019-12-01 17:13:49
This program: #include <iostream> #include <cstdlib> #include <string> int main(int argc, const char *argv[]) { using ::std::cerr; using ::std::cout; using ::std::endl; if (argc < 2 || argc > 3) { cerr << "Usage: " << argv[0] << " [<count>] <message>\n"; return 1; } unsigned long count = 10000; if (argc > 2) { char *endptr = 0; count = ::std::strtoul(argv[1], &endptr, 10); if ((argv[1][0] == '\0') || (*endptr != '\0')) { cerr << "Usage: " << argv[0] << " [<count>] <message>\n"; return 1; } } const ::std::string msg((argc < 3) ? argv[1] : argv[2]); for (unsigned long i = 0; i < count; ++i) {

Reading a single character from an fstream?

可紊 提交于 2019-12-01 16:22:37
I'm trying to move from stdio to iostream, which is proving very difficult. I've got the basics of loading a file and closing them, but I really don't have a clue as to what a stream even is yet, or how they work. In stdio everything's relatively easy and straight forward compared to this. What I need to be able to do is Read a single character from a text file. Call a function based on what that character is. Repeat till I've read all the characters in the file. What I have so far is.. not much: int main() { std::ifstream("sometextfile.txt", std::ios::in); // this is SUPPOSED to be the while

How to read numeric data as uint8_t [duplicate]

时光怂恿深爱的人放手 提交于 2019-12-01 16:20:59
问题 This question already has answers here : istringstream decimal integer input to 8-bit type (4 answers) Closed 5 years ago . I have some human-readable numeric data in an istream . The values range from 0-255, and I want to store them in uint8_t . Unfortunately, if I try something like uint8_t a, b; stringstream data("124 67"); data >> a >> b; then I end up with a == '1' and b == '2' . I understand that this is the desired behavior in many situations, but I want to end up with a == 124 and b =