steel2010
全部博文(42)
2011年(1)
2009年(41)
cynthia
lijunbin
patrick3
leon916
tomer
10330295
ss00_201
水落云起
qqq50221
分类: C/C++
2009-11-18 11:31:26
// SetOfString.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <set>#include <iostream>#include <string>#include <algorithm>using namespace std;//方法一struct StringPtrLess : public binary_function<const string *, const string *, bool>{ bool operator()(const string *sp1, const string *sp2) const { return *sp1 < *sp2; }};//方法二void print(const string * ptr){ cout<<*ptr<<endl;}//方法三struct DereferenceLess { template <typename PtrType> bool operator()(PtrType pT1, PtrType pT2) const { return *pT1 < *pT2; }};int _tmain(int argc, _TCHAR* argv[]){ typedef set<string *, DereferenceLess> StrPtr; //typedef set StrPtr; StrPtr ssp; ssp.insert(new string("Anteater")); ssp.insert(new string("Wombat")); ssp.insert(new string("Lemur")); ssp.insert(new string("Penguin")); //for_each(ssp.begin(), ssp.end(), print); for (StrPtr::const_iterator i=ssp.begin(); i!=ssp.end(); i++) { cout<<*(*i)<<endl; //注意两个星号,否则返回地址 } return 0;}
上一篇:c++面向对象:多态与动态绑定
下一篇:operate[]和insert在map应用
登录 注册