如何在 c++ 中创建空间?
假设我们有一段代码:
int main()
{
char a[10];
for(int i = 0; i < 10; i++)
{
cin>>a[i];
if(a[i] == ' ')
cout<<"It is a space!!!"<<endl;
}
return 0;
}
如何从标准输入中输入空格符号?如果你写空格,程序会忽略! :( 是否有任何符号组合(例如“\s”或类似的东西)表示“空格”,我可以从标准输入中为我的代码使用它?
Say we have a code:
int main()
{
char a[10];
for(int i = 0; i < 10; i++)
{
cin>>a[i];
if(a[i] == ' ')
cout<<"It is a space!!!"<<endl;
}
return 0;
}
How to cin a Space symbol from standard input? If you write space, program ignores! :(
Is there any combination of symbols (e.g. '\s' or something like this) that means "Space" that I can use from standard input for my code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
默认情况下,它会跳过所有空白(空格、制表符、换行符等)。您可以更改其行为,或使用稍微不同的机制。要更改其行为,请使用操纵器
noskipws
,如下所示:但是,由于您似乎想查看单个字符,因此我建议使用
get
,例如注意:如果
get
找到换行符 (\n
) 或在 n-1 个字符之后,get
将停止从流中检索字符。它提前停止,以便可以将空字符 (\0
) 附加到数组中。您可以在此处了解有关istream
接口的更多信息。It skips all whitespace (spaces, tabs, new lines, etc.) by default. You can either change its behavior, or use a slightly different mechanism. To change its behavior, use the manipulator
noskipws
, as follows:But, since you seem like you want to look at the individual characters, I'd suggest using
get
, like this prior to your loopNote:
get
will stop retrieving chars from the stream if it either finds a newline char (\n
) or after n-1 chars. It stops early so that it can append the null character (\0
) to the array. You can read more about theistream
interface here.要输入包含大量空格的整行,您可以使用
getline(cin,string_variable);
例如:
此格式捕获句子中的所有空格,直到按下
return
To input AN ENTIRE LINE containing lot of spaces you can use
getline(cin,string_variable);
eg:
This format captures all the spaces in the sentence untill
return
is pressed使用 cin.get() 读取下一个字符。
然而,对于这个问题,一次读取一个字符是非常低效的。请改用
istream::read()
。并使用
==
检查相等性,而不是=
。Use
cin.get()
to read the next character.However, for this problem, it is very inefficient to read a character at a time. Use the
istream::read()
instead.And use
==
to check equality, not=
.使用cin的>>运算符将删除前导空格并在第一个尾随空格处停止输入。要获取整行输入(包括空格),请尝试
cin.getline()
。要一次抓取一个字符,您可以使用 cin.get() 。Using cin's >> operator will drop leading whitespace and stop input at the first trailing whitespace. To grab an entire line of input, including spaces, try
cin.getline()
. To grab one character at a time, you can usecin.get()
.我想我会分享对我有用的答案。上一行以换行符结束,因此大多数答案本身不起作用。这样做是这样的:
假设输入总是至少 2 个字符长,这适合我的情况。您还可以尝试将其与字符串
"\n"
进行简单比较。I thought I'd share the answer that worked for me. The previous line ended in a newline, so most of these answers by themselves didn't work. This did:
That was assuming the input is always at least 2 characters long, which worked for my situation. You could also try simply comparing it to the string
"\n"
.尝试这四种方法来获取带有空格的输入:)
Try this all four way to take input with space :)
我有同样的问题,我只是使用了 cin.getline(input,300); 。
noskipws
和cin.get()
有时不太好用。由于您的数组大小正确,请尝试使用 cin.getline() ,它不关心任何字符并以指定的字符数读取整行。I have the same problem and I just used
cin.getline(input,300);
.noskipws
andcin.get()
sometimes are not easy to use. Since you have the right size of your array try usingcin.getline()
which does not care about any character and read the whole line in specified character count.