我有一个正在运行的C#
程序,如下所示。我正在尝试将其转换为C++
,但在获得所需的输出时遇到问题,尽管下面给出的C++
程序中没有编译错误。
我试图转换的C#
程序是:
class Evl_token
{
public enum Token_type { NAME, NUMBER, SINGLE };
public Token_type type;
public string str;
public int line_no;
}; // class evl_token
List<Evl_token> tokens = new List<Evl_token>()
{
//input is below
//line 1 module top;
//line 2 wire in,out;
//line 3 not(out,in);
//line 4 endmodule
new Evl_token {line_no = 1, str = "module", type = Evl_token.Token_type.NAME},
new Evl_token {line_no = 1, str = "top", type = Evl_token.Token_type.NAME},
new Evl_token {line_no = 1, str = ";", type = Evl_token.Token_type.SINGLE},
new Evl_token {line_no = 2, str = "wire", type = Evl_token.Token_type.NAME},
new Evl_token {line_no = 2, str = "in", type = Evl_token.Token_type.NAME},
new Evl_token {line_no = 2, str = ",", type = Evl_token.Token_type.SINGLE},
new Evl_token {line_no = 2, str = "out", type = Evl_token.Token_type.NAME},
new Evl_token {line_no = 2, str = ";", type = Evl_token.Token_type.SINGLE},
new Evl_token {line_no = 3, str = "not", type = Evl_token.Token_type.NAME},
new Evl_token {line_no = 3, str = "(", type = Evl_token.Token_type.SINGLE},
new Evl_token {line_no = 3, str = "out", type = Evl_token.Token_type.NAME},
new Evl_token {line_no = 3, str = ",", type = Evl_token.Token_type.SINGLE},
new Evl_token {line_no = 3, str = "in", type = Evl_token.Token_type.NAME},
new Evl_token {line_no = 3, str = ")", type = Evl_token.Token_type.SINGLE},
new Evl_token {line_no = 3, str = ";", type = Evl_token.Token_type.SINGLE},
new Evl_token {line_no = 4, str = "endmodule", type = Evl_token.Token_type.NAME},
};
我想出的相应的C++
程序如下:
struct evl_token
{
enum token_type { NAME, NUMBER, SINGLE };
token_type type;
std::string str;
int line_no;
}; //struct evl_token
typedef std::list<evl_token> evl_tokens;
evl_tokens tokens;
evl_token token;
//{
//input is below
//line 1 module top;
//line 2 wire in,out;
//line 3 not(out,in);
//line 4 endmodule
token.line_no = 1, token.str = "module", token.type = evl_token::NAME,
token.line_no = 1, token.str = "top", token.type = evl_token::NAME,
token.line_no = 1, token.str = ";", token.type = evl_token::SINGLE,
token.line_no = 2, token.str = "wire", token.type = evl_token::NAME,
token.line_no = 2, token.str = "in", token.type = evl_token::NAME,
token.line_no = 2, token.str = ",", token.type = evl_token::SINGLE,
token.line_no = 2, token.str = "out", token.type = evl_token::NAME,
token.line_no = 2, token.str = ";", token.type = evl_token::SINGLE;
token.line_no = 3, token.str = "not", token.type = evl_token::NAME,
token.line_no = 3, token.str = "(", token.type = evl_token::SINGLE,
token.line_no = 3, token.str = "out", token.type = evl_token::NAME,
token.line_no = 3, token.str = ",", token.type = evl_token::SINGLE,
token.line_no = 3, token.str = "in", token.type = evl_token::NAME,
token.line_no = 3, token.str = ")", token.type = evl_token::SINGLE,
token.line_no = 3, token.str = ";", token.type = evl_token::SINGLE,
token.line_no = 4, token.str = "endmodule", token.type = evl_token::NAME;
//}
tokens.push_back(token);
我在C++
程序中得到的输出只显示最后一行是4
行,如果C++
我的列表给出了我想要的输出,我应该怎样处理列表?
您可以使用与c#中几乎相同的语法。
evl_token
)这里使用与成员在类中声明的顺序相同的顺序list
<=> c ++ vector
(c#LinkedList
<=> c ++ list
)#include <vector>
#include <string>
struct evl_token
{
enum token_type { NAME, NUMBER, SINGLE };
int line_no;
std::string str;
token_type type;
};
std::vector<evl_token> tokens
{
{ 1, "module", evl_token::token_type::NAME},
{ 1, "top", evl_token::token_type::NAME},
{ 1, ";", evl_token::token_type::SINGLE},
{ 2, "wire", evl_token::token_type::NAME},
{ 2, "in", evl_token::token_type::NAME},
{ 2, ",", evl_token::token_type::SINGLE},
{ 2, "out", evl_token::token_type::NAME},
{ 2, ";", evl_token::token_type::SINGLE},
{ 3, "not", evl_token::token_type::NAME},
{ 3, "(", evl_token::token_type::SINGLE},
{ 3, "out", evl_token::token_type::NAME},
{ 3, ",", evl_token::token_type::SINGLE},
{ 3, "in", evl_token::token_type::NAME},
{ 3, ")", evl_token::token_type::SINGLE},
{ 3, ";", evl_token::token_type::SINGLE},
{ 4, "endmodule", evl_token::token_type::NAME}
};
您需要添加您创建的每个令牌。从C ++ 11开始,您可以使用列表初始化语法。
struct evl_token
{
enum token_type { NAME, NUMBER, SINGLE };
token_type type;
std::string str;
int line_no;
}; //struct evl_token
std::vector<evl_token> tokens;
tokens.push_back({ evl_token::NAME,"module",1 });
tokens.push_back({ evl_token::NAME,"top",1 });
tokens.push_back({ evl_token::SINGLE,";",2 });
...