您的位置:首页 > 编程语言 > Go语言

第09篇 ACM/ICPC竞赛之STL--algorithm

2010-04-02 15:15 405 查看
<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 )

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/camel_flying/archive/2009/08/17/4454199.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: