简体   繁体   中英

Initializer list vs. vector

In C++11, one can use initializer lists to initialize parameters in functions. What is the purpose of it? Can't the same be done with const vectors? What is the difference of the two programs below?

Using an initializer list:

#include <iostream>

using namespace std;

int sumL(initializer_list<int> l){
    int sum = 0;
    for (const auto i: l){
        sum += i;
    }
    return sum;
}

int main(){

    cout << sumL({1, 2, 3}) << "\n";

    return 0;
}

Using a const vector:

#include <iostream>
#include <vector>

using namespace std;

int sumV(const vector<int> l){
    int sum = 0;
    for (const auto i: l){
        sum += i;
    }
    return sum;
}

int main(){

    cout << sumV({1, 2, 3}) << "\n";
    return 0;
}

The common use of std::initializer_list is as argument to constructors of container (and similar) classes, allowing convenient initialisation of those containers from a few objects of the same type. Of course, you can use std::initializer_list otherwise and then use the same {} syntax.

Since a std::initializer_list has a fixed size, it doesn't require dynamic allocation and hence can be efficiently implemented. A std::vector , on the other hand, requires dynamic memory allocation. Even in your simple example it is unlikely that the compiler will optimize this overhead away (avoid the intermediary std::vector and its dynamic memory allocation). Other than that, there is no difference in the outcome of your programs (though you should take a const std::vector<int>& argument to avoid a copy and its associated dynamic memory allocation).

The semantics of the two are quite different. The initializer_list has pointer semantics while the vector has value semantics.

In your first example, the compiler will generate code similar to the following:

int const __temp_array[3] = {1, 2, 3};
cout << sumL(std::initializer_list<int>(__temp_array, __temp_array + 3)) << "\n";

This is explained in [dcl.init.list]/5 . As you can see, within sumL you have access to const pointers to the elements of the braced-init-list , this means you have no other option but to copy the elements out of the list.

In case of sumV you could've std::moved the elements from the vector if needed (assuming the parameter type is not const ).

Similarly, copying the initializer_list performs shallow copies, ie only the pointers will be copied, while copying the vector , of course, means the elements will be copied.

In your example, none of the points above really make any difference, other than constructing the vector will require dynamic memory allocation, while constructing the initializer_list will not.

initializer_list使用最佳存储位置并防止不必要的调用,它被设计为轻量级,而对于vector有一个堆分配,可能会有更多的副本/移动。

initalizer_list is not a generalized container like std::vector. It's primary purpose is object initialization. If the low overheard and no heap allocation is attractive to you I'd suggest looking at std::array. It a fixed sized stack allocated array that has all the conveniences of a STL container, which is essentially a thin wrapper on top of a c-array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM