set:如何列出以给定字符串开头并以“/”结尾的非字符串?

发布于 2024-12-01 05:39:13 字数 2419 浏览 1 评论 0原文

例如,我们的集合中有:

 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 

所以这就是我基于此尝试的 给出答案

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

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

输出:

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

并且我们得到了 bin/obj/Debug/string。我们想知道:

vc100.idb 
vc100.pdb 
CloudServerPrototype/

这样的事情怎么办?

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 

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

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

This outputs:

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

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

vc100.idb 
vc100.pdb 
CloudServerPrototype/

How to do such thing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

赢得她心 2024-12-08 05:39:13

您想要做什么的快速示例。

String.find(): http://www.cplusplus.com/reference/string/ string/find/

String.subStr(): http://www.cplusplus.com/reference/string/string/substr/

string str = "bin/obj/Debug/vc100.pdb";
    string checkString ("bin/obj/Debug");

     // Check if string starts with the check string
     if (str.find(checkString) == 0){
      // Check if last letter if a "/"
      if(str.substr(str.length()-1,1) == "/"){
        // Output strating at the end of the check string and for
        // the differnce in the strings.
        cout << str.substr(checkString.length(), (str.length() - checkString.length()) ) << endl;
      }
     }

Quick example of what you want to do.

String.find(): http://www.cplusplus.com/reference/string/string/find/

String.subStr(): http://www.cplusplus.com/reference/string/string/substr/

string str = "bin/obj/Debug/vc100.pdb";
    string checkString ("bin/obj/Debug");

     // Check if string starts with the check string
     if (str.find(checkString) == 0){
      // Check if last letter if a "/"
      if(str.substr(str.length()-1,1) == "/"){
        // Output strating at the end of the check string and for
        // the differnce in the strings.
        cout << str.substr(checkString.length(), (str.length() - checkString.length()) ) << endl;
      }
     }
巷子口的你 2024-12-08 05:39:13

目前尚不清楚您遇到了问题的哪一部分,因此这里有一个入门指南。

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

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

要将这些部分插入到新集合(输出)中以保证唯一性,您可以使用以下命令:

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

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

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

对结果进行排序留作练习。

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

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

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

You can then call it this way:

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

To output the new set:

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

Sorting the results is left as an exercise.

暮色兮凉城 2024-12-08 05:39:13

我能想到的最简单的方法,使用标准 C 函数,是:

char * string1 = "bin/obj/Debug"
char * string2 = "bin/obj/Debug/CloudServerPrototype/rc.write.1.tlog"
char result[64];
// the above code is just to bring the strings into this example

char * position = strstr(string1, string2);
int substringLength;
if(position != NULL){
    position += strlen(string2);
    substringLength = strchr(position, '/') - position;
    strncpy(result, position, substringLength);
}else{
    strcpy(result, string1); // this case is for when your first string is not found
}

cout << result;

首先发生的事情是在我们正在分析的字符串中查找子字符串 string1,即 string2< /代码>。一旦我们找到起点,并假设它在那里,我们使用指针算术将该子字符串的长度添加到该起点,然后通过从结束位置减去起始位置来找到结果字符串的长度,这是找到的与 strchr(position, '/')。然后我们只需将该子字符串复制到缓冲区中,然后使用 cout 进行打印。

我确信有一种奇特的方法可以用 std::string 来做到这一点,但我会把它留给任何能够更好地解释 C++ 字符串的人,我从来没有设法适应它们,哈哈

The easiest way I can think of, using the standard C functions, would be:

char * string1 = "bin/obj/Debug"
char * string2 = "bin/obj/Debug/CloudServerPrototype/rc.write.1.tlog"
char result[64];
// the above code is just to bring the strings into this example

char * position = strstr(string1, string2);
int substringLength;
if(position != NULL){
    position += strlen(string2);
    substringLength = strchr(position, '/') - position;
    strncpy(result, position, substringLength);
}else{
    strcpy(result, string1); // this case is for when your first string is not found
}

cout << result;

The first thing that occurs, is finding the substring, string1, in the string we are analyzing, being string2. Once we found the starting point, and assuming it was there at all, we add the length of that substring to that starting point using pointer arithmatic, and then find the resulting string's length by subtracting the starting position from the ending position, which is found with strchr(position, '/'). Then we simply copy that substring into a buffer and it's there to print with cout.

I am sure there is a fancy way of doing this with std::string, but I'll leave that to anyone who can better explain c++ strings, I never did manage to get comfortable with them, haha

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文