简体   繁体   中英

Initializing std::vector of structs

I wish to add a bunch of objects of this type to a std::vector.

typedef struct 
{
    int handle;
} Handle;

Handle is defined within a C API header which I am unable to change.

I'm doing this at the moment but am wondering if it can be done as a one-liner.

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

I using a C++98 compiler.

Just write a make_handle function:

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

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