Coder Profile - Show off your skills, get a coder profile.
 
 
 
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.
 
FallingTears     Posted 14 Days Ago
 
 
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!
 
wap2k     Posted 173 Days Ago
 
 
Thankyou i know understand vector and it has made the arrays in my wml parser very
powerfull compared to arrays cheers Scott :),

Rob
 
Cinjection     Posted 318 Days Ago
 
 
Thanks, Scott.
 
VBAssassin     Posted 318 Days Ago
 
 
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 :-)
Page 1 of 1
More Articles By This Author
Intorduction to memoization.
Strings in C++
Vectors in C++
Use Cases
[C++] Pointers and their practical uses in programming.
The Differences and Similarities Between C and C++
[C++] A tour of OOP
[C++] Templates
Oleksi Derkatch (18)
Canada, Ontario
Cinjection has 14 fans
become a fan
� Applications
Articles Articles (8)
Source Codes Source Codes (59)
Code Pin Board Code Pin Board (13)
� About Me
About Me About Me
User Groups User Groups (11)
Portfolio Portfolio (7)
Friends Friends (91)
� Misc
Overview Overview
Send A Friend Invite
Send Message Send A Message
RSS Whole Profile Feed
 
 
Latest News About Coder Profile
Coder Profile Poll
What is the next application you would like to be added to your profiles?

Books
Coding Challenges
Project Management
Blog
Tutorials


please login to cast your vote
and see the results of this poll
Latest Coder Profile Changes
Coder Profile was last updated
25 Days Ago
Official Blog :: Make A Donation :: Credits :: Contact Me
Terms & Conditions :: Privacy Policy :: Documents :: Wallpapers
Version 1.46.00
Copyright � 2007 - 2008, Scott Thompson, All Rights Reserved