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 7931879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:50:58+00:00 2026-06-03T20:50:58+00:00

I have a TreeVertex class: // TreeVertex.h #ifndef __TREEVERTEX__ #define __TREEVERTEX__ #include <list> using

  • 0

I have a TreeVertex class:

// TreeVertex.h
#ifndef __TREEVERTEX__
#define __TREEVERTEX__

#include <list>

using namespace std;

class TreeVertex {
public:
    TreeVertex(list<int>, TreeVertex* = NULL);
    list<int> getItemset();
private:
    list<int> Itemset;

    TreeVertex * Parent;
    TreeVertex * LeftChild;
    TreeVertex * RightSibling;
};

#endif // __TREEVERTEX__

// TreeVertex.cpp

#include "TreeVertex.h"

TreeVertex::TreeVertex(list<int> Itemset, TreeVertex* Parent) : Itemset(Itemset),     Parent(Parent), LeftChild(NULL),
    RightSibling(NULL) { }

list<int>
TreeVertex::getItemset() {
    return Itemset;
}

And a main function like this:

#include <iostream>
#include "TreeVertex.h"

using namespace std;

int main (int argc, const char ** const argv)
{    
    list<int> tmpList1;
    tmpList1.push_back(1);

    TreeVertex * tmpTreeVert1 = new TreeVertex(tmpList1);

    list<int> tmpList2;
    tmpList2.push_back(2);

    TreeVertex * tmpTreeVert2 = new TreeVertex(tmpList2);

    list<int> newVertItemset;

    newVertItemset.push_back(tmpTreeVert1->getItemset().front());
    newVertItemset.push_back(tmpTreeVert2->getItemset().front());

    cout << newVertItemset.front() << " " << newVertItemset.back() << endl;

    TreeVertex * newTreeVert = new TreeVertex(newVertItemset);

    cout << newTreeVert->getItemset().front() << " " << newTreeVert->getItemset().back() << endl;

    for (list<int>::iterator it = newTreeVert->getItemset().begin(); it != newTreeVert->getItemset().end(); ++it) {
        cout << (*it) << " ";
    }

    cout << endl;

    cout << newTreeVert->getItemset().size() << endl;
    return 0;
}

The output looks like this:

1 2

1 2

2

2

The next to the last output (the first single “2”), should be “1 2” just like the others.

Any ideas why the iterator is not going over the first element?

Thanks.

  • 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-03T20:50:59+00:00Added an answer on June 3, 2026 at 8:50 pm

    The problem with this:

    list<int>
    TreeVertex::getItemset() {
        return Itemset;
    }
    

    Everytime you call this function, it returns a copy of the object, which means the following loop should not work:

    for (list<int>::iterator it = newTreeVert->getItemset().begin(); 
                             it != newTreeVert->getItemset().end(); ++it) {
    

    as it compares iterators from two different objects. A solution is to return reference as:

    list<int> &   //<--- return reference, not copy
    TreeVertex::getItemset() {
        return Itemset;
    }
    

    But a better solution is to remove getItemset altogether and instead of that, add begin() and end() member functions as:

    //define these typedefs first in the public section
    typedef list<int>::iterator iterator;
    typedef list<int>::const_iterator const_iterator;  
    
    iterator begin() { return itemSet.begin(); }
    iterator end() { return itemSet.end(); }
    

    and then write the for loop as:

    for(TreeVertex::iterator it = newTreeVert->begin(); 
                             it != newTreeVert->end(); ++it) {
    

    If you can use C++11, then you should add these:

    //note : the function names start with `c`
    const_iterator cbegin() const { return itemSet.cbegin(); }
    const_iterator cend() const { return itemSet.cend(); }
    

    Or, if you use C++03 (and cannot use C++11), then add these:

    const_iterator begin() const { return itemSet.begin(); }
    const_iterator end() const { return itemSet.end(); }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have you ever had to justify the choice over using .NET instead of Java
I made a class Tree (abstraction for mathematical expression). It have nested class 'Vertex'
**Have it working now. I forgot to populate the Array List. How embarrassing. I'm
Have some code: using (var ctx = new testDataContext()) { var options = new
Have written all the code in a silverlight class library (dll) and linked this
Have any one tried to activate fancybox thumbnail gallery using a button or an
Have just started using Google Chrome , and noticed in parts of our site,
Have just started using Visual Studio Professional's built-in unit testing features, which as I
Have started playing with Xcode 4.2, and created a single page application using storyboard
Have a rails app that is supposed to display a list of products/managers. After

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.