What does initializer_list do?

丶灬走出姿态 提交于 2020-01-04 07:03:04

问题


I'm currently tasked with creating a definition for a special custom class my professor provided to us; I've never seen initializer_list in use, nor did my professor go over it. What exactly does it do?

template<class T>
LinkedQueue<T>::LinkedQueue(const std::initializer_list<T>& il) {

}

I have to define the constructor, which I will do on my own. I just want to know what this parameter does. Also, my compiler keeps telling me that std::initializer_list member declaration is not found. I've imported it using #include but the issue is still here.


回答1:


std::initializer_list is a class template that holds a sequence of initializers, which has a very minimal interface since its only purpose is to be used to initialize another object with the sequence of values it contains. Unlike std::vector or std::deque, std::initializer_list is not intended to be used as a general-purpose container.

The function LinkedQueue<T>::LinkedQueue(const std::initializer_list<T>&) is called when someone attempts to list-initialize a LinkedQueue<T>, like this:

LinkedQueue<int> q {1, 2, 3};

The language automatically constructs a std::initializer_list<int> object which contains the values 1, 2, 3, in that order. This object is passed to the constructor. After the constructor returns, the user expects that the newly constructed LinkedQueue<int> object will contain the values 1, 2, 3, in that order.

You need to write the constructor such that this will be true. To do so, you can iterate over the std::initializer_list<T> object using a range-based for loop and add the values in order to the list. Something like this might work, assuming you have a push_back function and a working default constructor:

template<class T>
LinkedQueue<T>::LinkedQueue(const std::initializer_list<T>& il): LinkedQueue() {
    for (const T& val : il) {
        push_back(val);
    }
}

This delegates to the default constructor to set up the class invariants, then inserts the given initializers in order.




回答2:


It allows you to initalize your LinkedQueue with {}. as in

 LinkedQueue<char> theQueue = {'a','b','c','d','e','f','g','h' };

as well as other things, detailed here on a page about it and it's uses.




回答3:


In C++11 an initializer-list is a way to assign content to an object, when several literals are required.

Example, you could initialize a list like:

std::list<int> myList{1,2,3,4,5};

You may write your class, so it accept such syntax.

As a minimalist example, the following function accept a initialization list:

void func( initializer_list<int> intList)
{
    for (auto i: intList) /*whatever*/;
}

// used as:
func({1,2,3,5});

Initialization are required to be of the same type: you cannot mix values from different types in the initialization-list.



来源:https://stackoverflow.com/questions/34957393/what-does-initializer-list-do

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!