set<string>:如何列出不以给定字符串开头并以`/`结尾的字符串?

2022-01-17 00:00:00 string set search c++

例如我们在我们的集合中:

for example we have in our set:

 bin/obj/Debug/CloudServerPrototype/ra.write.1.tlog 
 bin/obj/Debug/CloudServerPrototype/rc.write.1.tlog 
 bin/obj/Debug/vc100.idb 
 bin/obj/Debug/vc100.pdb 

所以这就是我根据这个 grate answer:

So this is what I tried based on this grate answer:

#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() + 1;
        }

        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, "
"));
}

int main()
{
    directory_listning_without_directories_demo();
    cin.get();
    return 0;
}

这个输出:

CloudServerPrototype/
file1
file2
folder/
folder/folder/
vc100.idb
vc100.pdb

我们得到了 bin/obj/Debug/ 字符串.我们要cout:

and we are given with bin/obj/Debug/string. We want to cout:

vc100.idb 
vc100.pdb 
CloudServerPrototype/

这样的事情怎么办?

推荐答案

不清楚你卡在哪一部分的问题,所以这里给你一个入门.

It's not clear with which part of the problem you are stuck, so here is a starter for you.

获取给定字符串"和最终的/"(如果存在)之间的字符串部分:

To get the parts of the strings between "given string" and the final '/' (where present):

std::string get_pertinent_part(const std::string& s)
{
    std::string::size_type first = 0;
    if (s.find(given_string) == 0)
    {
        first = given_string.length() + 1;
    }

    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);
}

要将这些部分插入新集合(output)以保证唯一性,您可以使用以下内容:

To insert these parts into a new set (output) to guarantee uniqueness you can use the following:

std::transform(your_set.begin(),
               your_set.end(),
               std::inserter(output, output.end()),
               get_pertinent_part);

您可能希望将 given_string 传递给 get_pertinent_part(),在这种情况下,您需要将其转换为仿函数:

You may wish to pass given_string into get_pertinent_part(), in which case you'll need to convert it to a functor:

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;

        //
        // ...same code as before...
        //

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

你可以这样称呼它:

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

输出新的set:

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

将结果排序留作练习.

相关文章