用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

 
 

 

转载于:https://www.cnblogs.com/wqpkita/p/6403280.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值