Alex Rivera | Logout

Is there a quick way to create a set?

Asked 2010-11-16T11:00:07.987
16

Currently I'm creating a new set like this:

    std::set<A> s;
    s.insert(a1);
    s.insert(a2);
    s.insert(a3);
    ...
    s.insert(a10);

Is there a way to create s in one line?

Edit
Report

1 Answer

17

In C++0x the standard defines the Initializer List as an improvement for this kind of (awkward) construct.

It's much easier now:

std::set<int> set = {10, 20, 30, 40, 50};

All it took was for the standard library to declare the following constructor for set:

template <typename Value, typename Compare, typename Allocator>
set<Value, Compare, Allocator>::set(std::initializer_list<Value> list);

and all our worries were neatly swiped away.

answered 2010-11-16T13:46:30.167

Your Answer