article

Introduction to C++ STL vector

Email
Submitted on: 1/4/2015 12:53:00 AM
By: S Halayka (from psc cd)  
Level: Beginner
User Rating: By 3 Users
Compatibility: C++ (general), Microsoft Visual C++, Borland C++, UNIX C++
Views: 768
 
     Introduction to C++ STL vector - a C++ standard class that acts similarily to a C array. No 3rd party classes, this is all standard among every standards compliant C++ compiler! The major benefit to the vector is that it is dynamic in size. You can add to it, remove from it, and not have to worry about handling the memory yourself. Very useful.

 
				

// on linux, compile with g++, or use the -lstdc++ switch on gcc
#include  // for cout
#include  // for vector
using namespace std;
int main(void)
{
 // a vector is very similar to a standard C array
 // one key difference is that a vector has dynamic size
 // this means that you can add and delete items from it in a very simple fashion
 // as you work with the vector, it handles all of the necessary memory management for you
 // this allows you to focus on the more important parts of your programming task
 //
 //
 // here are some of the various useful member functions of a vector:
 //
 // push_back() - adds an item to the end of the vector
 // insert() - adds an item (or range of items) at a specified spot in the vector
 // pop_back() - removes the item at the end of the vector
 // erase() - removes the item (or range of items) at a specified spot in the vector
 // size() - tells how many items are currently stored in the vector
 // clear() - clears all items currently stored in the vector
 // operator [] - the familiar brackets, used in the same manner as with standard C arrays, to access items
 // create a vector container object - it will store ints in this example
 // however, a vector can store almost any type of object
 // from int and char, to string and custom classes, even other vectors (for a multi-dimensional vector)!
 vector<int> IntVector;
 // add an integer (value 24) to the end of the vector
 IntVector.push_back(24);
 // print out the current number of ints contained inside our vector
 cout << "After push_backing one item, the vector's size is: " << IntVector.size() << endl;
 // remove the element at the end of the vector
 IntVector.pop_back();
 // print out the current number of ints contained inside our vector
 cout << "After pop_backing one item, the vector's size is: " << IntVector.size() << endl;
 cout << endl;
 // a variable we will use with our for loops
 unsigned short counter = 0;
 // let's add 10 items to the vector, inserting each at the beginning of the vector, instead of the end
 // the first item we will add is 0, the final item we will add is 9
 for(counter = 0 ; counter < 10 ; counter++)
 {
// the first parameter, IntVector.begin(), indicates to add the new item at the very beginning of the vector
// the second parameter, counter, indicates the variable (value) we wish to add to the vector
IntVector.insert( IntVector.begin(), counter);
 }
 // print out the current number of ints contained inside our vector
 cout << "After inserting 10 items, the vector's size is: " << IntVector.size() << endl;
 // print out the vector's current contents, item by item
 // we will start with item 0 (like a standard C array)
 // we will want to stop at item 9 (like a standard C array)
 // notice the output starts at 9 and decreases from there
 // this is because we added the items in our previous loop to the beginning of the vector, and not to the end
 for(counter = 0 ; counter < IntVector.size() ; counter++)
 {
// we use IntVector[counter] to access an item's value, just like a standard C array
cout << "Item " << counter << " contains: " << IntVector[counter] << endl;
 }
 cout << endl;
 // let's erase item 6
 // we only pass one parameter to erase, since we are deleting only one item
 // this parameter indicates which item to delete
 IntVector.erase( IntVector.begin() + 6 );
 // print out the current number of ints contained inside our vector
 cout << "After erasing one item, the vector's size is: " << IntVector.size() << endl;
 // let's erase items 0-3. note: that is 4 items to be deleted
 // we pass two parameters this time, the first indicates the item position to begin erasing at
 // the second parameter indicates the item position to stop deleting at (this item does not get erased)
 IntVector.erase( IntVector.begin(), IntVector.begin() + 4 );
 // print out the current number of ints contained inside our vector
 cout << "After erasing four more items, the vector's size is: " << IntVector.size() << endl;
 // print out the current vector contents
 for(counter = 0 ; counter < IntVector.size() ; counter++)
 {
// we use IntVector[counter] to access an item's value, just like a standard C array
cout << "Item " << counter << " contains: " << IntVector[counter] << endl;
 }
 // clear the vector
 // note: it is not necessary to clear the vector when you are finished using it
 // all memory deallocation is handled automatically
 // 
 // it is only necessary when you want to erase everything inside of it and start out fresh
 IntVector.clear();
 // print out the current number of ints contained inside our vector
 cout << "After clearing the vector, the vector's size is: " << IntVector.size() << endl;
 return 1;
}


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.