The C++ Equivalent to Java’s ArrayList: Exploring std::vector

Adam Rizk
4 min readMay 9, 2024

Did you ever have a question in mind, “If there exists an equivalent to Java’s ArrayList in C++?” Well, we’re going to get to the bottom of the dynamic array problem, specifically with Java’s ArrayList and C++’s std::vector. Come with me as we unpack these data structures, learn what they have in common, discover their differences, and delve into one of C++’s most useful functions for efficiency: reserve().

We’ll look at some code examples and explain things in plain English so you can see how amazing it is to be able to control an array that grows with your program.

std::vector in C++
std::vector in C++

Understanding Java’s ArrayList and C++’s Vector

In Java, ArrayList is a part of the Java Collections Framework. It's a dynamic array, capable of resizing itself as more elements are added. It allows for seamless addition and removal of elements, automatically adjusting its capacity.

Now, switching to C++, the closest relative to Java’s ArrayList is std::vector. Both are designed to store elements in a contiguous block of memory and dynamically adjust their size.

Java’s ArrayList equivalent C++’s std::vector

For instance, let’s take an example:

#include <iostream>
#include <vector>
int main() {
std::vector<int> numbers; // Vector declaration
numbers.push_back(5); // Adding element
numbers.push_back(10); // Adding another element
std::cout << "The elements are: ";
for(int num : numbers) {
std::cout << num << " ";
}
std::cout << std::endl;

return 0;
}

So, here’s what you can expect as an output:

The elements are: 5 10

As shown above, vectors in C++ work almost identically to an ArrayList with smooth handling and retrieval capabilities. It demonstrates the ease with which std::vector can be used to store and access elements, akin to Java’s ArrayList

std::vector and the Magic of reserve()

Now, let’s address the reserve() function. The reserve() function is a performance optimization tool in C++. It pre-allocates memory for a specified number of elements, thus avoiding the cost of frequent reallocations as the vector grows.

When you know the number of elements that a vector will hold, reserve() can be a game-changer in terms of efficiency.

Efficient Memory Allocation Example:

#include <iostream>
#include <vector>
int main() {
// Creating a vector with reserved memory
std::vector<int> numbers;
numbers.reserve(100); // Reserving space for 100 integers
// Populating the vector without causing reallocations
for(int i = 0; i < 100; ++i) {
numbers.push_back(i * 2);
}
// Examining the capacity and size of the vector
std::cout << "Capacity of vector: " << numbers.capacity() << std::endl;
std::cout << "Number of elements in vector: " << numbers.size() << std::endl;
return 0;
}

And here we have the Output:

Capacity of vector: 100
Number of elements in vector: 100

The capacity remains unchanged at 100, showcasing the effectiveness of reserve() in preventing unnecessary memory reallocations.

Comparing Performance: reserve()-Enhanced Vector vs. Standard Vector

Without reserve(), a std::vector may undergo multiple memory reallocations as it grows, each of which can be computationally expensive. By using reserve(), you can allocate sufficient memory from the outset, enhancing performance, especially when dealing with large data sets.

The Safety Net and Versatility of std::vector

Unlike raw arrays, vectors in C++ offer a safer and more flexible way to handle collections of elements. They come equipped with member functions like push_back(), pop_back(), and size(), which make working with dynamic arrays more intuitive and less error-prone.

Let’s take an illustrative example:

#include <iostream>
#include <vector>
int main() {
// Initializing a vector with some integers
std::vector<int> digits{1, 2, 3};
// Removing the last element
digits.pop_back();
// Displaying the remaining elements
std::cout << "After pop_back: ";
for(int digit : digits) {
std::cout << digit << " ";
}
std::cout << std::endl;
return 0;
}

Look at the Output:

After pop_back: 1 2

Wrapping Up: Embracing std::vector in C++

In conclusion, std::vector stands as a formidable equivalent to Java’s ArrayList, enriched with features that enhance its utility and performance. The strategic use of reserve() further elevates its efficiency, making it indispensable for high-performance applications.

As you continue on your programming journey, whether you’re just starting out or refining your expertise, mastering std::vector and its associated functions will undoubtedly empower your development process and optimize your applications.

Get on your C++ endeavors with confidence, and let std::vector be your ally in crafting efficient, robust, and safe code.

Happy coding, and may your journey through C++ be as dynamic and adaptable as the std::vector itself!

If you learnt something or found this useful, upvote & check me out on Twitter & Linkedin:

Twitter: https://x.com/AdamR1776

Linkedin: https://www.linkedin.com/in/adamr1776

--

--

Adam Rizk

A Software Engineer making tech simplified with easy to understand blogs :)