Search code examples
c++arraysinitializer-listauto

Access auto declared array


auto messwerte2 = { 3.5, 7.3, 4.9, 8.3, 4.4, 5.3, 3.8, 7.5 };

Which possibilities exist to access a single value explicitly of this array-like looking structure which as I was informed is actually a std::initializer_list ?


Solution

  • Which possibilities exist to access a single value explicitly of this array?

    It's not an array, but is deduced to a std::initializer_list<double> which can be accessed through an iterator or a range based loop:

    #include <iostream>
    
    auto messwerte2 = { 3.5, 7.3, 4.9, 8.3, 4.4, 5.3, 3.8, 7.5 };
    
    int main() {
    
        for(auto x : messwerte2) {
            std::cout << x << std::endl;
        }
    }
    

    Live Demo

    To make it an array use an explicit array declaration:

    double messwerte2[] = { 3.5, 7.3, 4.9, 8.3, 4.4, 5.3, 3.8, 7.5 };