C++之Effective STL学习笔记Item20
Q:假设我们现在需要一个string*的set,我们向其插入一些动物到这个set中:
set<string*> ssp; // ssp = “set of string ptrs”
ssp.insert(new string("Anteater"));
ssp.insert(new string("Wombat"));
ssp.insert(new string("Lemur"));
ssp.insert(new string("Penguin"));
You then write the following code to print the contents of the set, expecting the strings to come out in alphabetical order. After all, sets keep their contents sorted.
for (auto i : ssp)
cout << i << endl;
真正打印出来的会是字符串的指针值。或许你说那我在i前面加个*,取出字符串不就搞定了,so easy!是可以,但是别忘了我们的需求,我们需要使得字符串有序排列,而这样得到的只是对指针值有序排列了,显然不满足我们得要求!
其实我们上述的语句set<string*> ssp;是下面语句的简化写法:
set<string*, less<string*> > ssp;
If you want the string* pointers to be stored in the set in an order determined by the string values, you can’t use the default comparison functor class less<string*>. You must instead write your own comparison functor class, one whose objects take string* pointers and order them by the values of the strings they point to. Like this:
struct StringPtrLess:
public binary_function<const string*, const string*, bool> {
bool operator()(const string *ps1, const string *ps2) const
{
return *ps1 < *ps2;
}
};
Then you can use StringPtrLess as ssp’s comparison type:
typedef set<string*, StringPtrLess> StringPtrSet;
StringPtrSet ssp;
好了,最终的解决方案出炉了:
typedef set<string*, StringPtrLess> StringPtrSet;
StringPtrSet ssp;
ssp.insert(new string("Anteater"));
ssp.insert(new string("Wombat"));
ssp.insert(new string("Lemur"));
ssp.insert(new string("Penguin"));
for (auto i : ssp)
{
cout << *i << endl;
}
当然,其中的打印过程可以有很多花样,比如我也可以利用alogrithm来完成,也是OK的。
If you want to use an algorithm instead, you could write a function that knows how to dereference string* pointers before printing them, then use that function in conjunction with for_each:
void print(const string *ps)
{
cout << *ps << endl;
} for_each(ssp.begin(), ssp.end(), print);
感谢大家的阅读,希望能帮到大家!
Published by Windows Live Writer.
C++之Effective STL学习笔记Item20的更多相关文章
- Effective STL 学习笔记 39 ~ 41
Effective STL 学习笔记 39 ~ 41 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...
- Effective STL 学习笔记 Item 38 : Design functor classes for pass-by-value
Effective STL 学习笔记 Item 38 : Design functor classes for pass-by-value */--> div.org-src-container ...
- Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据
Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据 */--> div.org-src-container { font-size: 85%; font-fam ...
- Effective STL 学习笔记 32 ~ 33
Effective STL 学习笔记 32 ~ 33 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...
- Effective STL 学习笔记 31:排序算法
Effective STL 学习笔记 31:排序算法 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...
- Effective STL 学习笔记 Item 30: 保证目标区间足够大
Effective STL 学习笔记 Item 30: 保证目标区间足够大 */--> div.org-src-container { font-size: 85%; font-family: ...
- Effective STL 学习笔记 Item 26: Prefer Iterator to reverse_iterator and const_rever_itertor
Effective STL 学习笔记 Item 26: Prefer Iterator to reverse_iterator and const_rever_itertor */--> div ...
- Effective STL 学习笔记: Item 22 ~ 24
Effective STL 学习笔记: Item 22 ~ 24 */--> div.org-src-container { font-size: 85%; font-family: monos ...
- Effective STL 学习笔记 Item 21:Comparison Function 相关
Effective STL 学习笔记 Item 21:Comparison Function 相关 */--> div.org-src-container { font-size: 85%; f ...
随机推荐
- fpga Verilog hdl 按键消抖 部分程序讲解
module debounce(clk_in,rst_in,key_in,key_pulse,key_state); input clk_in;//system clock input rst_in; ...
- JAVA小基础
JAVA的jsp程序中,jar如果不能再引用的时候加入到lib文件夹而选择外部引用,可能会导致jar不能被找到的问题. string.format的占位符一般使用%s表示字符串的意思,与C#的{0}这 ...
- XManager 远程连接Netbackup图形用户界面
XManager远程连接Netbackup图形用户界面 目标: 在自己的Windows桌面打开Linux的Netbackup图形用户界面 工具: Windows: Xmanager,Xshell, ...
- Render渲染函数和JSX
1.Render函数:render是用来替换temlate的,需要更灵活的模板的写法的时候,用render. 官网API地址:https://cn.vuejs.org/v2/guide/render- ...
- nodejs安装遇到npm命令无法使用问题
解决方法: 在用户文件夹中建立npm文件夹就可以使用了. 再使用npm命令就可以了.
- iOS中的数据存储方式_SQLite3
优点: 1) SQLite是一款轻型的嵌入式数据库; 2) 它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了 3) 它的处理速度比Mysql.PostgreSQL这两款著名的数据库都还 ...
- 【线性基】bzoj2844: albus就是要第一个出场
线性基求可重rank 题目描述 给定 n 个数 $\{ a_i \}$ ,以及数 $x$. 将 $\{ a_i \}$ 的所有子集(包括空集)的异或值从小到大排序,得到 $\{ b_i \} $. ...
- intellij idea 下载安装破解教程
官网下载:http://www.jetbrains.com/idea/download/#section=windows 选择 Ultimate 版本下载 下载完成后,打开安装 在安装路径位置,可以 ...
- 分享几个能用的 editplus 注册码
转载自: https://www.cnblogs.com/shihaiming/p/6422441.html 原文:http://host.zzidc.com/wljc/1286.html EditP ...
- centos 7 安装WordPress的参考博文
安装方法: https://www.cnblogs.com/flankershen/p/7476415.html 安装完,测试不成功的解决办法: https://blog.csdn.net/u0104 ...