初始化结构的std ::向量

Initializing std::vector of structs

本文关键字:向量 std 结构 初始化      更新时间:2023-10-16

我希望将此类型的一堆对象添加到std :: vector。

typedef struct 
{
    int handle;
} Handle;

句柄是在我无法更改的C API标头中定义的。

我目前正在这样做,但想知道它是否可以作为单线。

Handle handle1 = {12};
Handle handle2 = {13};
std::vector<Handle> handles = boost::assign::list_of(handle1)(handle2);

i使用C 98编译器。

只需写一个make_handle函数:

Handle make_handle(int handle) {
    Handle ret = { handle };
    return ret;
}