shared_ptr and "<"
来源:互联网 发布:xampp mac使用教程 编辑:程序博客网 时间:2024/06/16 16:29
a shared_ptr implements some comparison operators
- e.g., a shared_ptr implements the “<“ operator
- but, it doesn’t invoke “<“ on the pointed-to objects
‣ instead, it just promises a stable, strict ordering
‣ given two shared pointers, it will pick some ordering between them
(probably based on the pointer address, not the pointed-to value)
- this means you can use shared_ptrs as keys in maps, but you
have to use a slightly more complex form of the sort algorithm
- e.g., a shared_ptr implements the “<“ operator
- but, it doesn’t invoke “<“ on the pointed-to objects
‣ instead, it just promises a stable, strict ordering
‣ given two shared pointers, it will pick some ordering between them
(probably based on the pointer address, not the pointed-to value)
- this means you can use shared_ptrs as keys in maps, but you
have to use a slightly more complex form of the sort algorithm
‣ you have to provide sort with a comparison function
bool sortfunction(shared_ptr<int> x, shared_ptr<int> y) {
return *x < *y;
}
bool printfunction(shared_ptr<int> x) {
std::cout << *x << std::endl;
}
int main(int argc, char **argv) {
vector<shared_ptr<int> > vec;
vec.push_back(shared_ptr<int>(new int(9)));
vec.push_back(shared_ptr<int>(new int(5)));
vec.push_back(shared_ptr<int>(new int(7)));
std::sort(vec.begin(), vec.end(), &sortfunction);
std::for_each(vec.begin(), vec.end(), &printfunction);
return EXIT_SUCCESS;
}
0 0
- shared_ptr and "<"
- unique_ptr and shared_ptr
- C++ 之 constexpr And shared_ptr
- covariant return type and boost::shared_ptr
- The C++ Standard Library: shared_ptr and unique_ptr
- shared_ptr
- shared_ptr
- shared_ptr
- shared_ptr
- shared_ptr
- shared_ptr
- shared_ptr
- shared_ptr
- shared_ptr
- shared_ptr
- shared_ptr
- shared_ptr
- shared_ptr()
- cocapods 更新第三方库遇到的问题
- MFC中char*,string和CString之间的转换
- 蓝桥杯 猜算式
- HDU Humble Numbers
- a和&a的区别
- shared_ptr and "<"
- java中ArrayList和class简介
- C++获取随机数的办法
- 工作感悟
- 蓝桥杯 — 基础练习 特殊回文数(思路+详解)
- 使用CocoaPods过程中的几个问题
- 【BZOJ2243】【SDOI2011】染色(树链剖分+线段树)
- WEB前端面试重点
- iOS block 小结