nearest.cc
author Markus Bröker <mbroeker@largo.dyndns.tv>
Mon, 08 Mar 2010 18:30:04 +0100
changeset 121 fef2ccfa7b12
parent 89 66f0244c2863
permissions -rw-r--r--
step 2) Header seperation cpuid, bits, list and maybe lsf are too common names and have to reside in a seperate folder to avoid clashes committer: Markus Bröker <mbroeker@largo.homelinux.org>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
88
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     1
/**
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     2
 * I am experimenting with templates...
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     3
 *
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     4
 */
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     5
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     6
#include <iostream>
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     7
#include <list>
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     8
#include <vector>
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     9
#include <algorithm>
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    10
#include <stdexcept>
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    11
#include <iomanip>
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    12
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    13
typedef short MYTYPE;
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    14
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    15
namespace cute {
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    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
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    18
    {
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    19
        Iter it;
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    20
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    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
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    23
                return it;
89
66f0244c2863 nearest: more templates fun
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 88
diff changeset
    24
        } return it;
88
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    25
    }
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    26
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    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
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    29
    {
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    30
        Iter it, current;
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    31
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    32
        current = it = cute::find (begin, end, query);
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    33
        if (current == end)
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    34
            return;
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    35
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    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
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    38
            current++;
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    39
        }
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    40
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    41
        current = --it;
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    42
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    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
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    45
        } while ((current--) != begin);
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    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
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    62
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    63
int main (int argc, char **argv)
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    64
{
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    65
    MYTYPE value, query;
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    66
    vector <MYTYPE> vec;
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    67
    list <MYTYPE> lis;
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    68
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    69
    for (int i = 0; i < 10; i++) {
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    70
        cout << "[" << std::setw (2) << i << "] Enter a value: ";
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    71
        cin >> value;
89
66f0244c2863 nearest: more templates fun
Markus Bröker <mbroeker@largo.dyndns.tv>
parents: 88
diff changeset
    72
        if (cin) {
88
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    73
            vec.push_back (value);
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    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
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    77
    }
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    78
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    79
    sort (vec.begin (), vec.end ());
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    80
    lis.sort ();
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    81
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    82
    cout << "Enter a value to search: ";
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    83
    cin >> query;
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    84
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    85
    cute::nearest (vec.begin (), vec.end (), query);
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    86
    cute::nearest (lis.begin (), lis.end (), query);
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    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
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    90
    return 0;
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    91
}