c++ stl队列初始化
向量是什么? (What is the vector?)
Vector is a container in C++ STL, it is used to represent array and its size can be changed.
Vector是C ++ STL中的一个容器,用于表示数组,并且其大小可以更改。
Read more: C++ STL Vector
阅读更多: C ++ STL矢量
创建一个向量并从数组初始化它 (Create a vector and initializing it from an array)
We can also initialize a vector from a given array in C++ STL. Here, we are going to learn the same, how can we initialize a vector from a given array?
我们还可以从C ++ STL中的给定数组初始化向量。 在这里,我们将学习相同的知识, 如何从给定数组初始化向量?
Here is the syntax to create and initialize and initialize vector from an array,
这是从数组创建,初始化和初始化向量的语法,
vector<type> vector_name(array_name_from, array_name_to);
Here,
这里,
type – is the datatype.
type –是数据类型。
vector_name – is any use defined name to the vector.
vector_name –是向量的任何使用定义的名称。
array_name_from, array_name_to – from and to indexes of the array.
array_name_from,array_name_to –从数组索引到数组索引。
Example to create/declare and initialize vector from an array
从数组创建/声明和初始化向量的示例
vector<int> v1(iarr, iarr + size);
C ++ STL程序从数组创建和初始化向量 (C++ STL program to create and initialize a vector from an array )
//C++ STL program to create and initialize
//a vector from an array
#include <iostream>
#include <vector>
using namespace std;
int main()
{
//array declaration
int iarr[] = { 10, 20, 30, 40, 50 };
//vector declaration and initialization
//form a given array
//first finding the size of the array
int size = sizeof(iarr) / sizeof(iarr[0]);
vector<int> v1(iarr, iarr + size);
//printing the vector elements
//using for each kind of loop
cout << "Vector v1 elements are: ";
for (int element : v1)
cout << element << " ";
cout << endl;
//pushing the elements
v1.push_back(10);
v1.push_back(20);
v1.push_back(30);
v1.push_back(40);
v1.push_back(50);
//printing the vector elements
//using for each kind of loop
cout << "After pushing the elements\nVector v1 elements are: ";
for (int element : v1)
cout << element << " ";
cout << endl;
return 0;
}
Output
输出量
Vector v1 elements are: 10 20 30 40 50
After pushing the elements
Vector v1 elements are: 10 20 30 40 50 10 20 30 40 50
翻译自: https://www.includehelp.com/stl/create-a-vector-and-initialize-it-from-an-array.aspx
c++ stl队列初始化