前田稔(Maeda Minoru)の超初心者のプログラム入門
#include <stdio.h> #include <list> using namespace std; typedef struct { int key; char name[20]; } BEST; BEST tbl[8]= { { 5, "5abc" }, { 3, "3def" }, { 2, "2ghi" }, { 7, "7jkl" }, { 4, "4mnop" }, { 3, "3xyz" }, { 8, "8uvw" }, { 1, "1qrst" }, }; |
void main() { list<BEST> v; list<BEST>::iterator it; int i; for(i=0; i<8; i++) { it= algo_srh(v.begin(),v.end(),GreaterEqual(tbl[i].key)); v.insert(it, tbl[i]); } for(it=v.begin(); it!=v.end(); it++) printf("%d %s\n", it->key,it->name); } |
template <class T, class OP> T algo_srh(T first, T last, OP op) { for(; first != last; first++) if (op(first)) return first; return last; } |
class GreaterEqual { int bench_; public: GreaterEqual(int bench) : bench_(bench){} bool operator()(const list |