Error on multi-D dynamic array output

How solve the error on multi-D dynamic array read to be output below?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <vector>
using namespace std;

vector<vector<char>> l;

l[0].push_back('f');
l[0].push_back('o');
l[0].push_back('o');

l[1].push_back('b');
l[1].push_back('a');
l[1].push_back('r');

l[2].push_back('b');
l[2].push_back('a');
l[2].push_back('z');

for( auto i: l) {

 cout << i[2] <<endl;
}

to get third element of each inner array by that declaration (must not changed to
vector<char> l[3] )
Last edited on
Why is it so hard for you to use code tags?
https://cplusplus.com/articles/jEywvCM9/

Looming in on 200 posts, and you still post eyesores.
Do you mean:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <vector>
#include <iostream>

int main() {

	vector<vector<char>> l(3);

	l[0].push_back('f');
	l[0].push_back('o');
	l[0].push_back('o');

	l[1].push_back('b');
	l[1].push_back('a');
	l[1].push_back('r');

	l[2].push_back('b');
	l[2].push_back('a');
	l[2].push_back('z');

	for (const auto& i : l)
		cout << i[2] << '\n';
}



o
r
z

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <vector>
using namespace std;

int main()
{
   vector<vector<char>> L;              // Why not vector<string> ?
   L.push_back( { 'f', 'o', 'o' } );
   L.push_back( { 'b', 'a', 'r' } );
   L.push_back( { 'b', 'a', 'z' } );

   for ( auto &thing : L)
   {
      for ( auto &subthing : thing ) cout << subthing;
      cout << '\n';
   }
}


foo
bar
baz
Topic archived. No new replies allowed.