您的位置:首页 > 编程语言 > C语言/C++

为包含指针的关联容器指定比较类型

2016-08-02 09:50 429 查看
#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>
#include <set>
using namespace std;
class DereferenceLess
{
public:
template<typename T>
bool operator()(const T* ptr1,const T* ptr2)
{
return *ptr1<*ptr2;
}
};
class Deference{
public:
template<typename T>
const T& operator()(const T* ptr)
{
return *ptr;
}
};
int main()
{
set<string*,DereferenceLess> ssp;
ssp.insert(new string("Anteater"));
ssp.insert(new string("Wombat"));
ssp.insert(new string("Lemur"));
ssp.insert(new string("Penguin"));

transform(ssp.begin(),ssp.end(),ostream_iterator<string>(cout,"\n"),Deference());
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++ STL 容器