用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
posted on 2017-02-15 20:37 HelloShijam 阅读(1660) 评论(0) 编辑 收藏 举报