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.
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?
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:
-
The
size
and Capacity: Thesize
tells you how many items are in the vector. But vectors also have something calledcapacity
, which tells you how much it can hold before it needs to grow. They aren’t always the same! -
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.
-
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.
Some Neat Related Features:
- 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!
Exercises
-
Understanding the
size
function:- Create a
std::vector
of typestring
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.
- Create a
-
Comparing
size
andcapacity
:- 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.
- Using the same
-
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
andmax_size
functions might be useful in different scenarios.
- Use the
Discussion