c++ stl队列初始化_使用C ++ STL中的不同方法创建和初始化向量

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矢量

Following are the different ways by using them we can create and initialize a vector in C++ STL,

以下是使用它们的不同方法,我们可以在C ++ STL中创建和初始化向量,

1)创建一个空向量并通过推入值进行初始化 (1) Create an empty vector and initialize by pushing values)

//Create an empty vector and initialize by pushing values
#include <iostream>
#include <vector>
using namespace std;

int main()
{
    //vector declaration
    vector<int> v1;

    //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
    //creating iterator to access the elements
    vector<int>::iterator it;
    cout << "Vector v1 elements are: ";
    for (it = v1.begin(); it != v1.end(); it++)
        cout << *it << " ";
    cout << endl;

    return 0;
}

Output

输出量

Vector v1 elements are: 10 20 30 40 50

2)通过指定大小创建向量,并使用默认值初始化元素 (2) Create a vector by specifying the size and initialize elements with a default value)

//Create a vector by specifying the size and
//initialize elements with a default value
#include <iostream>
#include <vector>
using namespace std;

int main()
{
    //vector declaration
    vector<int> v1(5, 10);

    //printing the vector elements
    //creating iterator to access the elements
    vector<int>::iterator it;
    cout << "Vector v1 elements are: ";
    for (it = v1.begin(); it != v1.end(); it++)
        cout << *it << " ";
    cout << endl;

    return 0;
}

Output

输出量

Vector v1 elements are: 10 10 10 10 10

3)创建向量并将其初始化为数组 (3) Create a vector and initialize it like an array)

//Create a vector and initialize it like an array
#include <iostream>
#include <vector>
using namespace std;

int main()
{
    //vector declaration
    vector<int> v1{ 10, 20, 30, 40, 50 };

    //printing the vector elements
    //creating iterator to access the elements
    vector<int>::iterator it;
    cout << "Vector v1 elements are: ";
    for (it = v1.begin(); it != v1.end(); it++)
        cout << *it << " ";
    cout << endl;

    return 0;
}

Output

输出量

Vector v1 elements are: 10 20 30 40 50

4)创建向量并从数组初始化 (4) Create a vector and initialize it from an array)

//Create a vector and initialize it from an array
#include <iostream>
#include <vector>
using namespace std;

int main()
{
    //array
    int arr[] = { 10, 20, 30, 40, 50 };

    //vector declaration from the array
    //array size
    int size = sizeof(arr) / sizeof(arr[0]);
    vector<int> v1(arr, arr + size);

    //printing the vector elements
    //creating iterator to access the elements
    vector<int>::iterator it;
    cout << "Vector v1 elements are: ";
    for (it = v1.begin(); it != v1.end(); it++)
        cout << *it << " ";
    cout << endl;

    return 0;
}

Output

输出量

Vector v1 elements are: 10 20 30 40 50

5)创建一个向量,然后从另一个向量初始化它 (5) Create a vector and initialize it from another vector )

//Create a vector and initialize it from another vector
#include <iostream>
#include <vector>
using namespace std;

int main()
{
    //vector
    vector<int> v1{ 10, 20, 30, 40, 50 };

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

    //printing the vector elements
    //creating iterator to access the elements
    vector<int>::iterator it;
    cout << "Vector v2 elements are: ";
    for (it = v2.begin(); it != v2.end(); it++)
        cout << *it << " ";
    cout << endl;

    return 0;
}

Output

输出量

Vector v2 elements are: 10 20 30 40 50


翻译自: https://www.includehelp.com/stl/create-and-initialize-a-vector-using-different-ways.aspx

c++ stl队列初始化

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值