在C+中演示noskipws+; 我在C++中尝试了NoSkIPWS操作器,我写了以下代码。p> #include <iostream> #include <sstream> #include <string> using namespace std; int main() { string first, middle, last; istringstream("G B Shaw") >> first >> middle >> last; cout << "Default behavior: First Name = " << first << ", Middle Name = " << middle << ", Last Name = " << last << '\n'; istringstream("G B Shaw") >> noskipws >> first >> middle >> last; cout << "noskipws behavior: First Name = " << first << ", Middle Name = " << middle << ", Last Name = " << last << '\n'; }
输出在C+中演示noskipws+; 我在C++中尝试了NoSkIPWS操作器,我写了以下代码。p> #include <iostream> #include <sstream> #include <string> using namespace std; int main() { string first, middle, last; istringstream("G B Shaw") >> first >> middle >> last; cout << "Default behavior: First Name = " << first << ", Middle Name = " << middle << ", Last Name = " << last << '\n'; istringstream("G B Shaw") >> noskipws >> first >> middle >> last; cout << "noskipws behavior: First Name = " << first << ", Middle Name = " << middle << ", Last Name = " << last << '\n'; },c++,manipulators,C++,Manipulators,输出 Default behavior: First Name = G, Middle Name = B, Last Name = Shaw noskipws behavior: First Name = G, Middle Name = , Last Name = B Default behavior: First Name = G, Middle Name = B, Last Name = Shaw noskipws behavior: First Name = G, Middle Nam
Default behavior: First Name = G, Middle Name = B, Last Name = Shaw
noskipws behavior: First Name = G, Middle Name = , Last Name = B
Default behavior: First Name = G, Middle Name = B, Last Name = Shaw
noskipws behavior: First Name = G, Middle Name = , Last Name = Shaw
我修改了这段代码,使它能够为这样的字符工作,并且工作得非常好
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
char first, middle, last;
istringstream("G B S") >> first >> middle >> last;
cout << "Default behavior: First Name = " << first << ", Middle Name = " << middle << ", Last Name = " << last << '\n';
istringstream("G B S") >> noskipws >> first >> middle >> last;
cout << "noskipws behavior: First Name = " << first << ", Middle Name = " << middle << ", Last Name = " << last << '\n';
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
首先,中间,最后;
istringstream(“G B S”)>>第一个>>中间>>最后一个;
最后;
cout字符串的>
的基本算法是:
1) 跳过空白
2) 读取并提取到下一个空格
如果使用noskipws
,则跳过第一步
第一次读取后,您将被定位在空白处,因此下一次(以及所有后续)读取将立即停止,不提取任何内容。
有关更多信息,请参见
形式
许多提取操作认为白字符本身是终止字符,因此,在禁用SKIPWS标志的情况下,一些提取操作可能根本不从流中提取字符。
因此,在与字符串一起使用时,请删除noskipws
原因是在第二个示例中,您根本没有读取最后一个变量,而是打印它的旧值
std::string first, middle, last;
std::istringstream iss("G B S");
^^^
iss >> first >> middle >> last;
std::cout << "Default behavior: First Name = " << first
<< ", Middle Name = " << middle << ", Last Name = " << last << '\n';
std::istringstream iss2("G B T");
^^^
iss2 >> std::noskipws >> first >> middle >> last;
std::cout << "noskipws behavior: First Name = " << first
<< ", Middle Name = " << middle << ", Last Name = " << last << '\n';
std::字符串第一、中间、最后;
标准:istringstream iss(“G B S”);
^^^
iss>>第一个>>中间>>最后一个;
std::cout>last;
标准::cout
对字符串执行提取时,首先清除字符串并将字符插入其缓冲区
21.4.8.9插入器和取出器
template<class charT, class traits, class Allocator>
basic_istream<charT, traits>&
operator>>(basic_istream<charT, traits>& is,
basic_string<charT, traits, Allocator>& str);
效果:其行为类似于中的格式化输入成员(如27.7.2.2.1所述)。在构造哨兵对象后,将从
中的中提取一个字符(如果有),并存储在c
中。否则,该函数将调用.setstate(failbit)
提取器的语义将字符存储到其操作数中(如果有可用的话)。它不会对空格(甚至EOF字符)进行分隔。它将像普通字符一样提取它。根据这一点,它应该首先读取G
,然后因为它将遇到一个空格,所以在字符串中间不会有任何内容
,然后B
应该保存在字符串last
中,但它将Shaw
保存在字符串last
@mozart:ple中如果看到我的编辑并阅读提供的链接,这将肯定解决问题。好的,我明白了。这意味着字符串last
根本没有改变?听起来更符合逻辑。你能引用一些参考资料来支持你的答案吗?@SumitGera Updated.)
template<class charT, class traits, class Allocator>
basic_istream<charT, traits>&
operator>>(basic_istream<charT, traits>& is,
basic_string<charT, traits, Allocator>& str);
template<class charT, class traits>
basic_istream<charT, traits>& operator>>(basic_istream<charT, traits>& in,
charT& c);