C++ Beginner

cpp_013: Arrays vs. Vectors in C++ - Understanding the Differences with Examples

codeaddict 2025. 2. 26. 21:19

Introduction

In C++, both arrays and vectors allow us to store multiple values in a structured format. However, they have fundamental differences in memory management, flexibility, and ease of use. This lesson will compare C++ arrays and vectors, highlight their key differences, and provide two example problems demonstrating when to use each.


🔹 Comparing the Syntax of Arrays and Vectors

Here, T is the data type (e.g., int, double, char), and N is the number of elements.

🔹 Key Differences Between Arrays and Vectors

🟢 Example 1: Finding the Sum of Elements

🔸 Problem Statement:
Write a program that takes N numbers as input, stores them in both an array and a vector, and computes the sum of elements.

✅ Solution Using an Array

#include <iostream>
using namespace std;

int main() {
    int N;
    cout << "Enter number of elements: ";
    cin >> N;
    
    int arr[N]; // Fixed-size array
    cout << "Enter " << N << " numbers: ";
    for(int i = 0; i < N; i++) {
        cin >> arr[i];
    }
    int sum = 0;
    for(int i = 0; i < N; i++) {
        sum += arr[i];
    }
    cout << "Sum using array: " << sum << endl;
    return 0;
}

🔹 Code Explanation:

  • int arr[N]; → Declares a fixed-size array.
  • cin >> arr[i]; → Reads N elements into the array.
  • sum += arr[i]; → Iterates through the array to compute the sum.

✅ Solution Using a Vector (Fixed-Size Approach)

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

int main() {
    int N;
    int sum = 0;

    cout << "Enter number N: ";
    cin >> N;

    vector<int> myvector(N); // Declare a vector with N elements

    for (int i = 0; i < N; i++) {
        cout << "Enter element " << i << " out of " << N << ": ";
        cin >> myvector[i];
        sum += myvector[i]; // Compute the sum
    }

    cout << "Sum of vector is " << sum << endl;
    return 0;
}

🔹 Code Explanation:

  • vector<int> myvector(N); → Declares a vector with a fixed size of N.
  • cin >> myvector[i]; → Reads N elements dynamically.
  • sum += myvector[i]; → Iterates through the vector to compute the sum.

✅ Advantages:

✔ Simple and easy to understand
✔ No need for push_back() since size is fixed

❌ Disadvantages:

✘ Requires N to be known beforehand
✘ If N is too large, unnecessary memory is allocated

Solution Using a Vector (Dynamic Approach)

 

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

int main() {
    int N;
    int sum = 0;
    vector<int> myvector; // Declare an empty vector

    cout << "Enter number N: ";
    cin >> N;

    for (int i = 0; i < N; i++) {
        int value;
        cout << "Enter element " << i << " out of " << N << ": ";
        cin >> value;
        myvector.push_back(value); // Dynamically add elements
        sum += value;
    }

    cout << "Sum of vector is " << sum << endl;
    return 0;
}

🔹 Code Explanation:

  • vector<int> myvector; → Declares an empty vector.
  • myvector.push_back(value); → Dynamically adds elements to the vector.
  • sum += value; → Computes the sum during input.

✅ Advantages:

✔ Can handle unknown or varying N
✔ More memory efficient if N changes frequently

❌ Disadvantages:

✘ push_back() can be slower due to memory reallocation
✘ Uses more memory in some cases due to dynamic resizing

🏆 Output Example

Enter number N: 5  
Enter element 0 out of 5: 2  
Enter element 1 out of 5: 4  
Enter element 2 out of 5: 6  
Enter element 3 out of 5: 8  
Enter element 4 out of 5: 10  
Sum of vector is 30