4 |
|
// |
5 |
|
// $Id$ |
6 |
|
|
7 |
– |
#include <iostream> |
7 |
|
#include <string> |
8 |
+ |
#include <sstream> |
9 |
+ |
#include <cassert> |
10 |
|
#include "DoublyLinkedList.hpp" |
11 |
|
|
12 |
|
using namespace std; |
13 |
|
|
14 |
|
template<typename Type> |
15 |
< |
bool contains(const Type& value, DoublyLinkedList<Type>& list) |
15 |
> |
inline bool contains(const Type& value, DoublyLinkedList<Type>& list) |
16 |
|
{ |
17 |
|
for (Iterator<Type> itor(list.iterator()); !itor.end(); itor++) |
18 |
|
{ |
24 |
|
|
25 |
|
class VowelVisitor : public Visitor<string> |
26 |
|
{ |
27 |
+ |
private: |
28 |
+ |
ostream* os; |
29 |
|
public: |
30 |
< |
void visit(const string& value) { if (value.find_first_of("aAeEiIoOuU") == 0) cout << value << '\n'; } |
30 |
> |
VowelVisitor(ostream& os) { this->os = &os; } |
31 |
> |
void visit(const string& value) { if (value.find_first_of("aAeEiIoOuU") == 0) *os << value << '\n'; } |
32 |
|
}; |
33 |
|
|
34 |
|
int main(int argc, char* argv[]) |
35 |
|
{ |
32 |
– |
cout.setf(ios_base::boolalpha); |
33 |
– |
|
36 |
|
DoublyLinkedList<int> list; |
37 |
|
|
38 |
|
list.addFront(1); |
42 |
|
|
43 |
|
// 4 1 2 5 |
44 |
|
|
45 |
< |
cout << "list.contains(1) = " << list.contains(1) << '\n' |
46 |
< |
<< "list.contains(2) = " << list.contains(2) << '\n' |
47 |
< |
<< "list.contains(3) = " << list.contains(3) << '\n' |
48 |
< |
<< "list.contains(4) = " << list.contains(4) << '\n' |
49 |
< |
<< "list.contains(5) = " << list.contains(5) << '\n'; |
45 |
> |
assert(list.contains(1) == true); |
46 |
> |
assert(list.contains(2) == true); |
47 |
> |
assert(list.contains(3) == false); |
48 |
> |
assert(list.contains(4) == true); |
49 |
> |
assert(list.contains(5) == true); |
50 |
|
|
51 |
|
list.removeFirst(2); |
52 |
|
|
53 |
|
// 4 1 5 |
54 |
|
|
55 |
< |
cout << "list.contains(2) = " << list.contains(2) << '\n' |
56 |
< |
<< "list.contains(5) = " << list.contains(5) << '\n'; |
55 |
> |
assert(list.contains(2) == false); |
56 |
> |
assert(list.contains(5) == true); |
57 |
> |
|
58 |
> |
ostringstream sout; |
59 |
|
|
60 |
|
for (Iterator<int> itor(list.iterator()); !itor.end(); itor++) |
61 |
|
{ |
62 |
< |
cout << *itor << '\n'; |
62 |
> |
sout << *itor << '\n'; |
63 |
|
} |
64 |
|
|
65 |
< |
cout << "contains(3, list) = " << contains(3, list) << '\n' |
66 |
< |
<< "contains(4, list) = " << contains(4, list) << '\n'; |
65 |
> |
assert(sout.str() == "4\n1\n5\n"); |
66 |
> |
sout.str(""); |
67 |
> |
assert(contains(3, list) == false); |
68 |
> |
assert(contains(4, list) == true); |
69 |
|
|
70 |
|
DoublyLinkedList<string> words; |
71 |
|
|
77 |
|
|
78 |
|
for (Iterator<string> itor(words.iterator()); !itor.end(); itor++) |
79 |
|
{ |
80 |
< |
if (itor->find_first_of("aAeEiIoOuU") == 0) cout << *itor << '\n'; |
80 |
> |
if (itor->find_first_of("aAeEiIoOuU") == 0) sout << *itor << '\n'; |
81 |
|
} |
82 |
|
|
83 |
< |
VowelVisitor vowels; |
83 |
> |
assert(sout.str() == "Edward\nAllen\nattack\n"); |
84 |
> |
sout.str(""); |
85 |
> |
|
86 |
> |
VowelVisitor vowels(sout); |
87 |
|
|
88 |
|
words.accept(dynamic_cast<Visitor<string>*>(&vowels)); |
89 |
|
|
90 |
+ |
assert(sout.str() == "Edward\nAllen\nattack\n"); |
91 |
+ |
|
92 |
|
return 0; |
93 |
|
} |