C++ Vector: A pretty simple guide.

C++ Vector: A pretty simple guide.

Image for post

I am studying the basics of C++ Standard Template Library (or STL) to improve my abilities to solve more competitive programming problems.

Here we?ll start with Vector and see how we can use its features.

Vector

If you?ve already played with arrays in another language (or even in C/C++), you have a pretty good idea about STL Vector. It is a container that can store elements, but its size can change dynamically (C/C++ array?s size do not change dynamically).

Image for post

Let?s start with the initialization. We can store int, string, float elements, depending on how we initialize the vector. We?ll play with integer numbers first.

You did it! Pretty simple!

Now we can store some random integer numbers. The magic method is ?push_back?.

Now we have 5 numbers stored in our vector:

Image for post

If we use pop_back(), we?ll remove the last element.

To remove the first element, we can use erase(). We need to pass the element?s position (iterator position), we want to remove, as an argument.

We can also remove the last element using erase.

Do you want the first element? Use front.

Do you want the last element? Use back.

Do you want to know the number of elements inside the vector? Use size.

Instead of using

use the empty method (see interesting reasons).

As a simple array, we can use the and the = operator.

Let?s remove all elements from this vector.

We can use algorithm’s sort to order the vector elements in an ascending order.

And in a descending order, using the greater<int> comparison as the third argument.

Compare comp = “Comparison function object which returns ?true if the first argument is less than the second.”

Imagine that you don’t want to write

“sort(v.begin(), v.end(), greater<int>());”

We can put this code into a void function, passing the vector as an argument. So now the question is: “How can we pass the vector as an argument?”. And we have two ways:

1. As a reference:

2. As a pointer:

That’s it!

For more stories about programming, c++ features and my path to master competitive programming, you should follow my publication Real Algorithms and my Algorithms repository on github.

If you like it, try learning more about C++ strings.

That it guys! You all should play a bit with those vector features and have FUN! 🙂 Keep learning!

I hope you liked this content. Support my work on Ko-Fi

My Twitter & Github. ?

Resources

vector – C++ Reference

template > class vector; // generic template Vectors are sequence containers representing arrays that can change in?

www.cplusplus.com

Power up C++ with the Standard Template Library: Part 1 – topcoder

By DmitryKorolev – TopCoder Member Discuss this article in the forums Perhaps you are already using C++ as your main?

www.topcoder.com

C++ Tutorial: A Beginner’s Guide to std::vector, Part 1

Environment: VC6 SP5, STLPort, Windows 2000 SP2 This C++ tutorial is meant to help beginning and intermediate C+?

www.codeguru.com

C++ STL Tutorial

Hope you already understand the concept of C++ Template which we already have discussed in one of the chapters. The C+?

www.tutorialspoint.com

23