当我制作 2D vector 时。
std::vector<std::vector<Pokemon>> pokemons;
我可以立即运行这个吗:
Pokemon pikachu = ...
pokemons[23].push_back(Pikachu);
或者我需要:
pokemons.reserve(100);
for (int i =0;i<100;i++) pokemons.push_back(vector<Pokemon>());
谢谢
最佳答案
构建时可以设置外 vector 的初始大小
std::vector<std::vector<Pokemon>> pokemons(100);
它将构建 100 个内部空 vector 。
关于C++,2D std::vector,我是否需要显式保留并推回空 vector ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47223791/