Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

c++ - Why doesn't std::noskipws work, or what is it supposed to do?

First off my understanding is that

cin >> std::noskipws >> str;

should stick a whole line from cin like "i have spaces" into str. However this only puts "i" into str. This could be a false assumption in which case what does std::noskipws do?

I know there is a function std::getline and that does work but simply for educational purposes I decided I would try to get std::noskipws to work for me. I have tried in the past and it just never works so I normally move on and use std::getline.

What I think I have found so far is that std::noskipws technically just unsets std::skipws which internally to the basic_iostream just calls

ios_base::unsetf(std::ios::skipws); 

or

ios_base::unsetf(ios_base::skipws);

So I tried inheriting my own stream form basic_iostream and setting those flags (unsetting) them manually. Still no dice.

So, am I just totally off base or is there a way to make this work?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

std::noskipws tells the istream to not skip any leading white space when attempting to read a type. When there is no leading white space, then the flag has no impact.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...