C++输入
整行读取 std::getline string s; getline(cin, s); cout << s << endl; 利用 scanf 的正则特性 char s[100]; scanf("%[^\n]%*c", s); printf("%s\n", s); std::gets (deprecated) char s[100]; gets(s); printf("%s\n", s); cin.get (std::basic_istream::get) char s[100]; cin.get(s,100); printf("%s\n", s); cin.getline (std::basic_istream::getline) char s[100]; cin.getline(s, 100); printf("%s\n", s); 比较常用的自然是方法1,简单省事儿。 比较一下上述方法4和方法5: cin.get 可以有1个、2个、3个…参数,这里用的是2个参数的形式。 此时它和cin.getline的两个形参是相同格式的: basic_istream& get(char_type* s, streamsize count) 不同点在于: 1) 输入字符串不超过指定值时:(包含直接按回车的情况) get(char*, size) 遇到 '\n' 停止,但不会从输入流中删去