c++用数组初始化向量
Vector in C++ STL in one of the most convenient data structure which can be used as a dynamic array. There are several ways to define a vector in C++ and here we discuss all of them in detail.
C ++ STL中的向量 ,是最方便的数据结构之一,可以用作动态数组。 有几种方法可以在C ++中定义向量,在这里我们将详细讨论所有这些方法。
一维矢量简介 (1D vector in brief)
A 1D vector is a linear data structure having exactly similar features like a 1D array. Vector has added advantage of dynamic features which helps it to grow at runtime, which means extending dynamically. Now to initialize a vector there have been several methods like below,
一维向量是一种线性数据结构,具有与一维数组完全相似的特征。 Vector增加了动态功能的优势,这有助于它在运行时增长,这意味着可以动态扩展。 现在,要初始化向量,可以使用以下几种方法,
1)定义空向量,然后根据需要定义push_back() (1) Define empty vector and then push_back() as required)
This is the most naïve approach to initialize a vector. Firstly, we just define an empty vector. At that point, it has no idea about how many elements it’s going to have. Then by using push_back() function we can simply keep adding elements at the back as per requirement.
这是初始化向量的最幼稚的方法。 首先,我们只定义一个空向量。 在那时,它尚不知道它将要包含多少个元素。 然后,通过使用push_back()函数,我们可以根据需要简单地在后面添加元素。
Below is an example to add elements as per user wants.
以下是根据用户需要添加元素的示例。
#include <bits/stdc++.h>
using namespace std;
int main()
{
//empty vector initiali