Pergunta

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.

Foi útil?

Solução

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(); }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top