Eri*_*son 0 c++ iterator list listiterator
我有一个TreeVertex类:
// 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;
}
Run Code Online (Sandbox Code Playgroud)
和这样的主要功能:
#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;
}
Run Code Online (Sandbox Code Playgroud)
输出如下所示:
1 2
1 2
2
2
最后一个输出(第一个单词"2")的旁边应该是"1 2",就像其他输出一样.
任何想法为什么迭代器不会超过第一个元素?
谢谢.
这个问题:
list<int>
TreeVertex::getItemset() {
return Itemset;
}
Run Code Online (Sandbox Code Playgroud)
每次调用此函数时,它都会返回该对象的副本,这意味着以下循环不起作用:
for (list<int>::iterator it = newTreeVert->getItemset().begin();
it != newTreeVert->getItemset().end(); ++it) {
Run Code Online (Sandbox Code Playgroud)
因为它比较来自两个不同对象的迭代器.解决方案是将引用返回为:
list<int> & //<--- return reference, not copy
TreeVertex::getItemset() {
return Itemset;
}
Run Code Online (Sandbox Code Playgroud)
但更好的解决方案是getItemset完全删除,而不是添加begin()和end()成员函数:
//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(); }
Run Code Online (Sandbox Code Playgroud)
然后将for循环写为:
for(TreeVertex::iterator it = newTreeVert->begin();
it != newTreeVert->end(); ++it) {
Run Code Online (Sandbox Code Playgroud)
如果您可以使用C++ 11,那么您应该添加以下内容:
//note : the function names start with `c`
const_iterator cbegin() const { return itemSet.cbegin(); }
const_iterator cend() const { return itemSet.cend(); }
Run Code Online (Sandbox Code Playgroud)
或者,如果您使用C++ 03(并且不能使用C++ 11),那么添加以下内容:
const_iterator begin() const { return itemSet.begin(); }
const_iterator end() const { return itemSet.end(); }
Run Code Online (Sandbox Code Playgroud)