📜  用C++初始化向量(6种不同方式)

📅  最后修改于: 2021-05-30 15:12:51             🧑  作者: Mango

以下是在C++ STL中创建和初始化向量的不同方法

1.通过一键推值进行初始化:

CPP
// CPP program to create an empty vector
// and push values one by one.
#include 
using namespace std;
 
int main()
{
    // Create an empty vector
    vector vect;
 
    vect.push_back(10);
    vect.push_back(20);
    vect.push_back(30);
 
    for (int x : vect)
        cout << x << " ";
 
    return 0;
}


CPP
// CPP program to create an empty vector
// and push values one by one.
#include 
using namespace std;
 
int main()
{
    int n = 3;
 
    // Create a vector of size n with
    // all values as 10.
    vector vect(n, 10);
 
    for (int x : vect)
        cout << x << " ";
 
    return 0;
}


CPP
// CPP program to initialize a vector like
// an array.
#include 
using namespace std;
 
int main()
{
    vector vect{ 10, 20, 30 };
 
    for (int x : vect)
        cout << x << " ";
 
    return 0;
}


CPP
// CPP program to initialize a vector from
// an array.
#include 
using namespace std;
 
int main()
{
    int arr[] = { 10, 20, 30 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    vector vect(arr, arr + n);
 
    for (int x : vect)
        cout << x << " ";
 
    return 0;
}


CPP
// CPP program to initialize a vector from
// another vector.
#include 
using namespace std;
 
int main()
{
    vector vect1{ 10, 20, 30 };
 
    vector vect2(vect1.begin(), vect1.end());
 
    for (int x : vect2)
        cout << x << " ";
 
    return 0;
}


CPP
#include 
using namespace std;
 
int main()
{
    vector vect1(10);
    int value = 5;
    fill(vect1.begin(), vect1.end(), value);
    for (int x : vect1)
        cout << x << " ";
}


输出:

10 20 30

2.指定大小并初始化所有值:

CPP

// CPP program to create an empty vector
// and push values one by one.
#include 
using namespace std;
 
int main()
{
    int n = 3;
 
    // Create a vector of size n with
    // all values as 10.
    vector vect(n, 10);
 
    for (int x : vect)
        cout << x << " ";
 
    return 0;
}

输出:

10 10 10

3.初始化类似数组:

CPP

// CPP program to initialize a vector like
// an array.
#include 
using namespace std;
 
int main()
{
    vector vect{ 10, 20, 30 };
 
    for (int x : vect)
        cout << x << " ";
 
    return 0;
}

输出:

10 20 30

4.从数组初始化:

CPP

// CPP program to initialize a vector from
// an array.
#include 
using namespace std;
 
int main()
{
    int arr[] = { 10, 20, 30 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    vector vect(arr, arr + n);
 
    for (int x : vect)
        cout << x << " ";
 
    return 0;
}

输出:

10 20 30

5.从另一个向量初始化:

CPP

// CPP program to initialize a vector from
// another vector.
#include 
using namespace std;
 
int main()
{
    vector vect1{ 10, 20, 30 };
 
    vector vect2(vect1.begin(), vect1.end());
 
    for (int x : vect2)
        cout << x << " ";
 
    return 0;
}

输出:

10 20 30

6.初始化具有特定值的所有元素:

CPP

#include 
using namespace std;
 
int main()
{
    vector vect1(10);
    int value = 5;
    fill(vect1.begin(), vect1.end(), value);
    for (int x : vect1)
        cout << x << " ";
}

输出:

5 5 5 5 5 5 5 5 5 5
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”