Vectors are a powerful STL container format that closely resembles an array, however is much more useable and intuitive. Vectors allow you to insert, remove, pop, push, change, and resize the array, making it very powerful indeed. It's what C standard arrays would be if they weren't terrible.
So how do you use a Vector? Easy, just do this:
That simple code includes the vector header file, and declares a std::vector for use in your program. The "<int>" part of that vector is because a vector is a class template, which means it can be used to produce an array of any class object you want, from simple int's, pointers, std::strings or even other vectors! Let's start using a vector, and see what we can make!Code:#include <vector> int main(){ std::vector<int> myVector; return 0; }
(You should be familiar with atoi and accepting command line statements) This code, when run, accepts a command line argument. Go ahead and build it, then, in the command line, enter "{PROGNAME} 25" and it will print out a string of numbers counting from one to 25.Code:#include <vector> #include <iterator> #include <iostream> #include <cstdlib> int main(int argc, char *argv[]) { using namespace std; int aval; if (argc == 2) { aval = atoi(argv[1]); } else { cout << "You must input one argument and only one argument!" << endl; return 0; } // Sanity check that input variable... if (aval < 1) { cout << "Please input a number greater than or equal to 1 to " << "continue." << endl; return 0; } // Now let's create and initialize a vector. vector<int> myVector; for (int iii = 0; iii < aval; ++iii) myVector.push_back(iii + 1); // Now let's print out the value of that vector! for (int i = 0; (unsigned int)i < myVector.size(); ++i) cout << myVector[i] << " "; cout << endl; cout << std::endl; return 0; }
You may notice that there is no dynamic memory allocation necessary in this program. That is because the vector class template already has that functionality built-in to it, so there's no need to resize it ourselves, we just call "push_back" and vector will do that for us. push_back is a function that adds the value given to it as an argument to the end (or back) of the std::vector. There's a lot more functionality with vectors, so let's try another loop, this time removing each even number from the vector before printing it.
What we did here was we simply added an additional for loop that initialized a vector iterator, which counted through the vector, and if the value with which the iterator was pointing to, if divided by 2, had a remainder of 0 (IE it was even), then that vector erased the number at the location of myIter.Code:// Now let's create and initialize a vector. vector<int> myVector; for (int iii = 0; iii < aval; ++iii) myVector.push_back(iii + 1); // Remove even numbers, if any. for (vector<int>::iterator myIter = myVector.begin(); myIter < myVector.end(); ++myIter) { if (*myIter % 2 == 0) myVector.erase(myIter); } // Now let's print out the value of that vector! for (int i = 0; (unsigned int)i < myVector.size(); ++i) cout << myVector[i] << " ";
That's where the interesting part is, std::vectors also have the ability to erase certain values from within them, then dynamically resize themselves as if the erased value wasn't there at all (this would be very difficult with C arrays). The way this is done is the erase function is provided an iterator that points to the value in the vector array you want to erase, then the function deletes it and moves all of the values ahead of it back one. Go ahead and build that and try it!
This is just part one on a chapter called "Vectors" for a series On Learning the STL. In the second part we'll take a closer look at iterators and just what they are, as well as how useful they can be, since as you can see, they're going to be used a lot! The attachment contains a copy of the complete code you can build from.
- Zeke


LinkBack URL
About LinkBacks







Reply With Quote









Bookmarks
Algorithms and Data Structures
Java tutorials
Algorithms Forum