# 关于vector在堆上还是在栈上的思考与vector.push_back()究竟放入的是什么

vector如果不new是在栈上的，这是众人皆知的。

class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> result;
for (int i=0; i<numRows; ++i) {
vector<int> temp(i+1,1);
cout<<&temp<<endl;
result.push_back(temp);
}
for (int i=0; i<numRows; ++i) {
cout<<&result[i]<<endl;
}
return result;
}
};

0x7fff5fbff5f0
0x7fff5fbff5f0
0x7fff5fbff5f0
0x100105540
0x100105558
0x100105570
Program ended with exit code: 0

class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> result;
for (int i=0; i<numRows; ++i) {
vector<int> *temp =new vector<int>();
cout<<temp<<endl;
result.push_back(*temp);
}
for (int i=0; i<numRows; ++i) {
cout<<&result[i]<<endl;
}
return result;
}
};

0x1001054a0
0x1001054e0
0x1001054c0
0x100105560
0x100105578
0x100105590
Program ended with exit code: 0

——————————————

class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> result;
for (int i=0; i<numRows; ++i) {
vector<int> temp(i+1,1);
result.push_back(temp);
for (int j=1; j<i; ++j) {
temp[j]=result[i-1][j-1]+result[i-1][j];
}
}
return result;
}
};


class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> result;
for (int i=0; i<numRows; ++i) {
vector<int> temp(i+1,1);
result.push_back(temp);
for (int j=1; j<i; ++j) {

result[i][j]=result[i-1][j-1]+result[i-1][j];

}
}
return result;
}
};