views:

31

answers:

3

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?

+2  A: 

You can use Boost.Assign.

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

If you can use boost, then take a look here how to do it : http://beta.boost.org/doc/libs/1_43_0/libs/assign/doc/index.html

VJo
A: 

You could do:

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

!

Oli Charlesworth