矢量对循环中引用的明确影响
vector clear effect on reference in a loop
#include <iostream>
#include <vector>
using namespace std;
void printMatrix(vector<vector<int> >& m) {
for (int i = 0; i < m.size(); ++i) {
cout << "{" << m[i][0];
for (int j = 1; j < m[i].size(); ++j) {
cout << "," << m[i][j];
}
cout << "}\n";
}
cout << "************************\n";
}
vector<vector<int> > getClimbs(int n) {
vector<vector<int> > first = {{1}};
if (n==1) return first;
vector<vector<int> > second = {{1,1}, {2}};
if (n==2) return second;
vector<vector<int> >& one = first;
vector<vector<int> >& two = second;
vector<vector<int> > res;
for (int i = 3; i <= n; ++i) {
res.clear();
for (int j = 0; j < one.size(); ++j) {
one[j].push_back(2);
res.push_back(one[j]);
}
for (int j = 0; j < two.size(); ++j) {
//cout << "two[0] " << two[j][0] << "\n";
two[j].push_back(1);
//cout << "two[1] " << two[j][1] << "\n";
//cout << "size : " << two[j].size() << "\n";
res.push_back(two[j]);
//cout << "size : " << res.back().size() << "\n";
//printMatrix(res);
two[j].pop_back();
}
one = two;
two = res;
}
return res;
}
int main() {
vector<vector<int> > res = getClimbs(2);
printMatrix(res);
res = getClimbs(3);
printMatrix(res);
res = getClimbs(4);
printMatrix(res);
res = getClimbs(5);
printMatrix(res);
}
以上代码运行良好。但是,如果您观察到,我在 getClimbs() 中使用了两个引用,一个和两个。我的疑问是,我在迭代结束时执行两个 = res,然后在下一次迭代开始时执行 res.clear() 。现在,由于 two 是对 res 的引用,它应该指向一个空容器。但是,那不会发生。即使在 res 被清除后,我也可以访问 res 中的所有元素。
有谁知道为什么?
请注意,如果没有循环,则不会发生这种情况,我只是做了 two=res 后跟 res.clear() ,然后 two 会指向一个空容器。
two
是 而不是 对 res
的引用。这是对 second
:
的引用
vector<vector<int>>& two = second;
永远不会重新分配引用,所以行:
two = res;
将调用赋值运算符并将res
的内容复制到two
(second
的别名)。
您不能像那样重新绑定引用变量。相反,实际发生的是您调用复制构造函数将 res
向量复制到 two
所引用的任何内容(在本例中为 second
)。
所以您实际上用 res
向量的副本替换了 second
向量。
#include <iostream>
#include <vector>
using namespace std;
void printMatrix(vector<vector<int> >& m) {
for (int i = 0; i < m.size(); ++i) {
cout << "{" << m[i][0];
for (int j = 1; j < m[i].size(); ++j) {
cout << "," << m[i][j];
}
cout << "}\n";
}
cout << "************************\n";
}
vector<vector<int> > getClimbs(int n) {
vector<vector<int> > first = {{1}};
if (n==1) return first;
vector<vector<int> > second = {{1,1}, {2}};
if (n==2) return second;
vector<vector<int> >& one = first;
vector<vector<int> >& two = second;
vector<vector<int> > res;
for (int i = 3; i <= n; ++i) {
res.clear();
for (int j = 0; j < one.size(); ++j) {
one[j].push_back(2);
res.push_back(one[j]);
}
for (int j = 0; j < two.size(); ++j) {
//cout << "two[0] " << two[j][0] << "\n";
two[j].push_back(1);
//cout << "two[1] " << two[j][1] << "\n";
//cout << "size : " << two[j].size() << "\n";
res.push_back(two[j]);
//cout << "size : " << res.back().size() << "\n";
//printMatrix(res);
two[j].pop_back();
}
one = two;
two = res;
}
return res;
}
int main() {
vector<vector<int> > res = getClimbs(2);
printMatrix(res);
res = getClimbs(3);
printMatrix(res);
res = getClimbs(4);
printMatrix(res);
res = getClimbs(5);
printMatrix(res);
}
以上代码运行良好。但是,如果您观察到,我在 getClimbs() 中使用了两个引用,一个和两个。我的疑问是,我在迭代结束时执行两个 = res,然后在下一次迭代开始时执行 res.clear() 。现在,由于 two 是对 res 的引用,它应该指向一个空容器。但是,那不会发生。即使在 res 被清除后,我也可以访问 res 中的所有元素。 有谁知道为什么?
请注意,如果没有循环,则不会发生这种情况,我只是做了 two=res 后跟 res.clear() ,然后 two 会指向一个空容器。
two
是 而不是 对 res
的引用。这是对 second
:
vector<vector<int>>& two = second;
永远不会重新分配引用,所以行:
two = res;
将调用赋值运算符并将res
的内容复制到two
(second
的别名)。
您不能像那样重新绑定引用变量。相反,实际发生的是您调用复制构造函数将 res
向量复制到 two
所引用的任何内容(在本例中为 second
)。
所以您实际上用 res
向量的副本替换了 second
向量。