Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What type does auto use for containers?

I can achieve identical output by using different containers in C++. For example . .

    std::array<int, 5> v = {1,2,3,4,5};
    for(auto i : v)
        std::cout << i << ", ";

or

    std::vector<int> v = {1,2,3,4,5};

or

    int v[] = {1,2,3,4,5};

etc . .

So what container does auto use here?

    auto v = {1,2,3,4,5};
    for(auto i : v)
        std::cout << i << ", ";
like image 880
learnvst Avatar asked Jun 28 '13 09:06

learnvst


People also ask

What are the three categories of containers?

The three types of containers found in the STL are sequential, associative and unordered.

What is auto data type in C++?

The auto keyword in C++ automatically detects and assigns a data type to the variable with which it is used. The compiler analyses the variable's data type by looking at its initialization. It is necessary to initialize the variable when declaring it using the auto keyword.

What is the difference between auto and Decltype in C++?

'auto' lets you declare a variable with a particular type whereas decltype lets you extract the type from the variable so decltype is sort of an operator that evaluates the type of passed expression.

When was C++ Auto added?

Auto was a keyword that C++ "inherited" from C that had been there nearly forever, but virtually never used. All this changed with the introduction of auto to do type deduction from the context in C++11.


1 Answers

std::initializer_list<int>


Not that hard to check for yourself, you can always decltype(v), and then compare it with said list type.

That has another nice property, that sometimes is very useful and might interest you:

for (auto i : {1,2,3,4,5})
    std::cout << i << ", ";

It can be done because initializer_list keeps the standard range interface.

like image 170
Bartek Banachewicz Avatar answered Sep 22 '22 21:09

Bartek Banachewicz