I was writing a function to dump the contents of a 'matrix' in a vector-of-vectors to cout when I ran across conceptual difficulties. Here's the code of the working function:
#include <iostream>
#include <vector>
using namespace std;
void output_string_matrix(const vector<vector<string> >& matrix)
{
vector<vector<string> >::const_iterator rows_itr;
vector<string>::const_iterator cols_itr;
for (rows_itr = matrix.begin(); rows_itr != matrix.end(); ++rows_itr)
{
vector<string>::const_iterator cols_endpoint = rows_itr->end();
for (cols_itr = rows_itr->begin(); cols_itr != cols_endpoint; ++cols_itr)
{
cout << *cols_itr << " ";
}
cout << endl;
}
}
My question for local gurus: Why do the inner/"column" iterators need to be const_iterators? I can understand why the "rows" iterator needs to be const_iterator, but not why that's the case for the inner one.