简体   繁体   中英

how to use lists with class/struct variables in c++

I have a running C# program provided below. I'm trying to convert it into C++ , but having trouble getting desired output although there are no compilation errors in C++ program given below.

The C# program I am trying to convert is:

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},
        };

The corresponding C++ program I figured out is below:

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);

The Output I get in case of C++ program only shows last line which is line 4 ,how should I work with lists in case of C++ that my list gives me desired output?

you can use almost the same syntax as in c#.

  • uniform initialization (for evl_token ) here use the same order as members declare in class
  • c# 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}
};

You need to add every token you have created. Since C++11 you can use list-initialization syntax.

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 });
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM