DEV Community

Cover image for C++ Arrays and Vectors: How I understood the Differences
Harold Mudosa
Harold Mudosa

Posted on

C++ Arrays and Vectors: How I understood the Differences

Yearning to become an Unreal Engine and VR developer, I’ve been diving into C++ fundamentals. Coming from a JavaScript background, I initially thought that working with arrays would be more than enough. That was until I started tackling coding challenges and realized that arrays in C++ aren’t quite the all-stars they are in JavaScript.

I was soon forced to learn about vectors. Realizing I had a big knowledge gap, I spent over 10 hours trying to understand why things suddenly felt so complicated, especially when I thought I already had it figured out, being a JavaScript professional.

Through this process, I identified some key differences between arrays and vectors in C++ that really helped me make sense of things.

1. How to read their collection length

- Arrays

You need to know their size beforehand or calculate them manually based on your full array and its first element's size in bytes.

int numbers[5] = {1, 2, 3, 4, 5};
int length = sizeof(numbers) / sizeof(numbers[0]); // calculates number of elements based on their size in bytes(sizeof() is for sizes in bytes)
std::cout << "Array length: " << length << std::endl; //Array length: 5
Enter fullscreen mode Exit fullscreen mode

- Vectors

They have a built-in .size() method, which makes it much easier(in my opinion😁).

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

vector<int> numbers = {1, 2, 3, 4, 5};
cout << "Vector length: " << numbers.size() << endl; //5
Enter fullscreen mode Exit fullscreen mode

2. Their Memory Allocation

- Arrays

At compile time, their size must be known, and once an array is created, you cannot change its size.

int numbers[5]; // memory for 5 ints is allocated at compile time

Enter fullscreen mode Exit fullscreen mode

- Vectors

They are dynamic, and since for them the memory is managed at runtime, their size doesn't need to be declared beforehand.

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

vector<int> numbers;      // no initial size needed
Enter fullscreen mode Exit fullscreen mode

3. Their growth in size

- Arrays

Their size can grow, but only up to the number of elements you initially allocated. You cannot exceed that size (unless you create a new array😌).

int numbers[5] = {1, 2, 3}; // can only grow up to 5
Enter fullscreen mode Exit fullscreen mode

- Vectors

They can grow beyond their initial capacity. When you add more elements than the current capacity(using the .push_back method), vectors automatically allocate more memory internally to accommodate the new elements.

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

vector<int> numbers = {1, 2, 3};
numbers.push_back(4);  // grows automatically
numbers.push_back(5);  
cout << "Vector size: " << numbers.size() << endl; // Vector size: 5
Enter fullscreen mode Exit fullscreen mode

3. Their decrease in size

- Arrays

Their size is fixed at compile time. You cannot remove elements or shrink the array. You can only overwrite values, but the memory and “length” stay the same. Here is how it's done:

  • Knowing that int numbers[5] = {1,2,3} is the same as {1,2,3,0,0} under the hood, the only way to decrease numbers in size is by updating, for example, the item 3 to 0.
int arr[3] = {1, 2, 3};
arr[2] = 0; // you can overwrite but cannot remove arr[2] (now we'll have something similar to {1, 2, 0, 0, 0}) ❌
Enter fullscreen mode Exit fullscreen mode

- Vectors

They are dynamic, so you can remove elements at runtime using .pop_back() to remove the last element or .erase() to remove specific elements.

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

vector<int> numbers = {1, 2, 3, 4};
numbers.pop_back();      // removes 4
numbers.erase(numbers.begin() + 1); // removes 2
cout << "Vector size: " << numbers.size() << endl; // Vector size: 2
Enter fullscreen mode Exit fullscreen mode

4.Their compatibility with the STL algorithm

- Arrays

You can use the Standard Template Library(STL) Algorithms. But for some cases, you'll need to provide pointers or iterators, which feels kinda manual.

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

// to sort an array, you need to first specify the range
int numbers[] = {5, 2, 4, 1, 3};
int n = sizeof(numbers) / sizeof(numbers[0]);
sort(numbers, numbers + n);

for(int x : numbers)
    cout << x << " "; // 1 2 3 4 5
Enter fullscreen mode Exit fullscreen mode

Vectors

They work directly with STL algorithms without extra pointers, making code cleaner and safer than it is for arrays (according to me🤫).

#include <algorithm>
#include <iostream>
using namespace stf;

vector<int> numbers = {5, 2, 4, 1, 3};
sort(numbers.begin(), numbers.end());

for(int x: numbers) {
  cout << x << " "; // 1 2 3 4 5
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

Arrays and vectors each have their strengths. Arrays are simple, fixed-size, and slightly faster for certain operations, while vectors are dynamic, flexible, and work seamlessly with modern C++ features like STL algorithms.

I hope the differences we covered here: length, dynamic growth, memory allocation, initialization, shrinking, templates, and STL compatibility, will help you understand when and why to use each.

For a deeper dive, check out GeeksforGeeks: Advantages of Vector over Array in C++.

Top comments (0)