topical media & game development
professional-program-23-IteratorAdapters-ReverseIterators.c
? /
professional-program-23-IteratorAdapters-ReverseIterators.c
include <algorithm>
include <vector>
include <iostream>
include <iterator>
using namespace std;
// Function template to populate a container of ints.
// The container must support push_back().
template<typename Container>
void populateContainer(Container& cont)
{
int num;
while (true) {
cout << "Enter a number (0 to quit): ";
cin >> num;
if (num == 0) {
break;
}
cont.push_back(num);
}
}
int main(int argc, char** argv)
{
vector<int> myVector;
populateContainer(myVector);
int num;
cout << "Enter a number to find: ";
cin >> num;
vector<int>::iterator it1;
vector<int>::reverse_iterator it2;
it1 = find(myVector.begin(), myVector.end(), num);
it2 = find(myVector.rbegin(), myVector.rend(), num);
if (it1 != myVector.end()) {
cout << "Found " << num << " at position " << it1 - myVector.begin()
<< " going forward.\n";
cout << "Found " << num << " at position "
<< it2.base() - 1 - myVector.begin() << " going backward.\n";
} else {
cout << "Failed to find " << num << endl;
}
return (0);
}
(C) Æliens
20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.