c++ - 2D vector of unknown size -
i defined empty vector of vectors :
vector< vector<int> > v;
how fill empty vector vector of size 2 integers ( input ) each iteration of while loop?
while ( cin >> x >> y ) { //.... }
will 1 work? or what's best , elegant / effective way of doing it?
while ( cin >> x >> y ) { vector<int> row; row.push_back( x ); row.push_back( y ); v.push_back( row ); }
as pointed out jerrycoffin, better use :
struct point { int x; int y; };
and might overload output operator
std::ostream& operator<< (std::ostream& o,const point& xy){ o << xy.x << " " << xy.y; return o; }
and similar input operator (see e.g. here). , can use this:
int main() { point xy; std::vector<point> v; v.push_back(xy); std::cout << v[0] << std::endl; return 0; }
Comments
Post a Comment