C++ 初始化向量时,这两个数字是什么?

C++ 初始化向量时,这两个数字是什么?,c++,vector,C++,Vector,这是什么意思 vector<int> scores(10, 0); 这会增加内存吗 #include <iostream> #include <vector> using namespace std; int main() { cout << "Creating a 10 element vector to hold scores.\n"; //initialize all 10 elements to 0 vector<int>

这是什么意思

vector<int> scores(10, 0);
这会增加内存吗

#include <iostream>
#include <vector>
using namespace std;

int main()
{

cout << "Creating a 10 element vector to hold scores.\n";
//initialize all 10 elements to 0
vector<int> scores(10, 0); 
cout << "Vector size is :" << scores.size() << endl;
cout << "Vector capacity is:" << scores.capacity() << endl;

cout << "Adding a score.\n";
//memory is reallocated to accommodate growth
scores.push_back(0); 
cout << "Vector size is :" << scores.size() << endl;
cout << "Vector capacity is:" << scores.capacity() << endl;
#包括
#包括
使用名称空间std;
int main()
{
cout创建一个包含十个元素的向量,并将每个元素设置为零

至于调用,它可能会导致分配更多内存。(请参阅链接参考以了解何时发生。)

这是否意味着向量的大小是10,然后降到0

否,这意味着大小为10,每个元素都用值0初始化

这会增加内存吗

#include <iostream>
#include <vector>
using namespace std;

int main()
{

cout << "Creating a 10 element vector to hold scores.\n";
//initialize all 10 elements to 0
vector<int> scores(10, 0); 
cout << "Vector size is :" << scores.size() << endl;
cout << "Vector capacity is:" << scores.capacity() << endl;

cout << "Adding a score.\n";
//memory is reallocated to accommodate growth
scores.push_back(0); 
cout << "Vector size is :" << scores.size() << endl;
cout << "Vector capacity is:" << scores.capacity() << endl;

它增加了向量的大小。如果有足够的容量(即如果已经为新元素分配了足够的内存),它将只使用该内存。否则,将分配一个更大的内存块,并将元素移到其中,以便将新元素放在它们之后。

您是否尝试运行代码并查看结果?在询问之前是否检查了文档?读作:
int
0
的副本。