Effective STL学习笔记-条款19
2017-08-27 21:01
239 查看
条款19 了解相等和等价的区别
接下来实现一个set,它可以存放不区分大小写的string。
先看看书上的版本:
正如我们所想的一样,set容器只有一个值是”str”。
在新版的c++11中已经放弃了binary_function。在c++17更是把他移除了,同样移除的还包括:point_to_binary_function,ptr_fun等等。
实际上我们不继承binary_function,也可以实现同样的行为,这也是仿函数。
(如有不正确的地方,欢迎指出)
了解相等和等价的区别
例如find函数,或者一个set容器插入一个值得时候都会进行比较。但是它们的行为是不同的,find是通过 operator==,而set::insert对于相同的定义是等价,基于operator<。接下来实现一个set,它可以存放不区分大小写的string。
先看看书上的版本:
//忽略大小写 struct CIStringCompare : binary_function<string, string, bool> { public: bool operator()(const string& lhs, const string& rhs) { //忽略大小写比较函数 return stringCompareIgnoreCase(lhs.c_str(), rhs.c_str()); } }; //使用如下 set<string, CIStringCompare> s; s.insert("str"); s.insert("Str"); s.insert("sTr"); for (const string& str: s) { cout << str << " "; } cout << endl;
正如我们所想的一样,set容器只有一个值是”str”。
在新版的c++11中已经放弃了binary_function。在c++17更是把他移除了,同样移除的还包括:point_to_binary_function,ptr_fun等等。
实际上我们不继承binary_function,也可以实现同样的行为,这也是仿函数。
(如有不正确的地方,欢迎指出)
相关文章推荐
- 【Effective STL】条款1-5学习笔记
- Effective STL学习笔记-条款1
- 【Effective STL】条款13-18学习笔记
- Effective STL学习笔记-条款8-9
- Effective STL学习笔记-条款36
- Effective STL学习笔记-条款44
- Effective STL学习笔记-条款43
- Effective STL学习笔记-条款31
- Effective STL学习笔记-条款37
- Effective STL学习笔记-条款24
- Effective STL 学习笔记:19 ~ 20
- Effective STL学习笔记-条款30
- Effective STL学习笔记-条款39
- EffectiveC++学习笔记-条款18|19
- 【Effective STL】条款6-12学习笔记
- Effective STL学习笔记-条款34
- EffectiveC++学习笔记-条款28|29
- 学习笔记 07-1-19
- Java学习笔记_19_String类
- springmvc学习笔记(19)——CRUD之增加