Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6160087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:14:30+00:00 2026-05-23T21:14:30+00:00

I’d like to use stl::set to put some directory paths. The set has some

  • 0

I’d like to use stl::set to put some directory paths.
The set has some special directory paths which I put.
And I should find the special parent of some input paths.

There is the code. And I commented some points.

set<wstring> special_directories;

void WhoIsMySpecialParent(const wstring& f)
{
    set<wstring>::const_iterator it;
    it = special_directories.upper_bound(f);

    if (it == special_directories.begin())
    {
        printf("There isn't any special parent.");
        return;
    }

    --it;
    wprintf(L"The special parent is <%s>\n", it->c_str());
}

int _tmain(int argc, _TCHAR* argv[])
{
    // These are special directories which I will manage.
    // "/home" and "/home/benjamin" are not included in the special directories.
    // If I want to make "/home" as special directory,
    // I have to add "/home" in the set.
    special_directories.insert(L"/");
    special_directories.insert(L"/bin");
    special_directories.insert(L"/etc");
    special_directories.insert(L"/home/benjamin/documents");
    special_directories.insert(L"/var");
    special_directories.insert(L"/var/log");

    WhoIsMySpecialParent(L"/bin/ls"); // Okay. It prints /bin
    WhoIsMySpecialParent(L"/var/log/aaaa"); // Okay. It prints /var/log
    WhoIsMySpecialParent(L"/var/apache2"); // Okay. It prints /var

    WhoIsMySpecialParent(L"/bz");
    // Wrong. It prints "/bin". It should print "/"

    WhoIsMySpecialParent(L"/home/benjamin");
    // Wrong. It prints "/etc", It should print "/"

    return 0;
}

I thought this can be handled with upper_bound. But I might be wrong.
How do you think? Should I give up to use std::set?
If you were me, how would you solve this problem? any idea please.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T21:14:31+00:00Added an answer on May 23, 2026 at 9:14 pm

    Using std::set<T>::upper_bound() simply finds the lexicographical sorted sorted index that is the mathematical upper bound in the tree data-structure that std::set uses. In this case “/etc” was the upper bound to “home/benjamin” since if you count through the alphabet, “home/benjamin” will come before “home/benjamin/documents” and right after “/etc”. Therefore in the sorted tree, you would find that “/etc” was the least upper bound for your search parameter “home/benjamin”. If you wanted to get “/” as the result, you would have to find the greatest upper bound in the data-structure, not the least upper bound. By “greatest”, I’m talking in the mathematical sense, where a sorted tree will create a topological sort that has N number of upper-bounds for a given search string, if those upper bounds exist. The std::set<T>::upper_bound() method finds the least of these upper-bounds, meaning it finds the first possible upper-bound from a lexicographical sort (since that is the method it’s using to sort a std::string). With your “/home/benjamin” case, you are looking for the greatest upper-bound that includes a root directory that is a “special” directory. But unfortunately applying that criteria to your other cases will break some of them (i.e., it will always return “/”). That means you are going to have to create a custom version of an upper_bound() type function for your needs that does not work strictly by a lexicographical sorting of the elements to find the upper-bound. In fact I wouldn’t even use an upper_bound search.

    A better approach would be to use std::string::find_last_of(), and use that to parse your directories by the / character. Doing so, you can parse and basically “peel” back the directory paths until you find a perfect match in your std::set. So for instance:

    void WhoIsMySpecialParent(const wstring& f)
    {
        wstring temp = f;
        while (temp.size())
        {
            temp = temp.substr(0, temp.find_last_of(L"/"));
    
            set<wstring>::const_iterator it;
    
            //setup a special case for the root directory
            if (temp.size())
                it = special_directories.find(temp);
            else
                it = special_directories.find(L"/");
    
            if (it != special_directories.end())
            {
                wprintf(L"The special parent is <%s>\n", it->c_str());
                return;
            }
        }
    
        printf("There isn't any special parent.");
        return;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
I used javascript for loading a picture on my website depending on which small
I would like to count the length of a string with PHP. The string
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.