getline 切割字符串
1、使用 iostream 中 getline 函数截取 string
函数原型:
std::istream &std::getline<char, std::char_traits<char>, std::allocator<char>>(std::istream &_Istr, std::string &_Str, char _Delim)
getline(istream && is,string&str,char delim)
/* 需要使用的头文件 */
#include <iostream>
//getline 函数
#include <sstream>
//stringstream 类
using namespace std;
int main () {
string str = "/home/name/Desktop/";
string res;
stringstream sstr(str);
while (getline(sstr, res, '/')) {
cout << res << endl;
}
}
输出:
空字符
home
name
Desktop