C++ 获取路径第一个字符串的简单方法
C++ 获取路径第一个字符串的简单方法
在 C++ 中,可以使用字符串流 istringstream 来获取路径中的第一个字符串。具体操作如下:
- 包含头文件
<sstream> - 定义一个字符串路径
path - 将路径
path以空格为分隔符,分割成多个子字符串,存放在istringstream对象中 - 使用
istringstream对象的>>运算符,读取第一个子字符串
示例代码如下:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
string path = "/usr/local/bin";
istringstream iss(path);
string first;
iss >> first;
cout << "The first string in the path is: " << first << endl;
return 0;
}
输出结果为:
The first string in the path is: /usr/local/bin
原文地址: https://www.cveoy.top/t/topic/mQ22 著作权归作者所有。请勿转载和采集!