-5
我正在使用C++實現FP增長算法,但在使用向量時出現一些錯誤。這裏是我的代碼:向量推送錯誤
typedef struct Node
{
FPData *fpData;
int childCount;
struct Node *parent;
vector<Node*> childs;
}FPNode;
FPNode* FPTree::NewNode(string data, int support)
{
FPNode *node = (FPNode*) malloc(sizeof(FPNode));
FPData *fpData = (FPData*) malloc(sizeof(FPData));
fpData -> data = data;
fpData -> support = support;
node -> fpData = fpData;
node -> childCount = 0;
node -> parent = NULL;
return node;
}
void FPTree::InsertFPPath(FPNode* root, list<FPData*> fpDatas)
{
if(fpDatas.size() <= 0)
{
return;
}
bool flag = true;
for(int i = 0; i < root -> childCount; i++)
{
FPNode* child = root -> childs[i];
if(child -> fpData -> data == fpDatas.front() -> data)
{
flag = false;
child -> fpData -> support ++;
fpDatas.pop_front();
InsertFPPath(child, fpDatas);
break;
}
}
if(flag)
{
FPData* firstData = fpDatas.front();
FPNode* newChild = NewNode(firstData -> data, firstData -> support);
root -> childs.push_back(newChild); // error at here
root -> childCount ++;
fpDatas.pop_front();
for(int i = 0; i < fpDatas.size(); i++)
{
InsertFPPath(newChild, fpDatas);
}
}
}
的InsertFPPath()
是一個遞歸方法,我得到的錯誤,當第二次迭代:
root -> childs.push_back(newChild)
的錯誤出現在:
construct(_Up* __p, _Args&&... __args) // here the __p is null, and __args is right
{
::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
}
和線程隊列停止at:
#0 0x00000001000050cf in void std::__1::allocator::construct(Node**, Node* const&&&) [inlined] at /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:1673
回溯是:
#0 0x00000001000050cf in void std::__1::allocator::construct(Node**, Node* const&&&) [inlined] at /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:1673 #1 0x00000001000050bc in void std::__1::allocator_traits >::__construct(std::__1::integral_constant, std::__1::allocator&, Node**, Node* const&&&) [inlined] at /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:1600 #2 0x000000010000509c in void std::__1::allocator_traits >::construct(std::__1::allocator&, Node**, Node* const&&&) [inlined] at /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:1453 #3 0x0000000100005079 in std::__1::vector >::push_back(Node* const&) [inlined] at /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:1590 #4 0x0000000100004fa9 in FPTree::InsertFPPath(Node*, std::__1::list >) at /Users/dorothy/ME/USTC/作業/數據挖掘/experiment/FPTree/FPTree/FPTree.cpp:129 #5 0x00000001000051b4 in FPTree::InsertFPPath(Node*, std::__1::list >) at /Users/dorothy/ME/USTC/作業/數據挖掘/experiment/FPTree/FPTree/FPTree.cpp:135**
什麼錯誤?請具體說明。 – ajshort
爲什麼在獲取孩子的size()時,你會跟蹤childCount?另外,當迭代一個'std :: vector'時,你應該使用迭代器,而不是索引訪問。 – tillaert
@ user2435797您的C++看起來很像C.選擇一個並堅持下去。 – Biffen