nearest.cc
author Markus Bröker <mbroeker@largo.dyndns.tv>
Thu, 30 Apr 2009 23:38:48 +0200
changeset 88 91fbc3ea240b
child 89 66f0244c2863
permissions -rw-r--r--
how to use templates We read 10 numbers from stdin and print the nearest values of a query. 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
using namespace std;
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
typedef short MYTYPE;
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    16
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    17
namespace cute {
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    18
    template <typename Iter, typename T>
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    19
    Iter find (Iter begin, Iter end, const T& query)
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
        Iter it;
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    22
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    23
        for (it = begin; it != end; it++) {
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    24
            if (*it == query)
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    25
                return it;
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
        return it;
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    28
    }
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
    template <typename Iter, typename T>
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    31
    void nearest (Iter begin, Iter end, const T& query)
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    32
    {
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    33
        Iter it, current;
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    34
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    35
        current = it = cute::find (begin, end, query);
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    36
        if (current == end)
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    37
            return;
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    38
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    39
        while (current != end) {
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    40
            cout << *current << endl;
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    41
            current++;
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
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    44
        current = --it;
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    45
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    46
        do {
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    47
            cout << *current << endl;
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    48
        } while ((current--) != begin);
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    49
    }
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    50
}
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    51
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    52
int main (int argc, char **argv)
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    53
{
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    54
    MYTYPE value, query;
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    55
    vector <MYTYPE> vec;
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    56
    list <MYTYPE> lis;
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    57
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    58
    for (int i = 0; i < 10; i++) {
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    59
        cout << "[" << std::setw (2) << i << "] Enter a value: ";
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    60
        cin >> value;
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    61
        if (!cin)
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    62
            throw std::runtime_error ("BAD_INPUT");
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    63
        else {
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    64
            vec.push_back (value);
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    65
            lis.push_back (value);
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    66
        }
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    67
    }
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
    sort (vec.begin (), vec.end ());
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    70
    lis.sort ();
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    71
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    72
    cout << "Enter a value to search: ";
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    73
    cin >> query;
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    74
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    75
    cute::nearest (vec.begin (), vec.end (), query);
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    76
    cute::nearest (lis.begin (), lis.end (), query);
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
    return 0;
91fbc3ea240b how to use templates
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    79
}