vector::size in C++ (With Examples)

Hey there! Today, we’re diving into the wonderful world of C++ to discuss a handy feature of the std::vector container - its size function. We’ll start with a brief overview and then walk through its usage with some easy-to-follow examples.

Похожее
vector::insert in C++ (With Examples)
In C++, the vector data structure provides a dynamic array-like mechanism. One of its many features is the vector::insert function, which allows for adding elements at any position in the vector. This article will dive deep into the use and intricacies of this function.
go

Understanding Vector’s Size in C++

If you think of std::vector as a flexible box where you can keep and organize your items (or data), the size function simply tells you how many items you have in that box.

Here’s the basic signature of the size function:

size_type size() const;
  • It doesn’t need any inputs (parameters).
  • It gives back the size of a vector as a number. This number is of a type called size_type, which is a kind of unsigned number.

A Quick Example:

Consider you have a box (std::vector), and you want to know how filled it is. Here’s how you’d do that:

#include <iostream>
#include <vector>

int main() {
  std::vector<int> my_numbers;
  std::cout << "The box contains: " << my_numbers.size() << " items." << std::endl;

  return 0;
}

When you run this, it’d say:

The box contains: 0 items.

Why zero? Because we just made the box and didn’t put anything in it yet!

Filling up our Box:

Vectors in C++ are dynamic. You can add items or remove them. As you make these changes, the size function helps you keep track.

Here’s a quick run-through:

#include <iostream>
#include <vector>

int main() {
  std::vector<int> my_numbers;
  for (int i = 0; i < 10; i++) {
    my_numbers.push_back(i); // Adding items in the box.
  }

  std::cout << "Now the box contains: " << my_numbers.size() << " items." << std::endl;
  return 0;
}

And, the output?

Now the box contains: 10 items.

Easy, right?

Похожее
strlen in C/C++: string length
The strlen function in C is used to count the number of characters in a string. In this article, we'll look at examples of its usage. Additionally, we'll implement this function ourselves, and wrap up with some exercises for further practice.
go

When is this Useful?

Imagine you’re building a game, and you have a list (vector) of players. Sometimes players join, sometimes they leave. By using the size function, you can always know how many players you currently have. This can be useful for things like dividing players into teams, checking if the game has enough players to start, and more.

Things to Remember:

  1. The size and Capacity: The size tells you how many items are in the vector. But vectors also have something called capacity, which tells you how much it can hold before it needs to grow. They aren’t always the same!

  2. Fast and Reliable: Asking a vector its size is super fast, no matter how many elements it contains. And it’s always safe – it won’t modify the vector or the items inside.

  3. Exceptions? None! The best part is that this function doesn’t throw any errors or exceptions. It’s like asking a question and being sure you’ll always get an answer.

  • vector::capacity: As mentioned, this tells you how much your vector can hold before needing to expand.
  • vector::resize: If you want to change the size of your vector (either make it bigger or shrink it), this function is your friend.
  • vector::max_size: Wondering what’s the maximum number of items your vector can possibly hold? This will tell you.

In conclusion, understanding the ins and outs of the std::vector container and its functions like size can make your journey with C++ smoother and more intuitive. Whether you’re just starting out or brushing up on your skills, the dynamic and versatile nature of vectors is sure to be an invaluable tool in your programming toolkit.

Happy coding!

Похожее
The map::find in C++: Efficiently Locating Elements
C++ boasts a rich collection of libraries, and the map container is among its gems. A key function within this container is map::find, which is essential for locating elements swiftly. In this article, we'll dive into the purpose of this function, explore its mechanics, and run through a practical example.
go

Exercises

  1. Understanding the size function:

    • Create a std::vector of type string to store names.
    • Add five names to this vector.
    • Print out the current size of the vector using the size function.
    • Remove two names from the vector.
    • Again, print out the current size of the vector.
  2. Comparing size and capacity:

    • Using the same std::vector of names from the previous task, print out the capacity of the vector.
    • Add 10 more names to the vector.
    • Print the size and the capacity of the vector after these additions.
    • Discuss the difference between size and capacity based on the outputs.
  3. Exploring Other Vector Functions:

    • Use the resize function to increase the size of your vector to 20 and initialize the new elements with the name “John Doe”.
    • Print out all elements of the vector.
    • Use the max_size function to print out the maximum number of items the vector can possibly hold.
    • Reflect on how the resize and max_size functions might be useful in different scenarios.

Discussion

© 2024, codelessons.dev