C++中getline()的用法详解
![star](https://csdnimg.cn/release/downloadcmsfe/public/img/star.98a08eaa.png)
"C++中getline()的用法详解" getline()是C++标准库函数,它有两种形式,一种是头文件<istream>中输入流成员函数;一种是在头文件<string>中普通函数。下面将详细介绍getline()的用法。 输入流成员函数getline() 输入流成员函数getline()函数语法结构:在<istream>中的getline()函数有两种重载形式: istream& getline (char* s, streamsize n ); istream& getline (char* s, streamsize n, char delim ); 作用是:从istream中读取至多n个字符(包含结束标记符)保存在s对应的数组中。即使还没读够n个字符,如果遇到delim或字数达到限制,则读取终止,delim都不会被保存进s对应的数组中。 代码实例: ```cpp #include <iostream> using namespace std; int main(){ char name[256]; cout << "Please input your name: "; cin.getline(name, 256); cout << "The result is: " << name << endl; return 0; } #include <iostream> using namespace std; int main( ){ char line[100]; cout << " Type a line terminated by 't'" << endl; cin.getline( line, 100, 't' ); cout << line << endl; return 0; } ``` 普通函数getline() 普通函数getline()函数语法结构:在<string>中的getline函数有四种重载形式: istream& getline (istream& is, string& str, char delim); istream& getline (istream&& is, string& str, char delim); istream& getline (istream& is, string& str); istream& getline (istream&& is, string& str); 函数的变量: * is:表示一个输入流,例如 cin。 * str:string类型的引用,用来存储输入流中的流信息。 * delim:char类型的变量,所设置的截断字符;在不自定义设置的情况下,遇到'\n',则终止输入 用法和上一种类似,但是读取的istream是作为参数is传进函数的。读取的字符串保存在string类型的str中。 代码实例: ```cpp #include <iostream> #include <string> using namespace std; int main(){ string name; cout << "Please input your name: "; getline(cin, name); cout << "Welcome to here!" << name << endl; return 0; } #include <iostream> #include <string> using namespace std; int main(){ string name; cout << "Please input your name: "; getline(std::cin, name, '#'); cout << "Welcome to here!" << name << endl; return 0; } ``` getline()函数可以用来从输入流中读取字符串,并且可以设置截断字符。根据不同的重载形式,可以选择适合的getline()函数来满足不同的需求。
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
- 粉丝: 7
- 资源: 945
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)
- 1
- 2
前往页