Create and initialize a vector using different ways in C++ STL

C++ STL | Create and Initialize a vector: Here, we are going to learn how to create a vector and how to initialize it using different ways in C++ STL?
Submitted by IncludeHelp, on May 12, 2019

What is the vector?

Vector is a container in C++ STL, it is used to represent array and its size can be changed.

Read more: C++ STL Vector

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

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

Related Tutorials




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.