Difference between vector<> and array() in C++

In C++, both vectors and arrays are used to store collections of elements, but they have significant differences in terms of flexibility, functionality, and usage. Here's a comparison of the two:

Arrays

  1. Static Size:

    • The size of an array must be known at compile-time and cannot be changed at runtime. This makes arrays a fixed-size data structure.
  2. Memory Allocation:

    • Arrays can be allocated on the stack (for small fixed-size arrays) or on the heap (for dynamic arrays using pointers and manual memory management).
  3. Syntax:

    • Declaring an array: int arr[10];

    • Accessing elements: arr[0] = 5;

  4. Performance:

    • Arrays have minimal overhead because they do not need to manage memory dynamically. This can make them slightly faster for certain operations.
  5. Standard Library Support:

    • Arrays do not provide many built-in functions. You need to manually handle tasks such as finding the size, sorting, etc.

Vectors

  1. Dynamic Size:

    • Vectors are dynamic arrays that can grow and shrink in size automatically. They manage their own memory, allowing you to add and remove elements easily.
  2. Memory Allocation:

    • Vectors allocate memory dynamically on the heap. They handle reallocation when the current capacity is exceeded, typically by allocating a larger block of memory and copying existing elements to the new block.
  3. Syntax:

    • Declaring a vector: std::vector<int> vec;

    • Adding elements: vec.push_back(5);

    • Accessing elements: vec[0] = 5;

  4. Performance:

    • Vectors have more overhead than arrays due to dynamic memory management. However, they provide more flexibility and convenience.
  5. Standard Library Support:

    • Vectors are part of the C++ Standard Library and provide many useful member functions such as size(), push_back(), pop_back(), clear(), insert(), and erase(). These functions make it easier to manipulate the collection of elements.

Key Differences

  1. Size and Flexibility:

    • Arrays have a fixed size, whereas vectors can dynamically resize themselves.
  2. Memory Management:

    • Arrays require manual memory management if dynamically allocated, while vectors handle memory allocation and deallocation automatically.
  3. Functionality:

    • Vectors provide a rich set of member functions for managing elements, whereas arrays have minimal built-in support.
  4. Safety and Ease of Use:

    • Vectors are generally safer and easier to use than arrays because they manage memory and provide bounds checking (with the at() member function).
  5. Performance Trade-offs:

    • Arrays can be slightly faster and more memory-efficient for small, fixed-size collections due to lower overhead, while vectors offer greater flexibility at the cost of some performance overhead due to dynamic resizing.

Example Code

Array Example

#include <iostream>

int main() {
    int arr[5]; // fixed-size array
    arr[0] = 1;
    arr[1] = 2;
    arr[2] = 3;
    arr[3] = 4;
    arr[4] = 5;

    for (int i = 0; i < 5; ++i) {
        std::cout << arr[i] << " ";
    }

    return 0;
}

Vector Example

#include <iostream>
#include <vector>

int main() {
    std::vector<int> vec; // dynamic-size vector
    vec.push_back(1);
    vec.push_back(2);
    vec.push_back(3);
    vec.push_back(4);
    vec.push_back(5);

    for (int i = 0; i < vec.size(); ++i) {
        std::cout << vec[i] << " ";
    }

    return 0;
}

In summary, use arrays when you need a fixed-size collection and performance is critical. Use vectors when you need a dynamically sized collection and convenience or safety is more important.