我很想知道下面的代码是什么意思
我只是想知道它是如何工作的。
vector<int> lotteryNumVect(10); // I do not understand this part.
int lotteryNumArray[5] = {4, 13, 14, 24, 34}; // I understand this part.
lotteryNumVect.insert(lotteryNumVect.begin(), lotteryNumArray,
lotteryNumArray + 3); // I do not understand this part.
cout << lotteryNumVect.at(2) << endl; // I understand this part.
这个声明
vector <int> lotteryNumVect(10);
声明一个由 10 个元素初始化为零的 vector 。
那就是使用了构造函数
explicit vector(size_type n, const Allocator& = Allocator());
3 Effects: Constructs a vector with n default-inserted elements using
the specified allocator.
构造函数的第二个参数有一个默认参数,因此您可以调用构造函数,仅指定要在 vector 中创建的元素数。
这个语句
lotteryNumVect.insert(lotteryNumVect.begin(), lotteryNumArray,
lotteryNumArray + 3);
在 vector 的开头插入数组中的 3 个元素。
因此 vector 看起来像
4, 13, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0