一.摘要

  本菜鸡昨晚做题的时候发现不会接收含空格和tab的字符串,固有本随笔;

二.cin.get(char *str, int count)

示例代码:

 1 /*cin.get()接收含空格、tab的字符串*/
 2 #include<iostream>
 3 #include<string>
 4 using namespace std;
 5 int main() {
 6     char c[1000];    //不能直接 char *c,不能使用未初始化的变量
 7     cin.get(c, 1000);
 8     string word(c);    //使用c创建字符串
 9     cout << "字符数组c:\t" << c << endl;
10     cout << "字符串word:\t" << word << endl;
11     system("pause");
12     return 0;
13 }

运行结果:

 C++接收含空格、tab的字符串

三.getline(std::istream &io,string &str);

示例代码:

 1 /*getline接收含空格、tab的字符串*/
 2 #include<iostream>
 3 #include<string>
 4 using namespace std;
 5 int main() {
 6     string s;
 7     getline(cin, s);
 8     cout << "字符串s:\t" << s << endl;
 9     system("pause");
10     return 0;
11 }

运行结果:

C++接收含空格、tab的字符串

四.总结

  c的 gets() 就不介绍了,听说有啥bug,其他的方法应该还有,后面再更吧~

相关文章:

  • 2022-12-23
  • 2021-07-29
  • 2022-01-02
  • 2022-12-23
  • 2022-02-07
  • 2021-10-15
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-15
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案