Menu
Coddy logo textTech

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 icon

Challenge

Easy

Create 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 n representing the number of student scores to process
  • Then n integers representing individual student scores

Your program should:

  1. Create an empty std::vector<int> named scores
  2. Print the initial size of the empty vector
  3. Use a loop to read each of the n student scores
  4. For each score, add it to the vector using push_back()
  5. After adding each score, print the current size of the vector
  6. 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;
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow