Chinaunix首页 | 论坛 | 博客
  • 博客访问: 65843
  • 博文数量: 42
  • 博客积分: 1730
  • 博客等级: 上尉
  • 技术积分: 430
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-02 13:06
文章分类

全部博文(42)

文章存档

2011年(1)

2009年(41)

我的朋友

分类: 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;
}


阅读(307) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~