author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Fri, 05 Mar 2010 22:03:56 +0100 | |
changeset 114 | 6f6850407ccf |
parent 89 | 66f0244c2863 |
permissions | -rw-r--r-- |
88 | 1 |
/** |
2 |
* I am experimenting with templates... |
|
3 |
* |
|
4 |
*/ |
|
5 |
||
6 |
#include <iostream> |
|
7 |
#include <list> |
|
8 |
#include <vector> |
|
9 |
#include <algorithm> |
|
10 |
#include <stdexcept> |
|
11 |
#include <iomanip> |
|
12 |
||
13 |
typedef short MYTYPE; |
|
14 |
||
15 |
namespace cute { |
|
16 |
template <typename Iter, typename T> |
|
89
66f0244c2863
nearest: more templates fun
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
88
diff
changeset
|
17 |
Iter find (const Iter begin, const Iter end, const T& query) |
88 | 18 |
{ |
19 |
Iter it; |
|
20 |
||
21 |
for (it = begin; it != end; it++) { |
|
89
66f0244c2863
nearest: more templates fun
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
88
diff
changeset
|
22 |
if (query == *it) |
88 | 23 |
return it; |
89
66f0244c2863
nearest: more templates fun
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
88
diff
changeset
|
24 |
} return it; |
88 | 25 |
} |
26 |
||
27 |
template <typename Iter, typename T> |
|
89
66f0244c2863
nearest: more templates fun
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
88
diff
changeset
|
28 |
void nearest (const Iter begin, const Iter end, const T& query) |
88 | 29 |
{ |
30 |
Iter it, current; |
|
31 |
||
32 |
current = it = cute::find (begin, end, query); |
|
33 |
if (current == end) |
|
34 |
return; |
|
35 |
||
36 |
while (current != end) { |
|
89
66f0244c2863
nearest: more templates fun
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
88
diff
changeset
|
37 |
std::cout << *current << std::endl; |
88 | 38 |
current++; |
39 |
} |
|
40 |
||
41 |
current = --it; |
|
42 |
||
43 |
do { |
|
89
66f0244c2863
nearest: more templates fun
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
88
diff
changeset
|
44 |
std::cout << *current << std::endl; |
88 | 45 |
} while ((current--) != begin); |
46 |
} |
|
89
66f0244c2863
nearest: more templates fun
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
88
diff
changeset
|
47 |
|
66f0244c2863
nearest: more templates fun
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
88
diff
changeset
|
48 |
template <class C, class T> |
66f0244c2863
nearest: more templates fun
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
88
diff
changeset
|
49 |
typename C::iterator nearest (C& con, const T& query) |
66f0244c2863
nearest: more templates fun
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
88
diff
changeset
|
50 |
{ |
66f0244c2863
nearest: more templates fun
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
88
diff
changeset
|
51 |
typename C::iterator it; |
66f0244c2863
nearest: more templates fun
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
88
diff
changeset
|
52 |
|
66f0244c2863
nearest: more templates fun
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
88
diff
changeset
|
53 |
for (it = con.begin (); it != con.end (); it++) { |
66f0244c2863
nearest: more templates fun
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
88
diff
changeset
|
54 |
if (query == *it) |
66f0244c2863
nearest: more templates fun
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
88
diff
changeset
|
55 |
return it; |
66f0244c2863
nearest: more templates fun
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
88
diff
changeset
|
56 |
} |
66f0244c2863
nearest: more templates fun
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
88
diff
changeset
|
57 |
return it; |
66f0244c2863
nearest: more templates fun
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
88
diff
changeset
|
58 |
} |
66f0244c2863
nearest: more templates fun
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
88
diff
changeset
|
59 |
}; |
66f0244c2863
nearest: more templates fun
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
88
diff
changeset
|
60 |
|
66f0244c2863
nearest: more templates fun
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
88
diff
changeset
|
61 |
using namespace std; |
88 | 62 |
|
63 |
int main (int argc, char **argv) |
|
64 |
{ |
|
65 |
MYTYPE value, query; |
|
66 |
vector <MYTYPE> vec; |
|
67 |
list <MYTYPE> lis; |
|
68 |
||
69 |
for (int i = 0; i < 10; i++) { |
|
70 |
cout << "[" << std::setw (2) << i << "] Enter a value: "; |
|
71 |
cin >> value; |
|
89
66f0244c2863
nearest: more templates fun
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
88
diff
changeset
|
72 |
if (cin) { |
88 | 73 |
vec.push_back (value); |
74 |
lis.push_back (value); |
|
89
66f0244c2863
nearest: more templates fun
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
88
diff
changeset
|
75 |
} else |
66f0244c2863
nearest: more templates fun
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
88
diff
changeset
|
76 |
throw runtime_error ("BAD INPUT"); |
88 | 77 |
} |
78 |
||
79 |
sort (vec.begin (), vec.end ()); |
|
80 |
lis.sort (); |
|
81 |
||
82 |
cout << "Enter a value to search: "; |
|
83 |
cin >> query; |
|
84 |
||
85 |
cute::nearest (vec.begin (), vec.end (), query); |
|
86 |
cute::nearest (lis.begin (), lis.end (), query); |
|
87 |
||
89
66f0244c2863
nearest: more templates fun
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
88
diff
changeset
|
88 |
cute::nearest (lis, query); |
66f0244c2863
nearest: more templates fun
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
88
diff
changeset
|
89 |
|
88 | 90 |
return 0; |
91 |
} |