c++ - How to define a class which contains a variable dimension vector and how to use this class? -
i want define class, contains variable dimension vector, , defined constructor initialize it.
now problem how can use it? want use vector contain multiple of class object.
#include <iostream> #include <vector> using namespace std; class block { public: long nx,ny; vector<long> s; block(long &nx, long &ny):nx(nx),ny(ny),s((nx+1)*(ny+1),0) {} }; int main() { vector<block> b; b.push_back(b(2,2)); //i believe wrong, how can this? b.push_back(b(1,2)); for(int k=b.begin(); k<=b.end(); k++) { for(int i=b[k].s.begin();i<=b[k].s.end();i++) { cout<<b[k].s[i]; } } cout << "!!!hello world!!!" << endl; // prints !!!hello world!!! return 0; }
#include <iostream> #include <vector> using namespace std; class block { public: long nx,ny; vector<long> s; block(long nx, long ny):nx(nx),ny(ny),s((nx+1)*(ny+1),0) {} //^^^^^^^^^^^^^^^^ not necessary use inference }; int main() { vector<block> b; b.push_back(block(2,2)); //use block(2,2) for(int i=0;i<b[0].s.size();i++) { //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^the right way traversal vector cout<<b[0].s[i]; } cout << "!!!hello world!!!" << endl; // prints !!!hello world!!! return 0; }
Comments
Post a Comment