0

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?

6
  • 2
    That should be possible in C++0x. As the language is currently, you can't initialize any container with this syntax. Commented Oct 22, 2010 at 11:00
  • Is std::set really an associative container? Commented Oct 22, 2010 at 11:22
  • @FredOverflow: According to the sgi documentation it is a sorted associative container. Commented Oct 22, 2010 at 14:18
  • @FredOverflow: what's your doubt? Commented Oct 22, 2010 at 14:20
  • @Arun: I always thought that an associative container provides a mapping from keys to elements, such as std::map<K, V> (each key is associated with its value). Commented Oct 22, 2010 at 14:37

2 Answers 2

2

You can use Boost.Assign.

std::set< int > _set = boost::assign::list_of(2)(3)(5); 
Sign up to request clarification or add additional context in comments.

Comments

0

You could do:

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

!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.