ネタ元 → LINQ with VisualBasic.NET ~ペンギンをさがそう~
C++/TR1 だとこんなんなります。
#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>
#include <boost/tr1/functional.hpp>
#include <boost/tr1/tuple.hpp>
using namespace std;
typedef tr1::tuple<string,int,string,string> penguin_type;
namespace std {
// ストリームにペンギンを流し込む (ペンギン涙目)
std::ostream& operator<<(std::ostream& stream,
const penguin_type& penguin) {
return stream << tr1::get<0>(penguin) << ':'
<< tr1::get<1>(penguin) << ':'
<< tr1::get<2>(penguin) << ':'
<< tr1::get<3>(penguin);
}
}
// 二つのtuple x, y に対し、xのN番目の要素 < yのN番目の要素 ならtrue
template<int N, typename Tuple>
struct tuple_less : binary_function<Tuple,Tuple,bool> {
bool operator()(const Tuple& x, const Tuple& y) const {
return tr1::get<N>(x) < tr1::get<N>(y);
}
};
// 二つのtuple x, y に対し、xのN番目の要素 != yのN番目の要素 ならtrue
template<int N, typename Tuple>
struct tuple_not_equal : binary_function<Tuple,Tuple,bool> {
bool operator()(const Tuple& x, const Tuple& y) const {
return tr1::get<N>(x) != tr1::get<N>(y);
}
};
int main() {
const int N = 4;
penguin_type penguins[N] = {
penguin_type("フンボルトペンギン", 60, "南アメリカ", "岩場"),
penguin_type("コビトペンギン", 38, "オーストラリア", "巣穴"),
penguin_type("イワトビペンギン", 61, "フォークランド諸島", "岩場"),
penguin_type("コウテイペンギン", 120, "南極", "氷上"),
};
cout << "\n全部のペンギン" << endl;
copy(penguins, penguins+N, ostream_iterator<penguin_type>(cout, "\n"));
cout << "\n最小のペンギン" << endl;
cout << *min_element(penguins, penguins+N,
tuple_less<1,penguin_type>()) << endl;
cout << "\n最大のペンギン" << endl;
cout << *max_element(penguins, penguins+N,
tuple_less<1,penguin_type>()) << endl;
cout << "\n岩場に生息するペンギン" << endl;
remove_copy_if(penguins, penguins+N,
ostream_iterator<penguin_type>(cout,"\n"),
bind1st(tuple_not_equal<3,penguin_type>(),
penguin_type("",0,"","岩場")));
}
お休みでもこのくらいのコードは毎日書いてないと休み明けのリハビリが辛いんす。