Skip to content

[Edit] C++: .size() #7261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 8, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 81 additions & 10 deletions content/cpp/concepts/vectors/terms/size/size.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
---
Title: '.size()'
Description: 'Returns the number of elements in the vector.'
Description: 'Returns the number of elements in a vector.'
Subjects:
- 'Computer Science'
- 'Game Development'
Tags:
- 'Vectors'
- 'Arrays'
- 'Data Structures'
- 'Elements'
- 'Vectors'
CatalogContent:
- 'learn-c-plus-plus'
- 'paths/computer-science'
---

The **`.size()`** method returns the number of elements in the vector. It follows the consistency of other standard library containers, such as [maps](https://www.codecademy.com/resources/docs/cpp/maps) and [strings](https://www.codecademy.com/resources/docs/cpp/strings).
In C++, the **`.size()`** method returns the number of elements in the vector. This method is also available for other STL containers like `std::string`, `std::map`, and `std::list`. Knowing how to use `.size()` effectively helps in writing safe and efficient loops and conditionals.

## Syntax

Expand All @@ -23,25 +24,95 @@ vector.size();

The `vector` must be defined using `std::vector` before the `.size()` method can be used.

## Example
**Parameters:**

The `.size()` method does not accept any parameters.

**Return value:**

Returns the number of elements in a vector.

## Example 1: Basic Usage

In the example below, `.size()` is called on the `numbers` vector:
In this example, `.size()` is called on the `numbers` vector:

```cpp
#include <iostream>
#include <vector>

int main() {
// Declaring a vector with 4 integers
std::vector<int> numbers = {1, 2, 3, 4};
// Declaring a vector with 4 integers
std::vector<int> numbers = {1, 2, 3, 4};

// Print out vector size
std::cout << numbers.size();
// Print out vector size
std::cout << numbers.size();
}
```

The output of the above code is:
Here is the output:

```shell
4
```

## Example 2: Using `.size()` in a Loop

This example uses the `.size()` method in a `for` loop:

```cpp
#include <iostream>
#include <vector>
using namespace std;

int main() {
vector<string> fruits = {"Apple", "Banana", "Cherry"};

for (size_t i = 0; i < fruits.size(); ++i) {
cout << fruits[i] << endl;
}

return 0;
}
```

Here is the output:

```shell
Apple
Banana
Cherry
```

## Codebyte Example: Checking if a Vector is Empty

In this codebyte example, `.size()` is used to determine if a vector is empty:

```cpp
#include <iostream>
#include <vector>
using namespace std;

int main() {
vector<int> data;

if (data.size() == 0) {
cout << "The vector is empty." << endl;
}

return 0;
}
```

## Frequently Asked Questions

### 1. Can `.size()` be used with any STL container?

Yes, `.size()` is a member function of most STL containers like `vector`, `string`, `list`, `map`, etc. It behaves similarly across them.

### 2. What is the difference between `.size()` and `.capacity()`?

`.size()` returns the actual number of items in a vector, whereas `.capacity()` returns the number of elements a vector can hold before needing to allocate more memory.

### 3. Is `.size()` a costly operation?

No, `.size()` runs in constant time, _O(1)_, because vectors store their size internally and update it automatically during insertions or deletions.