為使類屬性算法具有靈活性,STL常用函數重載機制為算法提供兩種形式,算法的第一種形式使用的是常規操作來實現目標。在第二種形式中,算法可以根據用戶指定的準則對元素進行處理。這種準則是通過函數對象來傳遞的。函數對象世紀上是重載了operator()的類模版。
STL提供了許多函數對象,這些對象包含在頭文件<functional>中。
| 函數對象 |
說明 |
| 算術函數對象: |
| plus<T> |
x+y |
| minus<T> |
x-y |
| multiplies<T> |
x*y |
| divides<T> |
x/y |
| modulus<T> |
x%y |
| negate<T> |
-x |
| 關系函數對象: |
| equal_to<T> |
x==y |
| not_equal_to<T> |
x!=y |
| grater<T> |
x>y |
| greater_equal<T> |
x>=y |
| less<T> |
x<y |
| less_equal<T> |
x<=y |
| 邏輯函數對象: |
? |
| logical_not<T> |
!x |
| logical_and<T> |
x&y |
| logical_or<T> |
x|y |
?