Stripped down to a minimal reproducible example, the code I'm having the issue with is as follows:
#include <iostream>
#include <vector>
using namespace std;
void fill_vector_of_vectors(vector<vector<char>>& data) // I gave up on trying to think up a proper name for this
{
data.clear();
data.resize(4);
for (vector<char> subarray : data)
{
subarray.resize(4, 'x');
}
}
int main()
{
vector<vector<char>> data;
fill_vector_of_vectors(data);
for (vector<char> subarray : data)
{
for (char must_be_x: subarray)
{
cout << must_be_x;
}
cout << "\n";
}
return 0;
}
Expected output is
xxxx
xxxx
xxxx
xxxx
But the actual output of the code is four blank lines. With the debugger, I found out that the filler function does fill out the child vectors properly, but the child vectors in main() are empty. Where am I losing the data?