C/C++中输入带空格的字符串用法总结

版权声明:未经允许不得私自转载,一旦发现严惩不贷!!! https://blog.csdn.net/Oneplus6/article/details/79312559
(1)gets(char *str)
    需要包含头文件#include <stdio.h>
(2)scanf("%[^\n]]",str)
    需要包含头文件#include <stdio.h>,这种方法需要对正则表达式有一定的理解,例如:scanf("%[a-z A-Z 0-9]",str)表示只匹配输入是大小写字母和数字。
(3)getline(cin,string str)
    需要包含头文件#include <string>,因为getline是string类成员对象,例如string::getline,其中第一个参数要求是输入流对象的引用&istream.
(4)cin.getline(char *str, int maxnum)
    需要包含头文件#include <iostream>,因为这里的getline是输入流的成员对象,如:istream::getline.

猜你喜欢

转载自blog.csdn.net/Oneplus6/article/details/79312559