[英]how can I copy some characters into string in c++
In one part of my code I should get characters one by one. 在代码的一部分中,我应该一个接一个地获得字符。 It's easy but my question is how can I add these characters one by one into a string.
这很容易,但是我的问题是如何将这些字符一个接一个地添加到字符串中。 Note that I don't know how many characters I will get.
请注意,我不知道会得到多少个字符。 It is important that just the characters be copied in string.
重要的是仅将字符复制为字符串。 In the other words, I want to generate words from characters until the character is not equal to
' '
or '\\n'
. 换句话说,我想根据字符生成单词,直到字符不等于
' '
或'\\n'
为止。 the wrong code that I wrote is: 我写的错误代码是:
char c;
string str = NULL;
cin.get(c);
while (c != " ")
{
str += c;
cin.get(c);
}
cout << str ;
For example, if character c
would be 'H'
at first and then be 'i'
, I want to the string str to be "Hi"
on cout
! 例如,如果字符
c
首先是'H'
,然后是'i'
,则我希望字符串cout
在cout
上是"Hi"
!
=!
should be !=
, and the string " "
should be a character ' '
. 应该是
!=
,字符串" "
应该是字符' '
。
If you want to check for new-line characters as well as space: 如果要检查换行符和空格:
while (c != ' ' && c != '\n')
or perhaps 也许
while (!std::isspace(c))
which will read up to any whitespace character. 它将读取最多任何空白字符。
You should also check for the end of the stream or other problems: 您还应该检查流的结尾或其他问题:
while (cin.get(c) && !std::isspace(c)) {
str += c;
}
如果您只想阅读直到空格或换行符:
std::getline( cin, str, ' ' );
c++ stringstreams are also useful when building strings C ++ stringstreams建立字符串时也很有用
stringstream ss;
while (cin.get(c) && !std::isspace(c)) {
ss << c;
}
string s = ss.str();
cout << s;
The mistake in your code was that you weren't checking for the newline character. 代码中的错误是您没有检查换行符。 Also, it should have been
!=
instead of =!
同样,它应该是
!=
而不是=!
. 。 The second option will actually behave as
c = (!(" "))
in your code. 第二个选项在您的代码中实际上将表现为
c = (!(" "))
。 Another mistake is that you should be checking for the blank space character ' '
instead of an empty string " "
. 另一个错误是您应该检查空格字符
' '
而不是空字符串" "
。
Here is the correct code: 这是正确的代码:
char c;
string str = "";
while (true)
{
cin.get(c);
cout << "c is " << c << endl;
if ((c == ' ') || (c == '\n'))
break;
str += c;
}
cout << str << endl ;
Also, if your requirement is that the I/O be stopped the moment a space character is encountered, refer to this question: C - Reading from stdin as characters are typed 另外,如果您的要求是在遇到空格字符时停止I / O,请参考以下问题: C-在键入字符时从stdin读取
All the solutions proposed here will continue to read input until the new line character is entered because until then your input hasn't been processed, but instead stored in a buffer. 此处提出的所有解决方案将继续读取输入,直到输入换行符为止,因为在此之前,尚未处理您的输入,而是将其存储在缓冲区中。
If your str
starts out empty, this is simple: 如果您的
str
开头为空,这很简单:
string str;
cin >> str; // cin's >> operator already reads until whitespace
cout << str;
If you don't know in advance how many characters it would be, I would declare a std::list, push elements to it and then copy them into a string. 如果您事先不知道它将有多少个字符,我将声明一个std :: list,将元素压入其中,然后将它们复制到字符串中。 This ensures that you don't reallocate the string memory on every addition:
这样可以确保您不会在每次添加时都重新分配字符串内存:
char c;
list<char> buff;
cin.get(c);
while (c != ' ' && c != '\n')
{
buff.push_back (c);
cin.get(c);
}
string str (buff.size(), 0);
size_t i = 0;
while (!buff.empty())
{
str[i] = buff.front();
buff.pop_front();
++i;
}
cout << str ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.