I encounter problems in inserting elements in a 2d vector. For example I want to insert 99 in v[2][5], how would I do it? What I have is this code (example given by Narue in http://www.daniweb.com/techtalkforums/thread70093.html)
#include <iostream>
#include <iomanip>
#include <vector>
int main()
{
std::vector< std::vector<int> > v;
for ( int i = 0; i < 10; i++ ) {
v.push_back ( std::vector<int>() );
for ( int j = 0; j < 10; j++ )
v[i].push_back ( i + j );
}
std::vector< std::vector<int> >::iterator row_it = v.begin();
std::vector< std::vector<int> >::iterator row_end = v.end();
for ( ; row_it != row_end; ++row_it ) {
std::vector<int>::iterator col_it = row_it->begin();
std::vector<int>::iterator col_end = row_it->end();
for ( ; col_it != col_end; ++col_it )
std::cout<< std::setw ( 3 ) << *col_it;
std::cout<<'\n';
}
}
I tried inserting the line v.at(2).insert(col_it-5, 99);
at line 27, but what I get is a segmentation fault message (I'm doing this in Linux). Can somebody tell me what I'm doing wrong. Any other suggestions on inserting elements on a 2d vector will be greatly appreaciated.