lundi 28 novembre 2016

Add members of initializer_list to std::list in C++

I know that one can initialize a std::list from an initializer_list in a constructor, like so:

class Foo
{
    std::list<int> list;
    Foo(std::initializer_list<int> init)
    {
        list = init;
    }
};

However, is there also a way to later add all elements of an initializer_list to the list, without creating a new list object, and without using a loop or iterator? So in other words: Can I use a member function of std::list directly? I tried with insert(), but failed because of invalid parameter types.

So in my example above I am looking for something like this:

list.insert(init);  // or something similar

Any ideas?

Aucun commentaire:

Enregistrer un commentaire