stl向量_创建一个向量,并从C ++ STL中的另一个向量初始化它

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 another vector)

We can also initialize a vector from a given vector in C++ STL. Here, we are going to learn the same, how can we initialize a vector from a given vector?

我们还可以从C ++ STL中的给定向量初始化向量。 在这里,我们将学习相同的知识, 如何从给定向量初始化向量?

Here is the syntax to create and initialize and initialize vector from another vector,

这是从另一个向量创建,初始化和初始化向量的语法,

    vector<type> vector_name(another_vector.begin(), another_vector.end());

Here,

这里,

  • type – is the datatype.

    type –是数据类型。

  • vector_name – is any use defined name to the vector.

    vector_name –是向量的任何使用定义的名称。

  • another_vector.begin(), another_vector.end() – another vector's begin() and end() functions.

    another_vector.begin(),another_vector.end() –另一个向量的begin()end()函数。

Example to create/declare and initialize vector from another vector

从另一个向量创建/声明和初始化向量的示例

    vector<int> v2(v1.begin(), v1.end());

C ++ STL程序从另一个向量创建和初始化向量 (C++ STL program to create and initialize a vector from another vector)

//C++ STL program to create and initialize 
//a vector from another vector
#include <iostream>
#include <vector>
using namespace std;

int main()
{

    //vector declaration and initialization
    vector<int> v1{ 10, 20, 30, 40, 50 };

    //vector declaration and initialization
    //from given vector v1
    vector<int> v2(v1.begin(), v1.end());

    //printing the vector elements
    //using for each kind of loop
    cout << "Vector v2 elements are: ";
    for (int element : v2)
        cout << element << " ";
    cout << endl;

    return 0;
}

Output

输出量

Vector v2 elements are: 10 20 30 40 50


翻译自: https://www.includehelp.com/stl/create-a-vector-and-initialize-it-from-another-vector.aspx

stl向量

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值