#include <algorithm>
#include <string>
#include <vector>
#include <iostream>
using namespace std;
template <class T>
void print(T& c){
for( typename T::iterator i = c.begin(); i != c.end(); i++ ){
std::cout << *i << endl;
}
}
int main( )
{
const char* name_array[] = { "John", "Harry", "Mark", "Abe" };
const int age_array[] = { 89, 34, 12, 20 };
const int people = sizeof( age_array ) / sizeof( age_array[0] );
// make vectors with names and ages
vector<string> names( name_array, name_array+people );
vector<int> ages( age_array, age_array+people );
// find the youngest age
vector<int>::iterator age_itr = min_element( ages.begin(), ages.end() );
// convert from iterator to index
cout << names[age_itr-ages.begin()]
<< ", the youngest person, is " << *age_itr << " years old\n";
// convert from index to iterator
print( names);
names.erase( names.begin()+1 ); // erase the second element
print( names);
}
|