简体   繁体   English

如何在C ++中将某些字符复制到字符串中

[英]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' ,则我希望字符串coutcout上是"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.

相关问题 如何将字符数组复制到字符串 C++ - How to copy an array of characters to a string C++ c++ - 如何在c ++中删除字符串中的某些字符? - How can I delete certain characters in a string in c++? 如何将网站中的文本复制到 C++ 中的字符串? - How can I copy the text from a website to a string in c++? 如何将字符串的内容复制到 char c++ 的数组中? - How can I copy the contents of string to an array of char c++? 如何将一个字符数组中的前i个字符复制到C ++中的另一个字符数组中? - How can I copy the first i amount of characters in a character array into another character array in C++? 如何通过套接字将带有Unicode字符的Java字符串发送到C ++,而没有奇怪的字符? - How can I send a Java string with Unicode characters to C++ via socket, without strange characters? 如何设置Visual C ++ 2010 Express在构建过程中复制某些文件夹? - How can I setup Visual C++ 2010 Express to copy some folders during the build process? Function 转义 C++ 字符串的一些字符 - Function to escape some characters for C++ string 我可以在C ++中使用这个C风格的字符串获得一些帮助吗? - Can I get some assistance with this C style string in C++? 在 C++ 中,如何从字符串中获取接下来的几个字符? - In c++, how can I grab the next few characters from a string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM