/* Faan Tone Liu Demo program for comp 1672 January 24, 2003 Demonstrates how to use vectors. vector is a class from the standard template library a vector is a "smart array". It grows as you require more memory. You don't need to worry about dynamic memory allocation, because the vector class handles that for you. This means that if the size of a list until you run the program, (such as the number of people listed in a file, for example) you should use a vector */ #include #include int main() { vector numbers; // declare a vector of floats called numbers vector ::iterator vecit; // iterator used to traverse the vector int i; /* Here's how to store new numbers at the end of the vector: */ numbers.push_back(5); numbers.push_back(4); numbers.push_back(3); numbers.push_back(2); numbers.push_back(1); /* Here's how to see how many elements are currently in the vector: */ cout << "The size of the vector is " << numbers.size() << endl; /* Here's how to get rid of an element at the end of a vector: */ numbers.pop_back(); cout << "The new size of the vector is " << numbers.size() << endl; /* Here's one method to traverse the vector */ cout << "Here are the elements in the vector: "; for (i=0; i < numbers.size(); i++) cout << numbers[i] << " "; cout << endl; /* Here's a better (more efficient) way to traverse the vector */ cout << "Here are the elements in the vector: "; for (vecit=numbers.begin(); vecit != numbers.end(); vecit++) cout << *vecit << " "; cout << endl; /* Here's how to get rid of everything in the vector */ numbers.clear(); /* See, it's empty now! */ cout << "Here are the elements in the vector: "; for (vecit=numbers.begin(); vecit != numbers.end(); vecit++) cout << *vecit << " "; cout << endl; return 0; }