C++ 获取路径斜杠分割的第一个字符串
可以使用字符串流 (stringstream) 和 getline 函数来实现获取路径斜杠分割的第一个字符串:
#include <iostream>
#include <sstream>
#include <string>
int main() {
std::string path = '/usr/local/bin';
std::stringstream ss(path); // 将路径字符串转换为字符串流
std::string first;
std::getline(ss, first, '/'); // 使用getline函数获取第一个斜杠分割的字符串
std::cout << first << std::endl; // 输出结果:usr
return 0;
}
在上面的代码中,我们将路径字符串转换为字符串流,然后使用 getline 函数获取第一个斜杠分割的字符串,存储在变量 first 中。最后输出 first 的值即可。
原文地址: https://www.cveoy.top/t/topic/mQ3q 著作权归作者所有。请勿转载和采集!