为什么std :: noskipws不工作,或者它应该做什么? [英] Why doesn't std::noskipws work, or what is it supposed to do?

查看:603
本文介绍了为什么std :: noskipws不工作,或者它应该做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我的理解是

cin >> std::noskipws >> str;

应该从 cin 我有空格到 str 。但是这只会将i放入 str 。这可能是一个假设,在这种情况下 std :: noskipws 做什么?

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?

函数 std :: getline 并且这样做只是为了教育目的,我决定我会尝试得到 std :: noskipws 为我工作。我曾经尝试过,它只是从来没有工作,所以我通常使用 std :: getline

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.

我认为我发现到目前为止, std :: noskipws 技术上只是unsets

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); 

ios_base::unsetf(ios_base::skipws);

所以我尝试继承我自己的流形式 basic_iostream 并手动设置这些标志(取消设置)。仍然没有骰子。

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?

推荐答案

std :: noskipws告诉istream在尝试读取类型时不跳过任何前导空白。当没有前导空白时,该标志没有影响。

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.

这篇关于为什么std :: noskipws不工作,或者它应该做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆