#include <iostream>
#include <string>
#include <vector>
using namespace std;
using std::vector;
int main()
{
cout << "方法一:" << endl;
vector<int> ivec1(10, 42);
for (auto &c : ivec1)
cout << c << " ";
cout << endl;
cout << "方法二:" << endl;
vector<int> ivec2{42,42,42,42,42,42,42,42,42,42};
for (auto &c : ivec2)
cout << c << " ";
cout << endl;
cout << "方法三:" << endl;
vector<int> ivec3;
for (int i = 0; i != 10; ++i)
ivec3.push_back(42);
for (auto &c : ivec3)
cout << c << " ";
cout << endl;
system("pause");
return 0;
}c++primer(第五版)3.19
最新推荐文章于 2023-12-25 23:25:03 发布
本文介绍了在C++中初始化vector的三种不同方法,并通过实例演示了如何使用这些方法创建包含相同元素的vector。第一种方法是使用构造函数指定初始大小及默认值;第二种方法是通过花括号列表初始化;第三种方法则是循环调用push_back方法。
196

被折叠的 条评论
为什么被折叠?



