zoukankan      html  css  js  c++  java
  • 用vector构造自动扩容的二维数组

    #include <iostream>
    #include <string>
    #include <vector>
    using namespace std;
    
    int main()
    {
        vector<vector<string> > edge; //注意<string>后有一个空格符号
        vector<string> stemp(2);      //将列的个数设置为2
      
    for(int i = 0;i<5;i++) //此处参数5表示将行数设置为5 { cin>>stemp[0]>>stemp[1]; edge.push_back(stemp); } for(int j=0;j<edge.size();j++) { cout<< j+1<<": "<<edge[j][0]<<" , "<<edge[j][1]<<endl; }return 0; }
    //eg
    #include <iostream>
    #include <vector>
    using namespace std;
    
    int main()
    {
        vector<vector<int> > a;
        vector<int> stmp(5);          //set the colCount=5
        for (int i = 0; i < 45; i++)  //set the rowCount=5
        {
            for (int j = 0; j < 5;j++)
            {
                stmp[j] = (i+1)*(j+1);
                cout << stmp[j] << " ";
            }
            cout << endl;
            a.push_back(stmp);
        }
        int rowCount = a.size();     //get the rowCout from a two-dimensional array
        int cloCount = a[0].size();  //get the colCout from a two-dimensional array
        cout << "row: " << rowCount << endl;
        cout << "col: " << cloCount << endl;
        return 0;
    }
    
    

    //注:如果二维数组是如 int a[15][8]定义的,则通过如下方法获取行列数

    cout << sizeof(a)/sizeof(a[0]) << endl;  //行数outpuut 15
    cout << sizeof(a[0]) / sizeof(a[0][0]) << endl; //列数outpuut 8

     
     
  • 相关阅读:
    Reverse Words in a String II -- LeetCode
    计算两点间的距离,hdu-2001
    A + B Problem,hdu-1000
    ASCII码排序,hdu-2000
    定义#define
    break语句的使用
    判断一个数是否为素数
    用下面公式求π:
    Sum Problem-hdu-1001
    正三角形的外接圆面积,nyoj-274
  • 原文地址:https://www.cnblogs.com/wqpkita/p/6403280.html
Copyright © 2011-2022 走看看