for(int jj = 0; jj< en; ++ jj) vec.push_back(jj); for(int jj = 0 ; jj< en; ++ jj) cout<< vec [jj]<< endl; 返回0; } No, you could do that (use reserve), or, use push_back() int main(int argc, const char* argv[]){int en = 10;vector<int vec; for (int jj = 0; jj < en; ++jj)vec.push_back(jj);for (int jj = 0; jj < en; ++jj)cout << vec[jj] << endl; return 0;} 不,但你需要以某种方式填充向量。你可以在 构建时,或者使用push_back,insert或resize来实现。 另一方面,reserve()方法没有增长向量。 No, but you need to fill the vector somehow. You can do that either uponconstruction, or by using push_back, insert, or resize. The reserve() method, on the other hand does not grow the vector. 它没有。您正在观察未定义行为的表现。 It does not. You are observing a manifestation of undefined behavior. 对于未定义的行为将如何表现,没有任何期望。 从实施的质量来看,如果你在打开断言的情况下编译程序,我会希望看到一个中止 。 There are no expectations as to how undefined behavior will show itself. From a quality of implementation point of view, would want to see an abortif you compile the program with assertions turned on. 尝试类似 cout<< vec.size()<<结束; 并思考你得到的含义。 Try something like cout << vec.size() << endl; and ponder the meaning of what you get. Best Kai-Uwe Bux Best Kai-Uwe Bux 这篇关于std :: vector:需要保留?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-23 14:31