如何在 C++ 中创建空间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2765462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to cin Space in c++?
提问by Narek
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?
如何从标准输入中输入空格符号?如果你写空格,程序忽略!:( 是否有任何符号组合(例如 '\s' 或类似的东西)表示我可以从标准输入中为我的代码使用“空格”?
回答by rcollyer
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:
默认情况下,它会跳过所有空格(空格、制表符、新行等)。您可以更改其行为,也可以使用稍微不同的机制。要更改其行为,请使用 manipulator noskipws
,如下所示:
cin >> noskipws >> a[i];
But, since you seem like you want to look at the individual characters, I'd suggest using get
, like this prior to your loop
但是,由于您似乎想查看单个字符,因此我建议get
在循环之前使用, 像这样
cin.get( a, n );
Note: 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 the istream
interface here.
注意: get
如果找到换行符 ( \n
) 或在 n-1 个字符之后,将停止从流中检索字符。它提前停止,以便将空字符 ( \0
)附加到数组。您可以在此处阅读有关istream
界面的更多信息。
回答by sbi
#include <iostream>
#include <string>
int main()
{
std::string a;
std::getline(std::cin,a);
for(std::string::size_type i = 0; i < a.size(); ++i)
{
if(a[i] == ' ')
std::cout<<"It is a space!!!"<<std::endl;
}
return 0;
}
回答by Nidhin David
To input AN ENTIRE LINE containing lot of spaces you can use getline(cin,string_variable);
要输入包含大量空格的整行,您可以使用 getline(cin,string_variable);
eg:
例如:
string input;
getline(cin, input);
This format captures all the spaces in the sentence untill return
is pressed
此格式捕获句子中的所有空格,直到return
按下
回答by Marcelo Cantos
Use cin.get()
to read the next character.
使用cin.get()
读取下一个字符。
However, for this problem, it is very inefficient to read a character at a time. Use the istream::read()
instead.
但是,对于这个问题,一次读取一个字符是非常低效的。使用istream::read()
来代替。
int main()
{
char a[10];
cin.read(a, sizeof(a));
for(int i = 0; i < 10; i++)
{
if(a[i] == ' ')
cout<<"It is a space!!!"<<<endl;
}
return 0;
}
And use ==
to check equality, not =
.
并用于==
检查相等性,而不是=
.
回答by Odrade
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 use cin.get()
.
使用 cin 的 >> 运算符将删除前导空格并在第一个尾随空格处停止输入。要获取整行输入(包括空格),请尝试cin.getline()
. 要一次抓取一个字符,您可以使用cin.get()
.
回答by Airhogs777
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:
我想我会分享对我有用的答案。上一行以换行符结尾,因此这些答案中的大多数本身都不起作用。这做到了:
string title;
do {
getline(cin, title);
} while (title.length() < 2);
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"
.
那是假设输入总是至少 2 个字符长,这对我的情况有效。您也可以尝试简单地将其与 string 进行比较"\n"
。
回答by Abhishek Sahay
Try this all four way to take input with space :)
试试这四种方式来输入空格:)
#include<iostream>
#include<stdio.h>
using namespace std;
void dinput(char *a)
{
for(int i=0;; i++)
{
cin >> noskipws >> a[i];
if(a[i]=='\n')
{
a[i]='##代码##';
break;
}
}
}
void input(char *a)
{
//cout<<"\nInput string: ";
for(int i=0;; i++)
{
*(a+i*sizeof(char))=getchar();
if(*(a+i*sizeof(char))=='\n')
{
*(a+i*sizeof(char))='##代码##';
break;
}
}
}
int main()
{
char a[20];
cout<<"\n1st method\n";
input(a);
cout<<a;
cout<<"\n2nd method\n";
cin.get(a,10);
cout<<a;
cout<<"\n3rd method\n";
cin.sync();
cin.getline(a,sizeof(a));
cout<<a;
cout<<"\n4th method\n";
dinput(a);
cout<<a;
return 0;
}
回答by HamzeLue
I have the same problem and I just used cin.getline(input,300);
.
我有同样的问题,我刚刚使用了cin.getline(input,300);
.
noskipws
and cin.get()
sometimes are not easy to use. Since you have the right size of your array try using cin.getline()
which does not care about any character and read the whole line in specified character count.
noskipws
而cin.get()
有时不易使用。由于您的数组大小正确,请尝试使用cin.getline()
它不关心任何字符并以指定的字符数读取整行。