Skip to main content

Posts

Showing posts from December, 2025

Introduction to Vectors

Today, let's talk about one of the most powerful and commonly used features in C++ — vectors. A vector is basically a resizable array, meaning it can grow or shrink automatically depending on how many elements you add or remove. Unlike normal arrays in C++—whose size cannot be changed once declared—vectors give us flexibility and safety. To use a vector, we simply include the <vector> header and then write vector<type> name; For example: vector<int> scores = {85, 90, 78}; Vectors are part of the Standard Template Library, also known as STL, which provides ready-made data structures like vectors, stacks, queues, sets, and maps. STL makes programming faster and reduces errors because the functions are well-tested and optimized. Now, one of the most important operations in a vector is push_back(), which adds an element to the end of the vector in constant time—meaning it’s very efficient. We can also insert elements at any position using insert(position, value). Acce...