【发布时间】:2016-02-02 13:32:10
【问题描述】:
我很疯狂,无法解释这里发生的事情。
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
int i = 0, j = 0, m = 3, n = 3;
vector<vector<int> > vvi(3, vector<int>(3, 1));
// why the following code outputs only single row,
// i.e.,"111" from vvi[0]? what about vvi[1], vvi[2]?
for(; i < m; ++i) {
for(; j< n; ++j) {
cout << vvi[i][j];
}
}
// any difference from the code below?
// for(int i=0; i < m; ++i) {
// for(int j= 0; j< n; ++j) {
// cout << vvi[i][j];
// }
// }
}
【问题讨论】:
-
注释掉的代码当然是正确的。它还说明了为什么在首次使用它们的地方初始化值是有帮助的。没有理由在
main的顶部定义i和j。在我评论代码时,将vvi定义中的两个3s 替换为m和n。