#include <iostream>
#include <numeric>
#include <vector>
#include <cstdlib>
#include <cstring>
// note: for c++20 `std::vector<std::string> &a` needs to be `std::vector<std::string> &&a`
int main()
{
std::string pathstr="/bin:/usr/bin:/usr/local/bin";
auto paths = std::accumulate(pathstr.begin(), pathstr.end(),
std::vector<std::string>(),
[](std::vector<std::string> &a, const char &b)
-> std::vector<std::string>&
{
auto new_elem = [&a](const char *p)
{
a.emplace_back();
auto count = strcspn(p,":");
//std::cerr << "reserving " << count << " bytes (with +1 for null)" << std::endl;
a.back().reserve(count+1);
};
if(!a.size())
new_elem(&b);
if(':'==b)
new_elem(&b+1);
else
a.back() += b;
return a;
});
for(auto &path : paths)
std::cout << path << std::endl;
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: