C++ STL The compare function
来源:互联网 发布:深圳java自行车 编辑:程序博客网 时间:2024/06/04 18:04
The Compare Function in STL of C++
There are many comparing functions used in the STL.
There are 3 ways to implement the comparing function.
1. Using operation overloading to define <
1.1 Using own bool function
Define the element in STL like following:
struct T{ bool operator< (T other) const{ return weight < other.weight; } int index; int weight;};
1.2 Using friend function
Define the element in STL like following:
struct T{ friend bool operator< (T a, T b){ return a.weight < b.weight; } int index; int weight;};
2. Using operator overloading to define ()
Using struct to reload ():
struct cmp{ bool operator () (T a, T b){ return a.weight < b.weight; }};
sort(container.begin(), container.end(), cmp());
NOTE: cannot miss the latest ()
3. Directly using bool function without struct:
bool cmp(T a, T b) { return a.weight > b.weight;}sort(contain.begin(), container.end(), cmp);
NOTE: < is from little to large
> is from large to little
4. Some intern comparing functions in the head file of functional
include <functional>
equal_to<Type>not_equal_to<Type> greater<Type>greater_equal<Type> less<Type>less_equal<Type>
Note: must add () when use
sort(begin, end, less<T>());
0 0
- C++ STL The compare function
- Compare the function: SQL server vs ORACLE
- The " count " function in map (STL)
- compare function overload
- c++compare
- Windows Via C/C++: The CreateProcess function
- C Function Call Conventions and the Stack
- C Function Call Conventions and the Stack
- Partially compare the CString
- The function that returns the pointer (C Programming)
- c++ container with user defined compare function
- c++ STL::String::Compare (_Buf内容一致,compare不相等)
- STL Function List
- STL Function Objects
- stl function扩展(一)
- Codeforces Round #150 (Div. 2) C. The Brand New Function
- C++: How is the process of function calling in C++
- Generic Programming and the STL笔记6--Function Object Classes
- SWT源码分析 (一)
- noip2007 守望者的逃离 (贪心)
- Git的笔记
- Android内存优化之OOM
- Object类
- C++ STL The compare function
- zzuli OJ 1104: 求因子和(函数专题)
- C++对象模型系列集合
- 测试用例的基本概念
- 错误: Native library not found! Please copy libbdpush_V2_3.so into your project!
- Jenkins+RobotFramework搭建实录(一)
- html5学习canvas
- 自学安卓复习基础_之四(关于intent和bundle传值)
- TCP和UDP比较