|
ex5-10.cpp - Example 5-10 STL Linked List example |
// File: ex5-10.cpp - STL linked list example
#include <iostream>
#include <list>
#include <algorithm> // for copy and find
#include <iterator> // for ostream_iterator
using namespace std;
void print(list<int>& lint)
{
copy(lint.begin(),lint.end(),ostream_iterator<int>(cout," "));
cout << endl;
}
bool find(list<int>& lint, int value)
{
return find(lint.begin(),lint.end(),value)!=lint.end();
}
int main (void)
{
list<int> L;
L.push_front(2);
L.push_front(4);
L.push_front(6);
L.push_front(8);
L.push_front(10);
print(L);
cout << "top=" << *L.begin() << endl;
if (find(L,2)) cout << 2 << " is in the list\n";
if (find(L,5)) cout << 5 << " is in the list\n";
if (find(L,6)) cout << 6 << " is in the list\n";
if (find(L,10)) cout << 10 << " is in the list\n";
L.remove(3);
L.remove(6);
print(L);
L.remove(2);
L.remove(10);
print(L);
return 0;
}
CIS27: Programming in C++ Instructor: Joe Bentley