From 1b878d7c7f86fc2dc25e13630ce2699a1bfdf854 Mon Sep 17 00:00:00 2001 From: Sriparno Roy Date: Fri, 4 Jul 2025 18:41:16 +0530 Subject: [PATCH 1/2] [Edit] C++: .size() --- .../cpp/concepts/vectors/terms/size/size.md | 91 +++++++++++++++++-- 1 file changed, 81 insertions(+), 10 deletions(-) diff --git a/content/cpp/concepts/vectors/terms/size/size.md b/content/cpp/concepts/vectors/terms/size/size.md index 48230467d1f..3edb3523fc6 100644 --- a/content/cpp/concepts/vectors/terms/size/size.md +++ b/content/cpp/concepts/vectors/terms/size/size.md @@ -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. 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). Understanding how and when to use `.size()` is essential for writing efficient and bug-free C++ code involving vectors. ## Syntax @@ -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 #include int main() { - // Declaring a vector with 4 integers - std::vector numbers = {1, 2, 3, 4}; + // Declaring a vector with 4 integers + std::vector 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 +#include +using namespace std; + +int main() { + vector 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 +#include +using namespace std; + +int main() { + vector 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. From 357ba06a22234b61e85b6a3a314053eb607dc119 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Sat, 5 Jul 2025 12:51:06 +0530 Subject: [PATCH 2/2] minor fixes --- content/cpp/concepts/vectors/terms/size/size.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/cpp/concepts/vectors/terms/size/size.md b/content/cpp/concepts/vectors/terms/size/size.md index 3edb3523fc6..2143659fb71 100644 --- a/content/cpp/concepts/vectors/terms/size/size.md +++ b/content/cpp/concepts/vectors/terms/size/size.md @@ -14,7 +14,7 @@ CatalogContent: - 'paths/computer-science' --- -In C++, 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). Understanding how and when to use `.size()` is essential for writing efficient and bug-free C++ code involving vectors. +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