Konrad
Konrad

Reputation: 40947

Constructing associative containers

I was convinced (until I just tried it a moment ago) that it was possible to instantiate an associative container with array style notation.

For example,

std::set< int > _set = { 2, 3, 5 };

This isn't the case but I am wondering if there is any other way of bulk initialising a container in the constructor like this?

Upvotes: 0

Views: 93

Answers (2)

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272507

You could do:

const int x[] = { 2, 3, 5 };
std::set<int> _set(&x[0], &x[sizeof(x)/sizeof(x[0])]);

!

Upvotes: 0

Bj&#246;rn Pollex
Bj&#246;rn Pollex

Reputation: 76788

You can use Boost.Assign.

std::set< int > _set = boost::assign::list_of(2)(3)(5);

Upvotes: 2

Related Questions