-1

我想知道是否有一种方法可以在单行的向量中初始化和添加结构,例如

vector<Row> list;

//Row t;

list.push_back(Row t ={"",23});
4

1 回答 1

4

This should work:

list.push_back(Row{"",23});

as well as this:

list.push_back({"",23});

The above works for C++11 and a modern compiler, e.g., GCC or Clang. If you can't enable C++11 or it is not supported with your compiler, you need to add a constructor to Row:

struct Row
{
    Row( const std::string& d, int w ) : data( d ), weight( w ) {}

    // ...
};

and you can use:

list.push_back(Row("",23));
于 2013-10-24T18:20:12.313 回答