(现代C++更新在最后)
如果您习惯使用变量引用计数的其他语言,您可能会期望
Grass g1;
在循环的每次迭代中创建一个新的“草”对象。事实并非如此,C++ 不是一种引用计数语言。
相反,它创建了一个scoped堆栈上的局部变量。
因为您是在循环中执行此操作,所以它可能每次都会位于内存中的同一位置。
您将需要:
-
不用指针,只需让容器成为 Grass 对象的容器:让容器为您处理分配。
-
使用 C++11unique_ptr
和 C++14make_unique
为循环的每次迭代动态创建 Grass 实例。当包含 unique_ptr 的向量超出范围时,它们将被自动释放。
-
Use the new
and delete
手动分配和释放指向的 Grass 对象的关键字。
选项1:
#include <vector>
struct Grass {
struct {
int x, y;
} position;
};
int main() {
std::vector<Grass> grassList;
for(int i=0;i<50;i++){
for(int j=0;j<50;j++){
Grass g1;
g1.position.x = i;
g1.position.y = j;
grassList.push_back(g1);
}
}
}
现场演示:http://ideone.com/DQs3VA
选项2:
#include <memory>
#include <vector>
struct Grass {
struct {
int x, y;
} position;
};
int main() {
std::vector<std::unique_ptr<Grass>> grassList;
for(int i=0;i<50;i++){
for(int j=0;j<50;j++){
auto g1 = std::make_unique<Grass>();
g1->position.x = i;
g1->position.y = j;
grassList.push_back(std::move(g1));
}
}
}
现场演示:http://ideone.com/hJUdwR
选项 3:
#include <vector>
struct Grass {
struct {
int x, y;
} position;
};
int main() {
std::vector<Grass*> grassList;
for(int i=0;i<50;i++){
for(int j=0;j<50;j++){
Grass* g1 = new Grass;
g1->position.x = i;
g1->position.y = j;
grassList.push_back(g1);
}
}
// ...
for (auto& g1: grassList) {
delete g1;
}
grassList.clear();
}
现场演示:http://ideone.com/GTk7ON
C++11 引入emplace_back
它允许您一次性分配和构造容器中的条目。
#include <vector>
struct Grass {
struct {
int x, y;
} position;
// because x and y are inside a private sub-struct,
// we'll need a constructor to pass the values in.
Grass(int x, int y) : position{x, y} {}
};
int main() {
std::vector<Grass> grassList{}; // default initialized.
static constexpr size_t dim = 10; // name the constant (DIMension)
grassList.reserve(dim); // allocate memory in advance
for (auto i = 0; i < dim; i++) {
for(auto j = 0; j < dim; j++) {
grassList.emplace_back(i, j);
}
}
// no cleanup required
}
现场演示:https://gcc.godbolt.org/z/G1YsW7hMs