标签云

微信群

扫码加入我们

WeChat QR Code

I am trying to learn C++ since yesterday and I am using this document:http://www.cplusplus.com/files/tutorial.pdf (page 32) . I found a code in the document and I ran it. I tried inputting Rs 5.5 for price and an integer for quantity and the output was 0.I tried inputting 5.5 and 6 and the output was correct.// stringstreams#include <iostream> #include <string> #include <sstream> using namespace std; int main () { string mystr; float price = 0; int quantity = 0; cout << "Enter price: "; getline (cin,mystr); stringstream(mystr) >> price; cout << "Enter quantity: "; getline (cin,mystr); stringstream(mystr) >> quantity; cout << "Total price: " << price*quantity << endl; return 0; }Question: What exactly does the mystring command do? Quoting from the document: "In this example, we acquire numeric values from the standard inputindirectly. Instead of extracting numeric valuesdirectly from thestandard input, we get lines from the standard input (cin) into astring object (mystr), and thenwe extract the integer values fromthis string into a variable of type int (quantity)."My impression was that the function will take the integral part of a string and use that as input.(I don't exactly know how to ask a question here. I am also new to programming)Thank you.


This example is kinda weird, I've never seen stringstream used that way. I usually load the line, convert it and then extract by parts, however this obviously has little advantage here because cin is an input stream already... So cin >> price >> quantity; would be by far simpler. That would be a good reason NOT to use cplusplus.com tutorials.

2019年06月26日59分40秒

Funny that that tutorial was my first exposure to C++. In hindsight, it's pretty poor and incomplete. I'd suggest a good book instead.

2019年06月26日59分40秒

BartekBanachewicz Maybe they just needed to come up with example to show how stringstream works. It is a bizarre one probably even a bad one =) But it shows you can treat string as a stream.

2019年06月26日59分40秒

If it's not an introduction to more advanced uses of stringstream then it's definitely a wrong example. And even if it is then it should be written differently.

2019年06月25日59分40秒

trojansdestroy You can't understand stringstream without understanding all the primitives it's based on, so I don't see how reading a tutorial helps in that regard.

2019年06月25日59分40秒

Note: the use of ret is unnecessary, one could write return ss.str();.

2019年06月25日59分40秒

MatthieuM. I think I wasn't sure if RVO would kick-in if it was written like that, or if object returned by ss.str() would survive exit point. This way I know I am making a copy, and RVO will work. But you are most probably right.

2019年06月26日59分40秒

Actually, there are 2 forms of RVO: URVO (for unnamed, that is temporaries) and NRVO (for named); most compilers implement RVO, but some restrict it to only URVO (depending on build options). In general, though, there are plenty of other factors to take into account, so you should just write the cleanest code possible and not worry too much about whether RVO will kick in.

2019年06月26日59分40秒

NRVO is common (as is URVO) however it's a non-issue due to move constructors.

2019年06月26日59分40秒