Vector Size
Part of the Logic & Flow section of Coddy's C++ journey — lesson 13 of 56.
When working with vectors, you often need to know how many elements they contain. The .size() method provides exactly this information - it returns the current number of elements stored in your vector.
Here's how to use the .size() method:
std::vector<int> numbers = {10, 20, 30, 40, 50};
int count = numbers.size();
std::cout << "The vector has " << count << " elements" << std::endl;This will output "The vector has 5 elements" because our vector contains five integers.
The .size() method is particularly useful when you need to check if a vector is empty, validate array bounds, or set up loops. It returns an unsigned integer representing the exact count of elements currently in the vector, and this count updates automatically as you add or remove elements using methods like push_back().
Challenge
EasyCreate a program that demonstrates how to use the .size() method to track the number of elements in a vector as you build a collection of student scores.
The following inputs will be provided:
- An integer
nrepresenting the number of student scores to process - Then
nintegers representing individual student scores
Your program should:
- Create an empty
std::vector<int>namedscores - Print the initial size of the empty vector
- Use a loop to read each of the
nstudent scores - For each score, add it to the vector using
push_back() - After adding each score, print the current size of the vector
- Finally, print a summary message showing the total number of scores collected
Use the following exact output format:
Initial vector size: 0
After adding score, size is: 1
After adding score, size is: 2
After adding score, size is: 3
...
Total scores collected: [final size]Use the .size() method to get the current number of elements in the vector after each addition and for the final summary.
Cheat sheet
The .size() method returns the current number of elements in a vector:
std::vector<int> numbers = {10, 20, 30, 40, 50};
int count = numbers.size();
std::cout << "The vector has " << count << " elements" << std::endl;The .size() method returns an unsigned integer and updates automatically as you add or remove elements. It's useful for checking if a vector is empty, validating array bounds, or setting up loops.
Try it yourself
#include <iostream>
#include <vector>
using namespace std;
int main() {
// Read the number of scores
int n;
cin >> n;
// Create an empty vector for scores
vector<int> scores;
// TODO: Write your code below
// 1. Print the initial size of the empty vector
// 2. Use a loop to read each score and add it to the vector
// 3. After adding each score, print the current size
// 4. Print the final summary message
return 0;
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Pointers and Memory
What is a Pointer?Address-Of OperatorDereference OperatorNull PointersPointers and ArraysDynamic Memory with 'new'Freeing Memory with 'delete'Recap - Pointer Practice2Vectors (Dynamic Arrays)
Introducing std::vectorCreating a VectorAdding ElementsAccessing ElementsVector SizeIterating with a For LoopRange-Based For LoopRemoving ElementsRecap - Vector Operations5Project: Inventory Tool
Project SetupAdding and Updating Items