你正在嘗試做的是一個
即將推出的
C++的當前特性被稱爲「初始化器列表」,其中一個向量或列表可以用= {}初始化。 我不知道他們是否在TR1中出現過它。也許它在TR2中。
#include <vector>
#include <list>
#include <iostream>
using namespace std;
int main(void) {
vector<int> vi = {1, 2, 3, 4, 5};
list<int> li = {5, 4, 3, 2, 1, 0};
cout<<"vector size="<<vi.size()<<", list size="<<li.size()<<endl;
return 0;
}
您使用的代碼看起來不合適。如果要實現包含結構(樹)的結構,請在節點中包含指向結構/節點的指針列表(或者只是指向無法實現的無效指針)。
大多數菜單結構本質上是一個有序的基於列表的樹(n個節點在一個地方,但可能是其他地方的m個節點等)。 Robert Sedgewick編寫了一本教科書「C++中的算法」。
#include <vector>
#include <iterator>
#include <string>
void * pRoot = NULL; //pointer to CTree
class CTreenode;
class CTree;
class CTree {
public:
vector<class CTreeNode> lctnNodeList; //list does not have at() or operator[]
vector<class CTreeNode>::iterator lctni;
public:
CTree() {}
~CTree() {
for (lctni=lctnNodeList.begin(); lctni != lctnNodeList.end(); nctni++) {
if (NULL==lctni->getChildPtr()) {
//do nothing, we have already done all we can
} else {
delete (CTree *)lctnNodeList.pChild;
}
//do action here
}
}
void addToList(string& data, CTree * p) {
CTreeNode ctn(data, p);
lctnNodeList.push_back(d);
}
void eraseAt(size_t index) {
vector<class CTreeNode>::iterator i = lctnNodeList.begin();
vector<class CTreeNode>::iterator i2 = lctnNodeList.begin();
i2++;
size_t x;
for (x=0; x <= index; x++,i++,i2++) {
if (index == x) {
lctnNodeList.erase(i,i2);
break;
}
}
}
void at(size_t index, string& returndata, CTree * &p) {
vector<class CTreeNode>::iterator i = lctnNodeList.begin();
size_t x;
for (x=0; x <= index; i++,x++) {
if (x==index) {
i->getData(returndata, p);
break;
}
}
}
const CTreeNode& operator[](const size_t idx) {
if (idx < lctnNodeList(size)) {
return lctnNodeList.at(idx);
} else {
//throw an error
}
}
const size() {
return lctnNodeList.size();
}
//this can be applied to the root of the tree (pRoot).
doActionToThisSubtree(void * root) {
CTree * pct = (CTree *)root;
for (pct->lctni=pct->lctnNodeList.begin(); pct->lctni != pct->lctnNodeList.end(); pct->nctni++) {
//this is a depth-first traversal.
if (NULL==pct->lctni->getChildPtr()) {
//do nothing, we have already done all we can
//we do this if statement to prevent infinite recursion
} else {
//at this point, recursively entering child domain
doActionToThisSubtree(pct->lctni->getChildPtr());
//at thisd point, exiting child domain
}
//do Action on CTreeNode node pct->lctni-> here.
}
}
};
class CTreeNode {
public:
CTree * pChild; //CTree *, may have to replace with void *
string sData;
public:
CTreeNode() : pChild(NULL) {}
CTreeNode(string& data, pchild) : pChild(pchild) {
sData = data;
}
~CTreeNode() {
if (NULL!=pChild) {
delete pChild;//delete (CTree *)pChild;
pChild = NULL;
}
void getChild(CTreeNode& child) {
child = *pChild;//child = *((CTree *)pChild);
}
bool addChild(string& s) {
if (NULL==pChild) {
return false;
} else {
pChild = new CTree;
}
return true;
}
void * getChildPtr() { return pChild; }
void getData(string& data, CTree * &p) { //not sure about how to put the & in there on CTree
data=sData;
p = pChild;
}
void setData(string& data, CTree * p) {
sData=data;
pChild = p;
}
};
問題是這裏相互依賴,我想我已經用類聲明解決了它。 做class CTreeNode;課前CTree {}。 http://www.codeguru.com/forum/showthread.php?t=383253
我可能會弄壞這段代碼,但它不完整,因爲我沒有多年需要編寫一棵樹,但我想我已經介紹了基本知識。我沒有實現operator []。
這是不確定的行爲有一個不完整的類型作爲模板參數爲標準庫容器。 – GManNickG 2011-04-17 05:48:37
有關更多信息,請參見Matt Austern的[「標準館員:不完整類型的容器」](http://drdobbs.com/184403814) – 2011-04-17 05:56:34