|
|
| STL Tutorial and Reference Guide: C++ Programming with the Standard Template Lib (ISBN: 0201379236) |
 |
List Price: $44.95
Our Price: $44.95
Used Price: $22.04
Release Date: 27 March, 2001
Manufacturer: Addison-Wesley Pub Co (Hardcover)
Sales Rank: 93,208
Author: David R. Musser, Gillmer J. Derge, Atul Saini, Gilmer J. Derge
|
More Info
|
|
|
STL Vector
|
2003-05-28 14:16:06
|
| |
cpp
|
|
|
|
|
Category: source:cpp:stl
|
|
Description: Basic example on inserting, finding, and retrieving elements of a stl vector.
|
|
Platform: all
|
|
Author: detour
|
|
Viewed: 9340
|
|
Rating: 4/5 (29 votes)
|
|
|
| If you have any questions about
this piece of code or still need help, try posting your question on the forum. |
/* vector.cpp by detour@metalshell.com
*
* Basic example on inserting, finding, and retrieving
* elements of a stl vector.
*
* http://www.metalshell.com/
*
*/
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main() {
// Create the vector of type string
vector<string> myvect;
myvect.push_back("Golden");
myvect.push_back("Age");
myvect.push_back("Illuminati");
cout << "Print using iterator.\n";
// Print out all values using an iterator
vector<string>::iterator i;
for(i = myvect.begin(); i < myvect.end(); i++)
cout << (*i) << endl;;
cout << "\nPrint using index.\n";
// Print out all values using an index
unsigned int index;
for(index = 0; index < myvect.size(); index++)
cout << myvect[index] << endl;
cout << "\nSearch for a value.\n";
// Find a value and print it
i = find(myvect.begin(), myvect.end(), "Illuminati");
if(i != myvect.end())
cout << "Found: " << (*i) << endl;
// erase everything in myvect.
myvect.clear();
// Nothing will be printed.
for(i = myvect.begin(); i < myvect.end(); i++)
cout << (*i) << endl;;
return 0;
}
|
|
|