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

  • Home
  • SEARCH
  • 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 8922019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:43:20+00:00 2026-06-15T06:43:20+00:00

In my application I have a (unbalanced) tree datastructure. This tree is simply made

  • 0

In my application I have a (unbalanced) tree datastructure. This tree is simply made of “std::list of std::lists” – node holds an arbitrary “list” of sub-nodes. Using this instead of a single list made the rest of the application a lot easier. (The program is about changing moving nodes from one tree to another tree / another part in the tree / to it’s own tree).

Now an obvious task is to find a subtree inside a “tree”. For non-recursive searches it is simple enough:

subtree_iterator find_subtree(const N& n) {         
    auto iter(subtrees.begin());
    auto e(subtrees.end());
    while (iter != e) {
        if ((*iter)->name == n) {
            return iter;
        }
        ++iter;
    }
    return e;
}

Which returns an iterator to the subtree position. The problem however starts when I try to implement a multi-level search. Ie, I wish to search for hello.world.test where the dots mark a new level.

Searching worked alright

subtree_iterator find_subtree(const pTree_type& otree, std::string identify) const {
    pTree_type tree(otree);
    boost::char_separator<char> sep(".");
    boost::tokenizer<boost::char_separator<char> > tokens(identify, sep);
    auto token_iter(tokens.begin());
    auto token_end(tokens.end());

    subtree_iterator subtree_iter;
    for (auto token_iter(tokens.begin()); token_iter != token_end; ++token_iter) {
        std::string subtree_string(*token_iter);
        subtree_iter = tree->find_subtree_if(subtree_string);
        if (subtree_iter == tree->subtree_end()) {
            return otree->subtree_end()
        } else {
            tree = *subtree_iter;
        }
    }
    return subtree_iter;
}

On first glace it seemed to work “correct”, however when I try to use it, it fails. Using it would be like

auto tIn(find_subtree(ProjectTree, "hello.world.test"));
if (tIn != ProjectTree->subtree_end()) {
    //rest
}

however that gives a debug assertion error “list iterators not compatible”. This isn’t too weird: I’m comparing a iterators from different lists to each other. However I could I implement such a thing? My “backup” option would be to return a std::pair<bool,iterator> where the boolean part determines if the tree actually exists. Is there another method, short of making the whole tree single list?

  • 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-06-15T06:43:22+00:00Added an answer on June 15, 2026 at 6:43 am

    You should not work on iterators internaly. Use nodes instead.

    template <typename T>
    struct Node {
     T item;
     Node<T>* next;
    };
    

    Then encapsulate your Node in an iterator facade like this :

    template<typename T>
    class iterator {
    private:
      Node<T>* node;
    public:
      ...
    };
    

    Then use a generic invalid node (when node is nullptr) that is returned whenever end() is reached or returned.
    Note that what i suggest is a single linked list (not double linked list as the standard one). this is because you can’t go back from an invalid generic end() iterator that point to an invalid null node.
    If you don’t use iterator operator–() in your algorithms this should be fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have application with needs to have access to some sensitive data(in this case
I'm starting work with PHPUnit with Kohana. My application have many controllers which simply
I have a web application written in ASP.NET MVC 3 . This application have
Does node.js (considering a web application) have any preferred UI framework? Or it's an
In my asp.net application have one data list, page load event I have to
I have an application in C#, i want to implement perfcounters in this application
Users of my application have the possibility of choosing some values from list. The
My application have something like this: TabActivity Tab 1 (ActivityGroup) Activity > Activity >
My application have performance issues, so i started to investigate this from the root:
My application have a service and an activity. Sometimes the service will send a

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.