recently I've started to study C++'s standard library containers from the Deitel&Deitel book. Conceptually, it is quite clear to me, but I have a problem when trying to replicate a piece of code that includes a 2-D array initialization.
The initialization in the book is (pretty much) this one:
#include <iostream>
#include <array>
using namespace std;
int main(){
array<array<int, 3>, 10> grades{{87, 96, 70},
{68, 87, 90},
{94, 100, 90},
{100, 81, 82},
{83, 65, 85},
{78, 87, 65},
{85, 75, 83},
{91, 94, 100},
{76, 72, 84},
{87, 93, 73}};
}
This shouldn't be problematic, but VisualStudioCode returns an error saying "too many initialization values". If instead, I simply initialize it in this way:
#include <iostream>
#include <array>
using namespace std;
int main(){
array<array<int, 3>, 10> grades{87, 96, 70,
68, 87, 90,
94, 100, 90,
100, 81, 82,
83, 65, 85,
78, 87, 65,
85, 75, 83,
91, 94, 100,
76, 72, 84,
87, 93, 73};
}
it works, which is what I do expect since the book in a previous example initialized a 2-D array by linearizing all its elements. What I do not understand is why in the other case VSC returns that error, since it should work just fine.