C STL常用演算法總結

2021-09-25 23:38:03 字數 4379 閱讀 6617

演算法:問題的求解步驟,以有限的步驟,解決數學或邏輯中的問題。

accumulate 元素統計

template t accumulate ( inputiterator first, inputiterator last, t init );

template t accumulate ( inputiterator first, inputiterator last, t init, binaryoperation binary_op );

template t accumulate ( inputiterator first, inputiterator last, t init )

int myfunction (int x, int y) 

struct myclass

} myobject;

int init = 100;

int numbers = ;

cout << accumulate (numbers, numbers+3, init, myfunction ); // 220

cout << accumulate (numbers, numbers+3, init, myobject ); // 280

binary_search 二分查詢

template bool binary_search ( forwarditerator first, forwarditerator last,  const t& value );

template bool binary_search ( forwarditerator first, forwarditerator last, const t& value, compare comp );

copy 拷貝

template outputiterator copy ( inputiterator first, inputiterator last, outputiterator result );
templateoutputiterator copy ( inputiterator first, inputiterator last, outputiterator result )

template bidirectionaliterator2 copy_backward ( bidirectionaliterator1 first, bidirectionaliterator1 last, bidirectionaliterator2 result );
templatebidirectionaliterator2 copy_backward ( bidirectionaliterator1 first,bidirectionaliterator1 last,  bidirectionaliterator2 result )

count :計數

template typename iterator_traits::difference_type

count ( forwarditerator first, forwarditerator last, const t& value ) ;

template ptrdiff_t count ( inputiterator first, inputiterator last, const t& value )

template typename iterator_traits::difference_type

count_if ( forwarditerator first, forwarditerator last, predicate pred );

template ptrdiff_t count_if ( inputiterator first, inputiterator last, predicate pred )

equal:判斷相等

template bool equal ( inputiterator1 first1, inputiterator1 last1,  inputiterator2 first2 );

template bool equal ( inputiterator1 first1, inputiterator1 last1, inputiterator2 first2, binarypredicate pred );

template bool equal ( inputiterator1 first1, inputiterator1 last1, inputiterator2 first2 )

return true;

}

fill :填充

template < class forwarditerator, class t >

void fill ( forwarditerator first, forwarditerator last, const t& value );

template < class forwarditerator, class t >

void fill ( forwarditerator first, forwarditerator last, const t& value )

find : 查詢

template inputiterator find ( inputiterator first, inputiterator last, const t& value );
templateinputiterator find ( inputiterator first, inputiterator last, const t& value )

for_each:對某一區間內所有元素實行某種操作

template function for_each (inputiterator first, inputiterator last, function f);
templatefunction for_each(inputiterator first, inputiterator last, function f)

merge:合併

template outputiterator merge ( inputiterator1 first1, inputiterator1 last1, inputiterator2 first2, inputiterator2 last2, outputiterator result );

template outputiterator merge ( inputiterator1 first1, inputiterator1 last1, inputiterator2 first2, inputiterator2 last2, outputiterator result, compare comp );`

template outputiterator merge ( inputiterator1 first1, inputiterator1 last1,  inputiterator2 first2, inputiterator2 last2, outputiterator result )

}

max 和 min

partile_sort:區域性排序

template void partial_sort ( randomaccessiterator first, randomaccessiterator middle, randomaccessiterator last );

template void partial_sort ( randomaccessiterator first, randomaccessiterator middle, randomaccessiterator last, compare comp );

sort : 排序

template void sort ( randomaccessiterator first, randomaccessiterator last );

template void sort ( randomaccessiterator first, randomaccessiterator last, compare comp );

今天先寫到這兒,下次繼續

c STL演算法總結

標頭檔案 功能algorithm 演算法函式 numeric 數值演算法 functiona 函式物件 仿函式 分類no.分類說明解釋1 非可變序列演算法 non modifying sequence operations 不直接修改容器內容的演算法。2可變序列演算法 modifying seque...

C STL 常用排序演算法

以下是排序和通用演算法 提供元素排序策略 merge 合併兩個有序序列,存放到另乙個序列。例如 vecinta,vecintb,vecintc是用vector宣告的容器,vecinta已包含1,3,5,7,9元素,vecintb已包含2,4,6,8元素 vecintc.resize 9 擴大容量 m...

C STL 常用查詢演算法

find 查詢元素,內建型別 void test01 查詢在區間內出現的第乙個5,返回當前查詢元素迭代器 vector int iterator it find v.begin v.end 5 if it v.end else find 查詢元素,自定義型別,得告訴編譯器如何做對比 class pe...