用vector创建二维数组的方法;

vector创建二维数组的方法;

vector<vector>x(10,vector(10));//创建二维数组;前面是行,后面是列;

原理很简单:

  • vector(n, elem); //构造函数将n个elem拷贝给本身。

    #include
    #include
    using namespace std;
    int main()
    {

      vector<vector<int>>x(10,vector<int>(10));//创建二维数组;
      //cout << x.size();
       for (int i = 0; i < 10; i++)
       {
    
           for (int j = 0; j < 10; j++)
               x[i][j] = 1;
       }
       cout << x.size() << endl;
       cout << x[9].size() << endl;
       for (int i = 0; i < 10; i++)
       {
    
      	 for (int j = 0; j < 10; j++)
      		 cout << x[i][j]<<" ";
      	 cout << endl;
       }
    

    }

猜你喜欢

转载自blog.csdn.net/weixin_45929885/article/details/113831424