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 by specifying the size)
We can create a vector by specifying the size and we can also initialize all elements with a default value while declaring it.
我们可以通过指定大小来创建向量,也可以在声明时使用默认值初始化所有元素。
Here is the syntax to create a vector by specifying the size,
这是通过指定大小来创建向量的语法,
vector<type> vector_name(size, default_value);
Here,
这里,
type – is the datatype.
type –是数据类型。
vector_name – is any use defined name to the vector.
vector_name –是向量的任何使用定义的名称。
size – is the initial size of the vector.
size –是向量的初始大小。
default_value – is the value to initialize all elements.
default_value –是初始化所有元素的值。
Example to create/declare vector by specifying the size
通过指定大小创建/声明矢量的示例
vector::<int> v1(5, 10);
C ++ STL程序通过指定大小创建矢量 (C++ STL program to create a vector by specifying the size)
//C++ STL program to create a vector
//by specifying the size
#include <iostream>
#include <vector>
using namespace std;
int main()
{
//vector declaration with size and value
vector<int> v1(5, 10);
//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 10 10 10 10
After pushing the elements
Vector v1 elements are: 10 10 10 10 10 10 20 30 40 50
stl栈有初始默认大小吗?