C Vectors - JohnHau/mis GitHub Wiki

In this tutorial, we will learn about vectors in C++ with the help of examples.

In C++, vectors are used to store elements of similar data types. However, unlike arrays, the size of a vector can grow dynamically.

That is, we can change the size of the vector during the execution of a program as per our requirements.

Vectors are part of the C++ Standard Template Library. To use vectors, we need to include the vector header file in our program.

image

image

image

Example: C++ Vector Initialization #include #include using namespace std;

int main() {

// initializer list vector vector1 = {1, 2, 3, 4, 5};

// uniform initialization vector vector2{6, 7, 8, 9, 10};

// method 3 vector vector3(5, 12);

cout << "vector1 = ";

// ranged loop for (const int& i : vector1) { cout << i << " "; }

cout << "\nvector2 = ";

// ranged loop for (const int& i : vector2) { cout << i << " "; }

cout << "\nvector3 = ";

// ranged loop for (int i : vector3) { cout << i << " "; }

return 0; }

Output

vector1 = 1 2 3 4 5
vector2 = 6 7 8 9 10
vector3 = 12 12 12 12 12 Here, we have declared and initialized three different vectors using three different initialization methods and displayed their contents.

Basic Vector Operations The vector class provides various methods to perform different operations on vectors. We will look at some commonly used vector operations in this tutorial:

Add elements Access elements Change elements Remove elements

image

image

image

image

image

image

image

image

image

image

image

image

image

image

⚠️ **GitHub.com Fallback** ⚠️