可以使用字符串流(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://cveoy.top/t/topic/boiR 著作权归作者所有。请勿转载和采集!