[使用boost :: filesystem提取目录的父文件夹

问题描述 投票:6回答:2

假设我有以下文件夹

std::string m("C:\MyFolderA\MyFolderB\MyFolderC");
boost::filesystem::path p(m);

反正我有没有要提取此文件夹的父文件夹。我想从上述路径中获取字符串MyFolderB.

c++ boost-filesystem
2个回答
7
投票

有方法parent_path,请检查文档。


1
投票

或者,如果您喜欢字符串操作方法。

#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,如果您对此感兴趣。

© www.soinside.com 2019 - 2024. All rights reserved.