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 << ", ";
The three types of containers found in the STL are sequential, associative and unordered.
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.
'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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With