Arrays and Vectors in C++ | Description & Usage With Examples

What You’ll Learn

By the end of this post, you’ll be able to:

  • Understand what arrays and vectors are
  • Declare and initialize arrays
  • Loop through arrays
  • Use vectors for dynamic lists
  • Perform common operations like insertion, access, and size checking

Why Use Arrays and Vectors?

When you need to store multiple values of the same type, like a list of test scores or names, you can use an array or a vector.

Example problem: You want to store the marks of 5 students.

Without an array:

int m1, m2, m3, m4, m5;

With an array:

int marks[5];

Arrays group values together, making your code cleaner and easier to manage.


Arrays in C++

Declaration:

int numbers[5]; // creates space for 5 integers

Initialization:

int numbers[5] = {10, 20, 30, 40, 50};

You can also omit the size if you’re initializing:

int numbers[] = {10, 20, 30};

Accessing Elements:

Array indexing starts at 0.

cout << numbers[0]; // prints 10
numbers[2] = 99; // changes the 3rd element to 99

Looping Through Arrays

Use a for loop to access each element.

int scores[5] = {88, 76, 92, 85, 69};

for (int i = 0; i < 5; i++) {
cout << "Score " << i << ": " << scores[i] << endl;
}

Inputting into an Array

int marks[5];

cout << "Enter marks of 5 students: ";
for (int i = 0; i < 5; i++) {
cin >> marks[i];
}

Common Mistakes with Arrays

  • Going out of bounds: Accessing numbers[5] in a size 5 array causes undefined behavior.
  • Using the wrong size in loops. Always match the loop with the array size.

When to Use Vectors

Arrays are fixed in size. If you want something dynamic, use a vector.

Vectors are part of the C++ Standard Template Library (STL). To use them:

#include <vector>
using namespace std;

Declaring Vectors

vector<int> numbers; // empty vector of integers

Initialization:

vector<int> scores = {85, 90, 78};

Add Elements:

scores.push_back(95); // adds 95 to the end

Access Elements:

cout << scores[0]; // prints 85
scores[1] = 100; // changes the second element to 100

Looping Through Vectors

vector<int> values = {10, 20, 30, 40};

for (int i = 0; i < values.size(); i++) {
cout << values[i] << endl;
}

You can also use range-based for loops (C++11 and later):

for (int val : values) {
cout << val << endl;

}

Vector Functions You Should Know

FunctionDescription
push_back(x)Adds x to the end
size()Returns number of elements
clear()Removes all elements
empty()Returns true if the vector is empty

Example: Average of N Numbers Using a Vector

#include <iostream>
#include <vector>
using namespace std;

int main() {
int n;
cout << "Enter number of elements: ";
cin >> n;

vector<int> nums;
int input;
int sum = 0;

for (int i = 0; i < n; i++) {
cin >> input;
nums.push_back(input);
sum += input;
}

double average = (double) sum / nums.size();
cout << "Average: " << average << endl;

return 0;
}

Quiz: Arrays and Vectors

Question 1/7

Mini Exercises

  1. Create an array of size 10 and store the first 10 square numbers.
  2. Write a program that finds the maximum value in an array of integers.
  3. Use a vector to read N values from the user, and then print only the even numbers.
  4. Initialize a vector with {1, 2, 3}, then add 4 and 5 using push_back, and print all values.

Summary

In this post, you learned:

  • How to declare, initialize, and access arrays
  • How to loop through arrays for input/output
  • How vectors are dynamic and more flexible than arrays
  • Useful vector functions like push_back() and size()

What’s Next?

In the next post, we’ll explore 2D Arrays — perfect for working with matrices, grids, and board-style problems.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top