遍历模板化列表

Iterating over templated list

我遇到关于在模板上声明迭代器的问题 列表。下面是代码。

#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(资源,根); ^ 4_4.cpp:52:20: 错误:成员引用基类型 'Node *' 不是结构或联合 如果(迭代器->正确) ~~~~^ ~~~~~ 产生了 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);

}

你的代码中有很多错误。

为避免提到的错误,在迭代器

之前添加 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++)

在 main

中传递给 levelLinkedLists 的参数数量错误
levelLinkedLists(res, root);

应该是3.

使用了未定义的函数。

createMinimalBst(root, arr, 0, 8);

您已经定义了函数createMinBST,所以可能又是一个错字。