Thursday 15 August 2013

c++ - two dimensional array using vector in cpp -


I want to create a 2D array using vector. But, when I do this, I have a mistake, can someone please explain to me what I am doing, and the possible solution to this problem.

I made everything public because now I do not want to deal with gates and sets. I want to clarify the concept of 2D array.

  #include & lt; Iostream & gt; # Include & lt; Vector & gt; using namespace std; Square point {public: point (): x (0), y (0) {} dot () {} point (float xx, float): x (xx), y (yy) {} float x, y ; }; Int main () {Vector & lt; Vector & lt; Point & gt; & Gt; a; // 2D sur point point (2,3); A [0] [0] = p; // error here 0; }    

Your vector is empty I [0] [0] can not be used.

Here's how you declare it:

  a.push_back (vector & lt; point & gt; ()); A [0] .push_back (P);  

If you know how many items will be from the beginning, then you can:

  vector & lt; Vector & lt; Point & gt; & Gt; One (10, vector, lie; point & gt; (10));  

This is a vector containing 10 points with 10 points. After that you can use

  a [4] [4] = p;  

However, I believe that using vector vectors is misleading. If you want an array, consider using uBLAS

  #include & lt; Boost / numeric / ublas / matrix.hpp & gt; #include & lt; Boost / numeric / ublas / io.hpp & gt; Int main () {namespace boost :: numeric :: ublas; Matrix & lt; Double & gt; M (3, 3); For (unsigned i = 0; i & lt; m.size1 (); ++ i) for (unsigned j = 0; j & lt; m.size2 (); ++ j) m (i, j) = 3 * i + j; Std :: cout & lt; & Lt; M & LT; & Lt; Std :: endl; }  

1 comment: