std :: transform如何不返回(也不抛出),只是跳过?

Rel*_*lla 3 c++ string search transform set

我想显示显示的文件夹内容(不具有任何文件夹系统),因此,在其中std::set<string_file_names>具有std::strings和某些目录的给定路径时,我们希望像在普通fs中一样搜索文件夹内容。因此具有:

    set<string> demo_set;
demo_set.insert("file1");
demo_set.insert("file2");
demo_set.insert("folder/file1");
demo_set.insert("folder/file2");
demo_set.insert("folder/folder/file1");
demo_set.insert("folder/folder/file2");
demo_set.insert("bin/obj/Debug/CloudServerPrototype/ra.write.1.tlog");
demo_set.insert("bin/obj/Debug/CloudServerPrototype/rc.write.1.tlog");
demo_set.insert("bin/obj/Debug/vc100.idb");
demo_set.insert("bin/obj/Debug/vc100.pdb");
Run Code Online (Sandbox Code Playgroud)

和搜索字符串,"bin/obj/Debug/"我们想要得到3个项目-文件夹,2个文件:

CloudServerPrototype/
vc100.idb
vc100.pdb
Run Code Online (Sandbox Code Playgroud)

但是我们也得到一个空行。如果找不到物品,如何获取它以及如何引发错误?

完整代码:

#include <iostream>
#include <algorithm>
#include <set>
#include <string>
#include <iterator>

using namespace std;

struct get_pertinent_part
{
    const std::string given_string;

    get_pertinent_part(const std::string& s)
        :given_string(s)
    {
    }

    std::string operator()(const std::string& s)
    {
        std::string::size_type first = 0;

        if (s.find(given_string) == 0)
        {
            first = given_string.length();
        }
        else
        {
            return "";
        }

        std::string::size_type count = std::string::npos;
        std::string::size_type pos = s.find_last_of("/");
        if (pos != std::string::npos && pos > first)
        {
            count = pos + 1 - first;
        }

        return s.substr(first, count);
    }
};

void directory_listning_without_directories_demo()
{
    set<string> output;
    set<string> demo_set;

    demo_set.insert("file1");
    demo_set.insert("file2");
    demo_set.insert("folder/file1");
    demo_set.insert("folder/file2");
    demo_set.insert("folder/folder/file1");
    demo_set.insert("folder/folder/file2");
    demo_set.insert("bin/obj/Debug/CloudServerPrototype/ra.write.1.tlog");
    demo_set.insert("bin/obj/Debug/CloudServerPrototype/rc.write.1.tlog");
    demo_set.insert("bin/obj/Debug/vc100.idb");
    demo_set.insert("bin/obj/Debug/vc100.pdb");


    std::transform(demo_set.begin(),
        demo_set.end(),
        std::inserter(output, output.end()),
        get_pertinent_part("bin/obj/Debug/"));

    std::copy(output.begin(),
        output.end(),
        std::ostream_iterator<std::string>(std::cout, "\n"));
}

int main()
{
    directory_listning_without_directories_demo();
    cin.get();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

基于炉排答案的代码示例。

Ayj*_*jay 5

由于没有transform_if,执行此操作的正确方法是首先将具有非零长度get_pertinent_part的路径的copy_if结果放入另一个容器,然后在该新容器上运行转换。

另外,您可以编写诸如transform_if之类的内容,将您的转换函数的结果与谓词进行比较。这是我未经测试的照片:

template <class InIt, class OutIt, class UnaryFunction, class Predicate>
void transform_if_value(InIt first, InIt last, OutIt out, UnaryFunction fn, Predicate pred)
{
    for ( ; first != last; ++ first)
    {
        auto val = fn(*first);
        if (pred(val))
            *out++ = val;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后你可以像这样使用

transform_if_value(
    demo_set.begin(),
    demo_set.end(),
    std::inserter(output, output.begin()),
    get_pertinent_part("bin/obj/Debug/"),
    [](const std::string& s) {return !s.empty();});
Run Code Online (Sandbox Code Playgroud)

有关更酷的语法,请查看boost的Range适配器:

boost::copy(
    demo_set | boost::adaptors::transformed(get_pertinent_part("bin/obj/Debug/"))
             | boost::adaptors::filtered([](const std::string& s) {return !s.empty();}),
    std::inserter(output, output.begin()));
Run Code Online (Sandbox Code Playgroud)