Effective STL 条款20
来源:互联网 发布:人间正道是沧桑 知乎 编辑:程序博客网 时间:2024/05/16 15:33
为指针的关联容器指定比较类型
举个书上的例子:
set<string*>ssp;ssp.insert(new string("Anteater"));ssp.insert(new string("Wombat"));ssp.insert(new string("Lemur"));ssp.insert(new string("Penguin"));for(set<string*>::const_iterator i=ssp.begin();i!=ssp.end();++i)cout<<*i<<endl;
你认为上面的code会输出什么内容?
是字符串?顺序排列的字符串?
实际上你看到的是四个十六进制的数,它们是指针的值。因为set容纳的是指针。*i是一个string的指针,这就是本节的重点!!!
一定要记住set中存储的到底是什么类型!!!
如果你想按字符串顺序输出,怎么做?
如下代码:
#include <functional>#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*ps1, const string *ps2)const { return *ps1 < *ps2; }};typedef set<string*, StringPtrLess>StringPtrSet;void print(const string*ps){ cout << (*ps) << endl; }int main(){ StringPtrSet 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); return 0;}
上面的code,是根据书上的源码调通的,大家要注意有关头文件的调用。
1 0
- Effective STL 条款20
- Effective STL学习笔记-条款20
- Effective STL 条款17
- Effective STL 条款18
- Effective STL 条款16
- Effective STL 条款7
- Effective STL条款50
- Effective STL 条款35
- Effective STL 条款37
- Effective STL 条款1
- Effective STL条款21
- Effective STL条款44
- Effective STL条款16
- Effective STL条款29
- Effective STL 条款18
- Effective STL 条款1
- Effective STL 条款30
- Effective STL 条款2
- 浙江省赛e题Modulo Query
- Linux 常用命令
- 在Python中使用LDA处理文本
- struts2:通配符的使用
- 枚举
- Effective STL 条款20
- keil uvision4怎么破解
- java简单排序之冒泡排序
- 如何制作BUNDLE
- 让一个div在页面底下,居中
- json转成复杂的bean
- Android KeyCode列表
- Docker网络详解及pipework源码解读与实践
- 设计模式读书笔记之观察者模式(Observer)