Home > OS >  How to split a path to subpaths and store it in std::vector?
How to split a path to subpaths and store it in std::vector?

Time:05-26

My question is how could I split my main path into several separate sub-paths and store it in a vector or list?

Example

I have path for example:

Assets/A/B/C

And I need to break them into separate sub-paths (or just strings):

Assets
A
B
C

And then stores it in std::vector. The last part i know how to do just push_back(s) where s is each subpath/string, but how to get this s?

Is there such a possibility in the standard library std::filesystem or do I need to find positions of two slashes myself and get a string within the boundaries of the first and next slash?

CodePudding user response:

A std::filesystem::path is iterable, so you can simply write:

for(auto p:path)
    vector.push_back(p.string());

Or even shorter:

std::vector(path.begin(),path.end());

If you do not need strings, but a vector of subpaths is also accaptable.

  • Related