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 like an array)
We can also initialize a vector like an array in C++ STL. Here, we are going to learn the same, how can we initialize a vector like an array?
我們還可以在C ++ STL中像數組一樣初始化向量。 在這里,我們將學習相同的知識, 我們如何初始化像數組這樣的向量?
Here is the syntax to create and initialize a vector like an array,
這是創建和初始化向量(如數組)的語法,
vector<type> vector_name{element1, element2, ...};
Here,
這里,
type – is the datatype.
type –是數據類型。
vector_name – is any use defined name to the vector.
vector_name –是向量的任何使用定義的名稱。
element1, element2, ... – elements to initialize a vector.
element1,element2,... –用于初始化向量的元素。
Example to create/declare and initialize vector like an array
創建/聲明和初始化向量(如數組)的示例
vector::<int> v1{ 10, 20, 30, 40, 50 };
C ++ STL程序來創建和初始化像數組一樣的向量 (C++ STL program to create and initialize a vector like an array)
//C++ STL program to create and initialize
//a vector like an array
#include <iostream>
#include <vector>
using namespace std;
int main()
{
//vector declaration and initialization
//like an array
vector<int> v1{ 10, 20, 30, 40, 50 };
//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-like-an-arrays.aspx
c++ stl隊列初始化