Vectors in C++
Programming
|
The C++ STL library defines a vector class which gives you a pre-made generic vector. You can use it in the same way as you would a regular array, only a vector is safer to use and more sophisticated.
Creating one is simple:
#include <vector>
using namespace std;
vector<int> myVector;
You have now created a vector to hold integers. One of the best things about vectors is that memory for them is dynamically allocated. This is perfect for occasions when you do not know the exact number of elements you may need at compile time. Here's an example:
#include <vector>
using namespace std;
vector<int> myVector;
myVector.push_back(42);
myVector.push_back(34);
Note that I never explicitly tell the compiler how large my vector is. At this point in time, it contains 2 elements, but I can easily add another element to the back of the vector. If I need to get the number of elements in the vector, I can simply use the size() member of the vector object.
There are several ways to retrieve values from the vector, including the standard iterator approach supported for all STL classes. The most common way to accomplish this task is subscripting, which is identical to subscripting arrays:
for(int i(0); i < myVector.size(); ++i)
{
std::cout<< myVector[i] <<endl;
}
As you can see getting values from the vector is simple. Clearing the vector is a simple one step process of calling the .clear() member.
Furthermore, vectors allow you to easily insert elements in the middle of them using simple iterator manipulations and the insert() member.
At the end of the day, vectors provide the programmer a sophisticated container. Prefer using vectors to arrays when you have the option.
Although vectors are great, I should mention that they are slower and larger than regular arrays. Most of the time, it is well worth the trade off, but always use discretion when using them. Sometimes arrays are suffice and the situation does not benefit from using vectors. In these cases, you should use arrays, but there are a lot more situations where vectors can make a programmers job easier and safer.
This is only a small overview of how vectors work and by no means comprehensive. If you plan on using them, make sure you look up a few more details about them. They really aren't hard to learn, and it will really help your C++ programming experience.Please login to rate coding articles.
Click here to register a free account with us. |
|
Comments
|
Please login to post comments. |
|
Wow,
I have been looking for something great concerning Vectors in C++ and now I've found it. I found it completely by accident, by searching through your profile, but I found it. Thank you so much for posting this ... this is definitely going to come in handy!
|
|
Thankyou i know understand vector and it has made the arrays in my wml parser very powerfull compared to arrays cheers Scott :),
Rob
|
|
I could have done with reading this like a year ago! When i was trying to find out what the substitute for dynamic arrays was in C++ :-P
All in all, a nice article :-)
|
|
|
 |
Oleksi Derkatch (18) Canada, Ontario |
|
Cinjection has 14 fans
become a fan |
|
 |
|
 |
|