如何编写这个递归定义? [英] How to write this recursive definition?

查看:77
本文介绍了如何编写这个递归定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我写了以下定义:


typedef vector<对< char,TrieNode * TrieNode;


但它不能被编译,因为TrieNode实际上我的目标是:

struct Pair;

typedef vector< PairTrieNode;

struct配对{

char key;

TrieNode * ptr;

Pair(char k,TrieNode * p){key = k; ptr = p;}

};

我希望使用模板pair来自STL。


怎么写这个定义?


非常感谢!

hello, I wrote the following definition:

typedef vector< pair<char, TrieNode* TrieNode;

but it can''t be compiled, because "TrieNode" is not declared
in fact my aim is:
struct Pair;
typedef vector<PairTrieNode;
struct Pair{
char key;
TrieNode *ptr;
Pair(char k, TrieNode *p){ key = k; ptr = p;}
};
and I want to use template "pair" from STL.

How to write this defintion?

Thanks very much!

推荐答案

Moonlit写道:
Moonlit wrote:

< lk ***** @ gmail.comwrote:
<lk*****@gmail.comwrote:

你好,我写了以下定义:


typedef vector<对< char,TrieNode * TrieNode;


但它不能被编译,因为TrieNode实际上我的目标是:

struct Pair;

typedef vector< PairTrieNode;

struct配对{

char key;

TrieNode * ptr;

Pair(char k,TrieNode * p){key = k; ptr = p;}

};

我希望使用模板pair来自STL。


如何写这个定义?
hello, I wrote the following definition:

typedef vector< pair<char, TrieNode* TrieNode;

but it can''t be compiled, because "TrieNode" is not declared
in fact my aim is:
struct Pair;
typedef vector<PairTrieNode;
struct Pair{
char key;
TrieNode *ptr;
Pair(char k, TrieNode *p){ key = k; ptr = p;}
};
and I want to use template "pair" from STL.

How to write this defintion?


Sinc它是一个指针添加(即向前声明)


类TrieNode;


在使用TrieNode之前
Sinc it is a pointer add (i.e forward declare)

class TrieNode;

Before using TrieNode



Uhhm,您是否在发布之前尝试过这个?因为我无法想象

会编译。 TrieNode不是类,它是模板的typedef

实例化。


祝你好运,


汤姆

Uhhm, did you try this before posting? Because I can''t imagine that
would compile. TrieNode isn''t a class, it''s a typedef of a template
instantiation.

Best regards,

Tom



非常感谢!

Thanks very much!



lk *** **@gmail.com 写道:

你好,我写了以下定义:


typedef vector< ;对< char,TrieNode * TrieNode;


但它不能被编译,因为TrieNode实际上我的目标是:

struct Pair;

typedef vector< PairTrieNode;

struct配对{

char key;

TrieNode * ptr;

Pair(char k,TrieNode * p){key = k; ptr = p;}

};

我希望使用模板pair来自STL。


如何写这个定义?
hello, I wrote the following definition:

typedef vector< pair<char, TrieNode* TrieNode;

but it can''t be compiled, because "TrieNode" is not declared
in fact my aim is:
struct Pair;
typedef vector<PairTrieNode;
struct Pair{
char key;
TrieNode *ptr;
Pair(char k, TrieNode *p){ key = k; ptr = p;}
};
and I want to use template "pair" from STL.

How to write this defintion?



你最有可能做的就是:


struct Pair;

typedef std :: vector< PairTrieNode;

struct Pair {

typedef std :: pair< ; char,TrieNode * PAIR;

PAIR p_;

对(const PAIR& p)

{

p_ = p;

}

运算符PAIR()

{

返回p_;

}

};


用作std :: pair:


void f( )

{

TrieNode t;

t.push_back(std :: make_pair(''c'',& t));

配对:: PAIR p = t [0];

}


使用std :: pair直接创建循环依赖不能使用前向声明打破
- 第二个模板参数''

类型必须事先知道。

HTH
Radu

most probably all you can do is:

struct Pair;
typedef std::vector<PairTrieNode;
struct Pair{
typedef std::pair<char, TrieNode *PAIR;
PAIR p_;
Pair(const PAIR& p)
{
p_ = p;
}
operator PAIR()
{
return p_;
}
};

to be used like std::pair:

void f()
{
TrieNode t;
t.push_back(std::make_pair(''c'', &t));
Pair::PAIR p = t[0];
}

using std::pair directly creates a cyclic dependency that cannot be
broken using forward declarations - the second template parameters''
type has to be known in advance.
HTH
Radu


非常感谢!
Thanks very much!


这个怎么样:


struct TrieNode:public vector< pair< char,struct TrieNode *

{

};

How about this:

struct TrieNode : public vector< pair<char, struct TrieNode*
{
};


这篇关于如何编写这个递归定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆