一.摘要
本菜雞昨晚做題的時候發現不會接收含空格和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 }
運行結果:
三.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的 gets() 就不介紹了,聽說有啥bug,其他的方法應該還有,后面再更吧~