我面临着在模板上声明迭代器的问题 名单。以下是代码。
#include<iostream>
#include<vector>
#include<list>
using namespace std;
template<typename T>
struct Node
{
T data;
Node<T> *left,*right;
};
template<typename T>
Node<T>* newNode(T data)
{
Node<T>* root = new Node<T>();
root->data = data;
root->left = NULL;
root->right = NULL;
return root;
}
template<typename T>
void createMinBST(Node<T>*& root,T arr[],int start,int end)
{
if(start>end)
return;
int mid = (start+end)/2;
if (root == NULL)
{
root = newNode(arr[mid]);
createMinBST(root->left, arr,start , mid-1);
createMinBST(root->right, arr,mid+1 , end);
}
}
template<typename T>
void levelLinkedLists(vector<list<Node<T>*> >& res,Node<T>* root)
{
list<Node<T>*>li;
li.push_back(root);
res.push_back(li);
int depth = 0;
while(!res[depth].empty())
{
list<Node<T>*>l;
typename list<Node<T>*>::iterator iter;
for(iter = res[depth].begin();iter != res[depth].end(); iter++)
{
if(iter->left)
l.push_back(*iter->left);
if(iter->right)
l.push_back(*iter->right);
}
depth++;
res.push_back(l);
}
}
template<typename T>
void printLevelLinkedLists(vector<list<Node<T>*> > res)
{
typename vector<list<Node<T>*> >::iterator iter;
for (iter = res.begin(); iter!= res.end();iter++)
{
list<Node<T>*>li = *iter;
typename list<Node<T>*>::iterator it;
for (it = li.begin(); it != li.end(); it++ )
{
cout << (*it)->data <<" ";
}
cout<<endl;
}
}
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
Node<int>* root;
root = NULL;
createMinBST(root, arr, 0, 8);
vector<list<Node<int>*> > res;
levelLinkedLists(res, root);
printLevelLinkedLists(res);
}
错误:
4_4.cpp:50:20: error: member reference base type 'Node<int> *' is not a structure or union
if(iter->left)
~~~~^ ~~~~
4_4.cpp:83:8:注意:在实例化函数模板专门化'levelLinkedLists'这里请求 levelLinkedLists(res,root); ^ 4_4.cpp:52:20:错误:成员引用基类型'Node *'不是结构或联合 如果(iter-&GT;右) ~~~~ ^ ~~~~~ 产生了2个错误。
最后得到了正确的工作代码..感谢大家的快速输入
#include<iostream>
#include<vector>
#include<list>
using namespace std;
template<typename T>
struct Node
{
T data;
Node<T> *left,*right;
};
template<typename T>
Node<T>* newNode(T data)
{
Node<T>* root = new Node<T>();
root->data = data;
root->left = NULL;
root->right = NULL;
return root;
}
template<typename T>
void createMinBST(Node<T>*& root,T arr[],int start,int end)
{
if(start>end)
return;
int mid = (start+end)/2;
if (root == NULL)
{
root = newNode(arr[mid]);
createMinBST(root->left, arr,start , mid-1);
createMinBST(root->right, arr,mid+1 , end);
}
}
template<typename T>
void levelLinkedLists(vector<list<Node<T>*> >& res,Node<T>* root)
list<Node<T>*>li;
li.push_back(root);
res.push_back(li);
int depth = 0;
while(!res[depth].empty())
{
list<Node<T>*>l;
typename list<Node<T>*>::iterator iter;
for(iter = res[depth].begin();iter != res[depth].end(); iter++)
{
if((*iter)->left)
l.push_back((*iter)->left);
if((*iter)->right)
l.push_back((*iter)->right);
}
depth++;
res.push_back(l);
}
}
template<typename T>
void printLevelLinkedLists(vector<list<Node<T>*> > res)
{
typename vector<list<Node<T>*> >::iterator iter;
for (iter = res.begin(); iter!= res.end();iter++)
{
list<Node<T>*>li = *iter;
typename list<Node<T>*>::iterator it;
for (it = li.begin(); it != li.end(); it++ )
{
cout << (*it)->data <<" ";
}
cout<<endl;
}
}
int main()
{ int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
Node<int>* root;
root = NULL;
createMinBST(root, arr, 0, 8);
vector<list<Node<int>*> > res;
levelLinkedLists(res, root);
printLevelLinkedLists(res);
}
答案 0 :(得分:3)
代码中有很多错误。
为避免提到错误,请在迭代器
之前添加typename
typename list<Node<T>*>::iterator it;
然后你在其中一个for
循环中有拼写错误
for (it = li.begin(); it != li.end();it+++)
//should be:
for (auto it = li.begin(); it != li.end();it++)
在主
中传递给levelLinkedLists
的参数数量错误
levelLinkedLists(res, root);
应为3。
使用未定义的功能。
createMinimalBst(root, arr, 0, 8);
您已定义了函数createMinBST
,因此可能再次输入错字。