G
#include <algorithm>
#include <functional>
#inlcude <iterator>
#include <iostream>
#include <string>
using namespace std;
#define for if(0);else for
const char newl = '\n';
struct Buch
{
Buch(const string & titel_, const string & autor_, const string & isbn_)
:titel(titel_), autor(autor_), isbn(isbn_)
{}
string titel;
string autor;
string isbn;
};
ostream & operator<< (ostream & os, const Buch & buch)
{
const char newl = '\n';
const char tab = '\t';
os << "<Buch>" << newl;
os << tab << "<titel>" << buch.titel << "</titel>" << newl;
os << tab << "<autor>" << buch.autor << "</autor>" << newl;
os << tab << "<isbn>" << buch.isbn << "</isbn>" << newl;
os << "</Buch>" << newl;
return os;
}
namespace PODTools
{
template
<
typename TofStruct,
typename TofMember,
typename Predicate
>
class Member_t : public binary_function<TofStruct, TofStruct, bool>
{
public:
typedef TofMember (TofStruct::*MemberPtr);
Member_t(MemberPtr pMember, Predicate pred)
: m_pMember(pMember),
m_pred(pred)
{
}
bool operator() (const TofStruct & lhs, const TofStruct & rhs) const
{
return m_pred( lhs.*m_pMember, rhs.*m_pMember );
}
private:
MemberPtr m_pMember;
Predicate m_pred;
};
template<typename TofStruct, typename TofMember, typename Predicate>
Member_t<TofStruct, TofMember, Predicate> Member(TofMember (TofStruct::*pMember), Predicate pred)
{
return Member_t<TofStruct, TofMember, Predicate>( pMember, pred );
}
}
int main()
{
Buch Buecher[] =
{
Buch( "OOP für Dummies", "Marcus Bäckmann", "3826629841" ),
Buch( "Effektiv C++ programmieren", "Scott Meyers", "3827313058" ),
Buch( "Die C++-Programmiersprache", "Bjarne Stroustrup", "382731660X" ),
Buch( "Entwurfsmuster", "GoF", "3827318629" ),
Buch( "Algorithmen in C++ ", "Robert Sedgewick", "3893194622" ),
Buch( "C++ Ent- Packt", "Herbert Schildt", "3826607317" ),
Buch( "Exceptional C++", "Herb Sutter", "3827317118" ),
Buch( "Modern C++ Design", "A. Alexandrescu", "0201704315" ),
Buch( "Effective STL", "Scott Meyers", "0201749629" )
};
Buch * const BuecherBegin = Buecher;
Buch * const BuecherEnd = Buecher + sizeof(Buecher) / sizeof(Buecher[0]);
sort( BuecherBegin, BuecherEnd, PODTools::Member( &Buch::titel, less<string>() ) ); //heute sortiere ich mal nach titeln
copy( BuecherBegin, BuecherEnd, ostream_iterator<Buch>( cout, "\n" ) );
Buch * const f = find_if( BuecherBegin, BuecherEnd, bind1st( PODTools::Member( &Buch::autor, equal_to<string>() ), "Scott Meyers" ) );
if(f != BuecherEnd)
cout << *f << endl;
}
ahja der name Member und member_t sind unpassend
[ Dieser Beitrag wurde am 01.05.2003 um 14:52 Uhr von Dimah editiert. ]