对向量的困惑
Confusion about vectors
我很想知道下面的代码是什么意思
我只想知道它是如何工作的。
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 个由零初始化的元素的向量。
那就是使用了构造函数
explicit vector(size_type n, const Allocator& = Allocator());
3 效果:使用 n 个默认插入的元素构造一个向量 指定的分配器。
构造函数的第二个参数具有默认参数,因此您可以调用构造函数,仅指定要在向量中创建的元素数。
本声明
lotteryNumVect.insert(lotteryNumVect.begin(), lotteryNumArray,
lotteryNumArray + 3);
插入数组中向量 3 元素的开头。
因此,矢量将看起来像
4, 13, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
解释
-
声明
vector <int> lotteryNumVect(10);
:这是使用构造函数的示例。 根据cplusplus的说法:
默认值 (1(:
explicit vector (const allocator_type& alloc = allocator_type());
填充 (2( :
explicit vector (size_type n, const value_type& val = value_type(), const allocator_type& alloc = allocator_type());
范围 (3( :
template <class InputIterator> vector (InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type());
副本 (4( :
vector (const vector& x);
因此,
vector <int> lotteryNumVect(10);
用十个零初始化向量(参见上面的 (1((。vector <int> lotteryNumVect(5, 2);
将用五个二初始化向量(参见上面的 (2((。 您可以在此处查看示例以更好地理解。 -
声明
lotteryNumVect.insert(lotteryNumVect.begin(), lotteryNumArray, lotteryNumArray + 3);
:这实际上是通过迭代器插入的。看看这个:
单元素 (1( :
iterator insert (iterator position, const value_type& val);
填充 (2( :
void insert (iterator position, size_type n, const value_type& val);
范围 (3( :
template <class InputIterator> void insert (iterator position, InputIterator first, InputIterator last);
术语
lotteryNumVect.begin()
实际上指向lotteryNumVect
的第一个元素(参见vector::begin(((。而lotteryNumArray
和lotteryNumArray+3
分别指向lotteryNumArray
数组的第一个和第三个元素。 因此,基本上lotteryNumVect.insert(lotteryNumVect.begin(), lotteryNumArray, lotteryNumArray + 3);
将lotteryNumArray
的前三个元素插入到向量lotteryNumVect
的开头。
进一步阅读 std::vector
- 普罗斯加斯
- CPP 首选项
- GeeksforGeeks
如何在 cplusplus 上导航:
- 页眉:
cplusplus.com/reference/<type header name here>
示例:cplusplus.com/reference/iostream/
- 函数/容器/关键字:
cplusplus.com/reference/<the header which contains it>/<function/container/keyword name>
示例:cplusplus.com/reference/iostream/cin/
- 成员函数/变量:
cplusplus.com/reference/<the header which contains it>/<function/container/keyword name>/<member variable/function name>/
示例:cplusplus.com/reference/string/string/size/
或者,你可以谷歌它。在这种情况下,您将在搜索结果中获得所有三个站点,并且可能会获得更好的结果。
让我们一步一步地看一
遍vector<int> lotteryNumVect(10);
- 创建
int
秒的向量,将大小设置为 10。
lotteryNumVect.insert(lotteryNumVect.begin(), // Place to insert stuff
lotteryNumArray, // Pointer to start of thing to insert
lotteryNumArray + 3); // Pointer to end of stuff to insert
- 在
lotteryNumVect
中插入前 3 件事lotteryNumArray
- 写入向量<向量<bool>>
- 函数向量_指针有不同的原型,我可以构建一个吗
- std::向量与传递值的动态数组
- 将值指定给向量(2D)的向量中的某个位置
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 如何使用向量的template_back函数
- 尝试通过多个向量访问变量时,向量下标超出范围
- 如何通过派生类函数更改基类中的向量
- C++从另一个类访问公共静态向量的正确方法是什么
- 如何将ampl中的集合表示为c++中的向量
- 变量没有改变?通过向量的函数调用
- 迭代时从向量和内存中删除对象
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- 如何为模板化对象创建模板向量?VS正在投掷C3203
- 计算排序向量的向量中唯一值的计数
- 矩阵向量乘法(cublasDgemv)返回零
- 一对向量构造函数:初始值设定项列表与显式构造
- 将结构向量排序为子组
- 在C++中调整向量中的索引
- 向量元素的引用地址与它所指向的向量元素的地址不同.为什么