Initializing a Vector

C++C++Beginner
Practice Now

Introduction

In this lab, we will learn how to initialize vectors in C++. A vector is a dynamically resizable array with the ability to resize itself depending on the number of elements to be inserted or deleted. Vectors are more advantageous than ordinary arrays, which are of fixed size and are static in nature.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL cpp(("`C++`")) -.-> cpp/SyntaxandStyleGroup(["`Syntax and Style`"]) cpp(("`C++`")) -.-> cpp/BasicsGroup(["`Basics`"]) cpp(("`C++`")) -.-> cpp/ControlFlowGroup(["`Control Flow`"]) cpp/SyntaxandStyleGroup -.-> cpp/comments("`Comments`") cpp/BasicsGroup -.-> cpp/variables("`Variables`") cpp/BasicsGroup -.-> cpp/data_types("`Data Types`") cpp/BasicsGroup -.-> cpp/operators("`Operators`") cpp/ControlFlowGroup -.-> cpp/for_loop("`For Loop`") cpp/BasicsGroup -.-> cpp/arrays("`Arrays`") subgraph Lab Skills cpp/comments -.-> lab-96148{{"`Initializing a Vector `"}} cpp/variables -.-> lab-96148{{"`Initializing a Vector `"}} cpp/data_types -.-> lab-96148{{"`Initializing a Vector `"}} cpp/operators -.-> lab-96148{{"`Initializing a Vector `"}} cpp/for_loop -.-> lab-96148{{"`Initializing a Vector `"}} cpp/arrays -.-> lab-96148{{"`Initializing a Vector `"}} end

Create a new C++ file

In your terminal, navigate to the project directory using the cd command. Create a new C++ file called main.cpp using the following command:

touch ~/project/main.cpp

Open the file using your favorite code editor.

Initialize a Vector like an Array

Create and initialize a vector like an array using the following code:

// Initializing a vector like an array
vector<int> v{1, 2, 3, 4};

// Print the vector
cout << "The elements of the first vector are: ";
for (int i : v)
{
    cout << i << " ";
}

This will create a vector v and initialize it with four elements. We can then print the elements of this vector using the for loop.

Initializing a Vector from an Array

Create and initialize a vector from an array using the following code:

// Initializing a vector from an array
int a[] = {11, 22, 33};
int n = sizeof(a) / sizeof(a[0]); // number of elements in the array
vector<int> v1(a, a + n); // create and initialize the vector from the array

// Print the vector
cout << "The elements of the second vector are: ";
for (int i : v1)
{
    cout << i << " ";
}

This will create an array a and initialize it with three elements. We can then calculate the number of elements in this array and create a vector v1 and initialize it with the elements of the array. We can then print the elements of this vector using the for loop.

Initializing a Vector from another Vector

Create and initialize a vector from another vector using the following code:

// Initializing a vector from another vector
vector<int> a1 = {10, 22, 33};
vector<int> b(a1.begin(), a1.end()); // create and initialize the second vector with the elements of the first vector

// Print the vector
cout << "The elements of the third vector are: ";
for (int i : b)
{
    cout << i << " ";
}

This will create a vector a1 and initialize it with three elements. We can then create another vector b and initialize it with the elements of vector a1. We can then print the elements of vector b using the for loop.

Compile and run the code

Save the changes to the main.cpp file. Compile the code using the following command:

g++ main.cpp -o main

Run the executable file using the following command:

./main

View the output

View the output on the terminal. You should see the following output:

The elements of the first vector are: 1 2 3 4

The elements of the second vector are: 11 22 33

The elements of the third vector are: 10 22 33

Summary

In this lab, we learned how to initialize vectors in C++ using different methods. We used an array to initialize a vector, initialized a vector with the elements of another vector, and initialized a vector like an array. Knowing the different ways to initialize vectors in C++ can help us write more efficient and readable code.

Other C++ Tutorials you may like