C++字符串函数getline
时间: 2023-11-14 10:55:36 浏览: 92
C++字符串函数getline有两种形式。第一种是iostream类的成员函数,在头文件<istream>中。第二种是普通函数,在头文件<string>中。
第一种形式的函数原型是:
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是输入流对象,str是存储读取的字符串的变量,delim是定界符,用来指定分割字符串的字符。
对于第二种形式的getline函数,可以使用istringstream来将输入的字符串按照指定的定界符进行分割。以下是一个示例代码:
```cpp
#include <string>
#include <sstream>
#include <vector>
vector<string> split(string& folder) {
stringstream s(folder);
string curLayer;
vector<string> result;
while (getline(s, curLayer, '/')) {
result.push_back(curLayer);
}
return result;
}
```
这段代码将输入的字符串按照"/"进行分割,并将分割后的结果存储在一个vector<string>中。
总结来说,C++的字符串函数getline可以用于从输入流中读取字符串,并按照指定的定界符进行分割。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)