Effective STL 20 Specify comparison types for associative containers of pointers
来源:互联网 发布:触摸屏一体机软件 编辑:程序博客网 时间:2024/06/07 01:19
define a set
set<string*>ssp;// is shorthand for this:set<string*, less<string*> > ssp;// to be completely accurate, it's shorthand forset<string*, less<string*>, allocator<string*> > ssp;
I. specify comparison type
// set tmplate's three parameters is a type. it wants a type that can // internally instantiate to create a function.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;StringPtrSet ssp;
II. better way to specify comparison
struct DeferenceLess { template<typename PtrType> bool operator()(PtrType pT1, PtrType pT2) const { return *pT1 < *pT2; }};set<string*, DerefernceLess> ssp;
III. why better?
// explanation (from item 7)class SpecialString:public string {...};template<typename T>struct DeleteObject: public unary_function<const T*, void> { void operator()(const T* ptr) const { delete ptr; }};deque<SpecialString*> dssp;...for_each(dssp.begin(), // Error! Deletion of a derived dssp.end(), DeleteObject<string>()); // object via a base class pointer // where there is no virtual // destructor// then, move the templatization from DeleteObject to its operator():struct DeleteObject { template<typename T> void operator()(const T*ptr) const { delete ptr; }deque<SpecialString*> dssp;...// Compilers know the type of pointer being passed to DeletObject::operator(), // so we have them automatically instantiate an operator() taking that type of pinterfor_each(dssp.begin(), dssp.end(), DeleteObject()); // well!
print directly
for (StringPtrSet::const_iterator i = ssp.begin(); i != ssp.end(); ++i) { cout << *i << endl;
for_each print
void print(const string *ps) { cout << *ps << endl;}for_each(ssp.begin(), ssp.end(), print);
transform print
// Compilers know the type of pointer being passed to DeletObject::operator(), // so we have them automatically instantiate an operator() taking that type of pinter// when functors of this type are passed a T*, they return a const T&struct Dereference { template<typename T> const T& operator()(const T *ptr) const { return *ptr; }};transform(ssp.begin(), ssp.end(), ostream_iterator<stirng>(cout, "\n"), Dereference());
阅读全文
0 0
- Effective STL 20 Specify comparison types for associative containers of pointers
- STL associative containers(关联容器)
- STL Associative Containers Types:map、set、multimap、mutilset以及对应的unordered类型
- Associative Containers
- #254 – 能够装载FlowDocument的容器(Types of Containers for Hosting a FlowDocument)
- The Standard Librarian: Containers of Incomplete Types
- stl containers inspectors for gdb
- !!!Chapter 10 Associative Containers
- Chapter 11.Associative Containers
- Effective STL 21 Always have comparison functions return false for equal values
- stl containers usage empirical—— from Effective STL
- Effective STL 23 Consider replacing associative container with sorted vector
- comparison of distinct pointer types lacks a cast
- error : comparison of distinct pointer types lacks a cast
- NOTES of "effective STL"
- STL Containers
- STL Containers
- comparison of AJAX frameworks for ASP.NET
- [日推荐]『小幸运商店』解忧杂货铺,专治不开心
- 从一道网易面试题浅谈OC线程安全
- 1568:兔子繁衍问题
- 边界值分析法设计测试用例
- selenium之unittest的简单用法(一)
- Effective STL 20 Specify comparison types for associative containers of pointers
- Android初级开发(九)——网络交互—HttpURLConnection
- Batch Normalization 学习笔记
- av_register_all函数分析
- 在字符串中插入另一个字符串方法二
- 【年度总结】万里赴戎机,关山度若飞
- Jquery里面的jsonp
- 【转】CSS块级元素和行内元素
- 欢迎使用CSDN-markdown编辑器