【发布时间】:2015-11-08 05:53:06
【问题描述】:
假设我有以下文件夹
std::string m("C:\MyFolderA\MyFolderB\MyFolderC");
boost::filesystem::path p(m);
无论如何我可以提取此文件夹的父级。我想从上面的路径中获取字符串MyFolderB.。
【问题讨论】:
标签: c++ boost-filesystem
假设我有以下文件夹
std::string m("C:\MyFolderA\MyFolderB\MyFolderC");
boost::filesystem::path p(m);
无论如何我可以提取此文件夹的父级。我想从上面的路径中获取字符串MyFolderB.。
【问题讨论】:
标签: c++ boost-filesystem
有方法parent_path,查看文档。
【讨论】:
或者,如果您更喜欢字符串操作方法。
#include <algorithm>
const std::string m("C:\\MyFolderA\\MyFolderB\\MyFolderC");
const std::string slash("\\");
auto last_slash(std::find_end(std::cbegin(m),
std::cend(m),
std::cbegin(slash),
std::cend(slash)));
auto second_to_last_slash(std::find_end(std::cbegin(m),
last_slash,
std::cbegin(slash),
std::cend(slash)));
const std::string parent(++second_to_last_slash, last_slash);
Live on Coliru,如果你感兴趣的话。
【讨论】:
m,如果slash 在Linux 上下文中被#ifdefed 初始化为"/"。