std::vector Basics: push_back size capacity
Create, grow and inspect vectors and understand the capacity vs size distinction.
std::vector Basics: push_back size capacity is a free C++ Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why std::vector?
std::vector is the default container in C++. It grows automatically, manages memory for you, and gives O(1) random access. Use it unless you have a specific reason not to.
Including and Declaring
Include <vector> and declare with the element type as a template parameter.
#include <vector>
std::vector<int> nums;
std::vector<std::string> names = {"Ada", "Bob"};Initialization Forms
Several ways to start a vector:
std::vector<int> a; // empty
std::vector<int> b(5); // 5 zeros
std::vector<int> c(5, 42); // 5 copies of 42
std::vector<int> d{1, 2, 3}; // {1,2,3}
std::vector<int> e(d); // copy of dpush_back: Append at the End
Add an element to the end with push_back. Amortized O(1).
std::vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);emplace_back: Construct in Place
emplace_back forwards its arguments to the element constructor — no temporary object. Slightly faster for non-trivial types.
std::vector<std::pair<int,int>> v;
v.emplace_back(1, 2); // constructs a pair in placesize vs capacity
Two distinct concepts:
- size — number of elements currently stored
- capacity — number of elements the storage can hold without reallocating
Capacity grows in chunks, usually doubling.
Inspecting size and capacity
Use the matching member functions:
std::vector<int> v;
for (int i = 0; i < 10; ++i) {
v.push_back(i);
std::cout << "size=" << v.size() << " capacity=" << v.capacity() << "\n";
}reserve: Preallocate Capacity
If you know how many elements you will add, call reserve first to avoid repeated reallocations.
std::vector<int> v;
v.reserve(1000000); // preallocate
for (int i = 0; i < 1000000; ++i) v.push_back(i);shrink_to_fit: Release Memory
After deleting many elements, free unused capacity with shrink_to_fit. The behavior is non-binding — the implementation may keep extra space.
Accessing Elements
Four ways:
v[i]— uncheckedv.at(i)— bounds-checked, throwsv.front()— first elementv.back()— last element
Empty and clear
Check emptiness with empty(). Remove all elements with clear() — capacity is unchanged.
Quick Check
Why does push_back have amortized O(1) time?
Recap
std::vector is a growable array with O(1) random access. size() is element count; capacity() is storage size. reserve() avoids reallocation when the final size is known.
Frequently asked questions
Is the “std::vector Basics: push_back size capacity” lesson free?
Yes — the full text of “std::vector Basics: push_back size capacity” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.
What will I learn in “std::vector Basics: push_back size capacity”?
Create, grow and inspect vectors and understand the capacity vs size distinction. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C++ Academy?
No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “std::vector Basics: push_back size capacity” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C++ Academy lesson?
Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- std::vector Basics: push_back size capacity
- Iterating Vectors: index range-for iterators
- Modifying Vectors: insert erase clear
- Vector vs std::array vs C Array