ACM/ICPC竞赛之STL--algorithm

本文详细介绍了C++ STL库中的<algorithm>头文件中常用模板函数的应用,通过多个示例程序展示了for_each、min_element/max_element、sort、copy等函数的使用方法,帮助读者深入理解STL算法的功能和实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


<algorithm>无疑是STL中最大的一个头文件,它是由一大堆模板函数组成的。 


下面列举出<algorithm>中的模板函数: 


adjacent_find / binary_search / copy / copy_backward / count / count_if / equal / equal_range / fill / fill_n / find / find_end / find_first_of / find_if / for_each / generate / generate_n / includes / inplace_merge / iter_swap / lexicographical_compare / lower_bound / make_heap / max / max_element / merge / min / min_element / mismatch / next_permutation / nth_element / partial_sort / partial_sort_copy / partition / pop_heap / prev_permutation / push_heap / random_shuffle / remove / remove_copy / remove_copy_if / remove_if / replace / replace_copy / replace_copy_if / replace_if / reverse / reverse_copy / rotate / rotate_copy / search / search_n / set_difference / set_intersection / set_symmetric_difference / set_union / sort / sort_heap / stable_partition / stable_sort / swap / swap_ranges / transform / unique / unique_copy / upper_bound 


如果详细叙述每一个模板函数的使用,足够写一本书的了。还是来看几个简单的示例程序吧。 


示例程序之一,for_each遍历容器: 


#include <iostream> 
#include <vector> 
#include <algorithm> 
using namespace std; 


int Visit(int v) // 遍历算子函数 

cout << v << " "; 
return 1; 



class MultInt // 定义遍历算子类 

private: 
int factor; 
public: 
MultInt(int f) : factor(f) 


void operator()(int &elem) const 

elem *= factor; 

}; 


main() 

vector<int> L; 
for (int i=0; i<10; i++) L.push_back(i); 
for_each(L.begin(), L.end(), Visit); 
cout << endl; 
for_each(L.begin(), L.end(), MultInt(2)); 
for_each(L.begin(), L.end(), Visit); 
cout << endl; 
return 1; 



程序的输出结果为: 


0 1 2 3 4 5 6 7 8 9 
0 2 4 6 8 10 12 14 16 18 


示例程序之二,min_element/max_element,找出容器中的最小/最大值: 


#include <iostream> 
#include <vector> 
#include <algorithm> 
using namespace std; 


main() 

vector<int> L; 
for (int i=0; i<10; i++) L.push_back(i); 
vector<int>::iterator min_it = min_element(L.begin(), L.end()); 
vector<int>::iterator max_it = max_element(L.begin(), L.end()); 
cout << "Min is " << *min_it << endl; 
cout << "Max is " << *max_it << endl; 
return 1; 



程序的输出结果为: 


Min is 0 
Max is 9 


示例程序之三,sort对容器进行排序: 


#include <iostream> 
#include <vector> 
#include <algorithm> 
using namespace std; 
void Print(vector<int> &L) 

for (vector<int>::iterator it=L.begin(); it!=L.end(); it++) 
cout << *it << " "; 
cout << endl; 

main() 

vector<int> L; 
for (int i=0; i<5; i++) L.push_back(i); 
for (int i=9; i>=5; i--) L.push_back(i); 
Print(L); 
sort(L.begin(), L.end()); 
Print(L); 
sort(L.begin(), L.end(), greater<int>()); // 按降序排序 
Print(L); 
return 1; 



程序的输出结果为: 


0 1 2 3 4 9 8 7 6 5 
0 1 2 3 4 5 6 7 8 9 
9 8 7 6 5 4 3 2 1 0 


示例程序之四,copy在容器间复制元素: 


#include <vector> 
#include <algorithm> 
#include <iostream> 
using namespace std; 
main( ) 

// 先初始化两个向量v1和v2 
vector <int> v1, v2; 
for (int i=0; i<=5; i++) v1.push_back(10*i); 
for (int i=0; i<=10; i++) v2.push_back(3*i); 


cout << "v1 = ( " ; 
for (vector <int>::iterator it=v1.begin(); it!=v1.end(); it++) 
cout << *it << " "; 
cout << ")" << endl; 


cout << "v2 = ( " ; 
for (vector <int>::iterator it=v2.begin(); it!=v2.end(); it++) 
cout << *it << " "; 
cout << ")" << endl; 


// 将v1的前三个元素复制到v2的中间 
copy(v1.begin(), v1.begin()+3, v2.begin()+4); 


cout << "v2 with v1 insert = ( " ; 
for (vector <int>::iterator it=v2.begin(); it!=v2.end(); it++) 
cout << *it << " "; 
cout << ")" << endl; 


// 在v2内部进行复制,注意参数2表示结束位置,结束位置不参与复制 
copy(v2.begin()+4, v2.begin()+7, v2.begin()+2); 


cout << "v2 with shifted insert = ( " ; 
for (vector <int>::iterator it=v2.begin(); it!=v2.end(); it++) 
cout << *it << " "; 
cout << ")" << endl; 
return 1; 



程序的输出结果为: 


v1 = ( 0 10 20 30 40 50 ) 
v2 = ( 0 3 6 9 12 15 18 21 24 27 30 ) 
v2 with v1 insert = ( 0 3 6 9 0 10 20 21 24 27 30 ) 
v2 with shifted insert = ( 0 3 0 10 20 10 20 21 24 27 30 )
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值