set: how to list not strings starting with given string and ending with `/`?

后端 未结 3 938
忘掉有多难
忘掉有多难 2020-12-22 08:42

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/vc10         


        
3条回答
  •  情书的邮戳
    2020-12-22 09:16

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

    Sorting the results is left as an exercise.

提交回复
热议问题