C++ STL(十)算法

算法(Algorithm):STL算法主要由头文件<algorithm>,<numeric>和<functional>组成

<algorithm> 比较、交换、查找、遍历、复制、修改、反转、排序、合并等等算法

<numeric> 只包括几个在序列上进行数学运算的模板函数,加法、乘法等

<functional> 定义了一些模板类,用于声明函数对象

 

查找算法:

_FwdIt adjacent_find(_FwdIt _First, _FwdIt _Last) //查找标志范围内一对相邻重复元素,如果找到返回指向第一个元素迭代器,未找到返回迭代器等于_Last

 1 inline void _adjacent_find()
 2 {
 3     vector<int> vecInt;
 4     vecInt.push_back(1);
 5     vecInt.push_back(3);
 6     vecInt.push_back(2);
 7     vecInt.push_back(2);
 8     vecInt.push_back(5);
 9 
10     vector<int>::iterator it = adjacent_find(vecInt.begin(), vecInt.end());
11     if(it != vecInt.end())
12     {
13         cout<<"ok,Find"<<endl; //cout
14     }else
15     {
16         cout<<"Error,NotFind"<<endl;
17     }
18 
19     list<int> listInt;
20     listInt.push_back(1);
21     listInt.push_back(3);
22     listInt.push_back(2);
23     listInt.push_back(1);
24     listInt.push_back(2);
25 
26     list<int>::iterator itList = adjacent_find(listInt.begin(), listInt.end());
27     if(itList != listInt.end())
28     {
29         cout<<"ok,Find"<<endl;
30     }else
31     {
32         cout<<"Error,NotFind"<<endl; //cout
33     }
34 
35     getchar();
36 }
adjacent_find()

bool binary_search(_FwdIt _First, _FwdIt _Last, const _Ty& _Val) //在有序序列中查找val,找到返回ture.注意:无序序列中,不可使用

 1 inline void _binary_search()
 2 {
 3     vector<int> vecInt;
 4     vecInt.push_back(1);
 5     vecInt.push_back(3);
 6     vecInt.push_back(5);
 7     vecInt.push_back(7);
 8     vecInt.push_back(9);
 9 
10     bool bFind = binary_search(vecInt.begin(), vecInt.end(), 3); //bFind = true
11     bool bFind_1 = binary_search(vecInt.begin(), vecInt.end(), 8); //bFind_1 = false
12 
13     getchar();
14 };
binary_search

count(_InIt _First, _InIt _Last, const _Ty& _Val) //在有序序列中查找val,返回相同元素个数

 1 inline void _count()
 2 {
 3     vector<int> vecInt;
 4     vecInt.push_back(1);
 5     vecInt.push_back(2);
 6     vecInt.push_back(2);
 7     vecInt.push_back(4);
 8     vecInt.push_back(2);
 9     int nCount = count(vecInt.begin(), vecInt.end(), 2); //nCount = 3
10     int nCount_1 = count(vecInt.begin(), vecInt.end(), 6); //nCount = 0
11 
12     getchar();
13 }
count

count_if(_InIt _First, _InIt _Last, _Pr _Pred) //与_count()功能相同,不同在于统计条件使用函数对象进行判断

 1 bool GreaterEqualThree(int nNum)
 2 {
 3     if(nNum >=3)
 4     {
 5         return true;
 6     }else
 7     {
 8         return false;
 9     }
10 }
11 inline void _count_if()
12 {
13     vector<int> vecInt;
14     vecInt.push_back(1);
15     vecInt.push_back(2);
16     vecInt.push_back(2);
17     vecInt.push_back(4);
18     vecInt.push_back(2);
19     vecInt.push_back(5);
20     //统计vecInt中元素值大于等于3的元素个数
21     int nCount = count_if(vecInt.begin(), vecInt.end(), GreaterEqualThree); //nCount=2
22 
23     getchar();
24 }
count_if

iterator find(_InIt _First, _InIt _Last, const _Ty& _Val) //在标志范围内的元素进行查找val,返回找到的第一个元素迭代器

 1 inline void _find()
 2 {
 3     vector<int> vecInt;
 4     vecInt.push_back(1);
 5     vecInt.push_back(2);
 6     vecInt.push_back(2);
 7     vecInt.push_back(4);
 8     vecInt.push_back(2);
 9     vecInt.push_back(5);
10 
11     vector<int>::iterator it = find(vecInt.begin(), vecInt.end(),2);
12 
13     getchar();
14 }
find()

iterator find_if(_InIt _First, _InIt _Last, _Pr _Pred) //与_find()功能相同,不同在于查找条件使用函数对象进行判断

 1 bool GreaterThree(int nNum)
 2 {
 3     if(nNum > 3)
 4     {
 5         return true;
 6     }else
 7     {
 8         return false;
 9     }
10 }
11 inline void _find_if()
12 {
13     vector<int> vecInt;
14     vecInt.push_back(1);
15     vecInt.push_back(2);
16     vecInt.push_back(2);
17     vecInt.push_back(4);
18     vecInt.push_back(2);
19     vecInt.push_back(5);
20     //查找大于3的第一个迭代器
21     vector<int>::iterator it = find_if(vecInt.begin(), vecInt.end(),GreaterThree);
22 
23     getchar();
24 }
find_if()

 

排序算法:

_OutIt merge(_InIt1 _First1, _InIt1 _Last1,_InIt2 _First2, _InIt2 _Last2, _OutIt _Dest) //合并二个容器中的元素到Dest容器中

注意:Dest容器需要设置足够大的空间用于存储新元素

 1 inline void _merge()
 2 {
 3     vector<int> vecIntA;
 4     vecIntA.push_back(1);
 5     vecIntA.push_back(3);
 6     vecIntA.push_back(5);
 7     vecIntA.push_back(7);
 8     vecIntA.push_back(9);
 9 
10     vector<int> vecIntB;
11     vecIntB.push_back(2);
12     vecIntB.push_back(4);
13     vecIntB.push_back(6);
14     vecIntB.push_back(8);
15 
16     vector<int> vecInResult;
17     vecInResult.resize(9); //设置大小用足容合并过的新元素
18     merge(vecIntA.begin(), vecIntA.end(), vecIntB.begin(), vecIntB.end(), vecInResult.begin());
19     vector<int>::iterator it = vecInResult.begin();
20     for (it; it!=vecInResult.end(); ++it)
21     {
22         cout<<*it<<" ";
23     }
24     //1 2 3 4 5 6 7 8 9
25     getchar();
26 }
merge()

void sort(_RanIt _First, _RanIt _Last)

void sort(_RanIt _First, _RanIt _Last, _Pr _Pred)

 1 class CStudent
 2 {
 3 public:
 4     CStudent(int nID, string strName)
 5     {
 6         this->m_nID = nID;
 7         this->m_strName = strName;
 8     }
 9     ~CStudent(){}
10 
11 public:
12     int m_nID;
13     string m_strName;
14 };
15 bool Compare(CStudent &stu1, CStudent &stu2)
16 {
17     return stu1.m_nID<stu2.m_nID;
18 }
19 inline void _sort()
20 {
21     vector<int> vecIntA;
22     vecIntA.push_back(3);
23     vecIntA.push_back(2);
24     vecIntA.push_back(1);
25     vecIntA.push_back(5);
26     vecIntA.push_back(7);
27 
28     sort(vecIntA.begin(), vecIntA.end());
29     vector<int>::iterator it = vecIntA.begin();
30 
31     for (it; it!=vecIntA.end(); ++it)
32     {
33         cout<<*it<<" ";
34     }
35     //1 2 3 5 7
36 
37     cout<<endl;
38 
39     vector<CStudent> vecStu;
40     vecStu.push_back(CStudent(2,"老二"));
41     vecStu.push_back(CStudent(1,"老大"));
42     vecStu.push_back(CStudent(3,"老三"));
43     vecStu.push_back(CStudent(4,"老四"));
44     sort(vecStu.begin(), vecStu.end(), Compare);
45     vector<CStudent>::iterator itVecStu = vecStu.begin();
46     for (itVecStu; itVecStu!=vecStu.end(); ++itVecStu)
47     {
48         cout<<itVecStu->m_nID<<":"<<itVecStu->m_strName.c_str()<<endl;
49     }
50 /*
51     1:老大
52   2:老二
53     3:老三
54     4:老四*/
55 
56     getchar();
57 }
sort

void random_shuffle(_RanIt _First, _RanIt _Last)  //对容器中的元素使用随机排序, 注意:使用前需要设置随机种子srand(time(0));

 1 inline void _random_shuffle()
 2 {
 3     vector<int> vecIntA;
 4     vecIntA.push_back(1);
 5     vecIntA.push_back(2);
 6     vecIntA.push_back(3);
 7     vecIntA.push_back(4);
 8     vecIntA.push_back(5);
 9 
10     srand((unsigned int)time(0)); //设置个随机种子
11     random_shuffle(vecIntA.begin(), vecIntA.end());
12 
13     getchar();
14 }
random_shuffle()

 

拷贝和替换算法 :

_OutIt copy(_InIt _First, _InIt _Last,_OutIt _Dest) //把源容器中的元素复制到Dest容器中

注意:Dest容器需要设置足够大的空间用于存储新元素

 1 inline void _copy()
 2 {
 3     vector<int> vecIntA;
 4     vecIntA.push_back(3);
 5     vecIntA.push_back(2);
 6     vecIntA.push_back(1);
 7     vecIntA.push_back(5);
 8     vecIntA.push_back(7);
 9 
10     vector<int> vecIntB;
11     vecIntB.resize(vecIntA.size());
12     copy(vecIntA.begin(), vecIntA.end(), vecIntB.begin());
13     vector<int>::iterator it = vecIntB.begin();
14     for (it; it!=vecIntB.end(); ++it)
15     {
16         cout<<*it<<" ";
17     }
18 
19     getchar();
20 }
copy()

void replace(_FwdIt _First, _FwdIt _Last,const _Ty& _Oldval, const _Ty& _Newval) //用新的元素替换容器中指定元素

 1 inline void _replace()
 2 {
 3     vector<int> vecIntA;
 4     vecIntA.push_back(3);
 5     vecIntA.push_back(2);
 6     vecIntA.push_back(2);
 7     vecIntA.push_back(2);
 8     vecIntA.push_back(7);
 9     //把容器中所有元素为2的替换为8
10     replace(vecIntA.begin(), vecIntA.end(), 2, 8);
11     vector<int>::iterator it = vecIntA.begin();
12     for (it; it!=vecIntA.end(); ++it)
13     {
14         cout<<*it<<" ";
15     }
16     //3 8 8 8 7
17 
18     getchar();
19 }
replace()

void replace_if(_FwdIt _First, _FwdIt _Last, _Pr _Pred, const _Ty& _Val)  //与_replace()功能相同,不同在于替换条件使用函数对象进行判断

 1 bool GreaterEqualTwo(int nNum)
 2 {
 3     if(nNum >= 2)
 4     {
 5         return true;
 6     }
 7     return false;
 8 }
 9 inline void _replace_if()
10 {
11     vector<int> vecIntA;
12     vecIntA.push_back(3);
13     vecIntA.push_back(1);
14     vecIntA.push_back(1);
15     vecIntA.push_back(1);
16     vecIntA.push_back(7);
17     //把容器中所有>=2的元素替换为8
18     replace_if(vecIntA.begin(), vecIntA.end(),GreaterEqualTwo,8);
19     vector<int>::iterator it = vecIntA.begin();
20     for (it; it!=vecIntA.end(); ++it)
21     {
22         cout<<*it<<" ";
23     }
24     //8 1 1 1 8
25 
26     getchar();
27 }
replace_if()

void fill(_FwdIt _First, _FwdIt _Last, const _Ty& _Val) //与replace()单一替换不同,fill可以批量替换容器中所有的元素

 1 inline void _fill()
 2 {
 3     vector<int> vecIntA;
 4     vecIntA.push_back(3);
 5     vecIntA.push_back(2);
 6     vecIntA.push_back(2);
 7     vecIntA.push_back(2);
 8     vecIntA.push_back(7);
 9 
10     fill(vecIntA.begin(), vecIntA.end(), 0);
11 
12     getchar();
13 }
fill()

void swap(vector<_Ty, _Alloc>& _Left, vector<_Ty, _Alloc>& _Right) //把二个容器进行交换,注意:交换后容器的size和resize都会发生改变

 1 inline void _swap()
 2 {
 3     vector<int> vecIntA;
 4     vecIntA.push_back(3);
 5     vecIntA.push_back(2);
 6     vecIntA.push_back(1);
 7     vecIntA.push_back(5);
 8     vecIntA.push_back(7);
 9 
10     vector<int> vecIntB;
11     swap(vecIntA,vecIntB);
12     
13     vector<int>::iterator itA = vecIntA.begin();
14     vector<int>::iterator itB = vecIntB.begin();
15 
16     cout<<"vecIntA"<<endl;
17     for (itA; itA!=vecIntA.end(); ++itA)
18     {
19         cout<<*itA<<" ";
20     }
21     cout<<"vecIntB"<<endl;
22     for (itB; itB!=vecIntB.end(); ++itB)
23     {
24         cout<<*itB<<" ";
25     }
26     
27     getchar();
28 }
swap()

 

算术算法:

int accumulate(_InIt _First, _InIt _Last, _Ty _Val) //把指定范围内元素进行相加,最后再把结果加上Val,返回最终的结果

 1 inline void _accumulate()
 2 {
 3     vector<int> vecIntA;
 4     vecIntA.push_back(1);
 5     vecIntA.push_back(3);
 6     vecIntA.push_back(5);
 7     vecIntA.push_back(7);
 8     vecIntA.push_back(9);
 9 
10     int nTotal = accumulate(vecIntA.begin(), vecIntA.end(), 100); //125
11 
12     getchar();
13 }
accumulate()

 

集合算法:

_OutIt set_union(_InIt1 _First1, _InIt1 _Last1,_InIt2 _First2, _InIt2 _Last2, _OutIt _Dest) //二个容器并集后赋值给新容器,新容器中包含二个容器中所有元素,但是不会包含重复相同的部份.resize剩余部份以默认值0填充

注意:Dest容器需要设置足够大的空间用于存储新元素

 1 inline void set_union()
 2 {
 3     vector<int> vecIntA;
 4     vecIntA.push_back(1);
 5     vecIntA.push_back(3);
 6     vecIntA.push_back(5);
 7     vecIntA.push_back(7);
 8     vecIntA.push_back(9);
 9 
10     vector<int> vecIntB;
11     vecIntB.push_back(1);
12     vecIntB.push_back(3);
13     vecIntB.push_back(5);
14     vecIntB.push_back(6);
15     vecIntB.push_back(8);
16 
17     vector<int> vecIntC;
18     vecIntC.resize(10);
19     set_union(vecIntA.begin(), vecIntA.end(), vecIntB.begin(), vecIntB.end(), vecIntC.begin());
20     for (size_t i = 0; i<vecIntC.size(); ++i)
21     {
22         cout<<vecIntC[i]<<" ";
23     }
24     //1 3 5 6 7 8 9 0 0 0
25     getchar();
26 }
set_union

_OutIt set_intersection(_InIt1 _First1, _InIt1 _Last1,_InIt2 _First2, _InIt2 _Last2, _OutIt _Dest) //二个容器交集赋值给新容器,新容器中仅包含二个容器中相同的部份,resize剩余部份以默认值0填充

 1 inline void set_intersection()
 2 {
 3     vector<int> vecIntA;
 4     vecIntA.push_back(1);
 5     vecIntA.push_back(3);
 6     vecIntA.push_back(5);
 7     vecIntA.push_back(7);
 8     vecIntA.push_back(9);
 9 
10     vector<int> vecIntB;
11     vecIntB.push_back(1);
12     vecIntB.push_back(3);
13     vecIntB.push_back(5);
14     vecIntB.push_back(6);
15     vecIntB.push_back(8);
16 
17     vector<int> vecIntC;
18     vecIntC.resize(10);
19     set_intersection(vecIntA.begin(), vecIntA.end(), vecIntB.begin(), vecIntB.end(), vecIntC.begin());
20     for (size_t i = 0; i<vecIntC.size(); ++i)
21     {
22         cout<<vecIntC[i]<<" ";
23     }
24     //1 3 5 0 0 0 0 0 0 0
25     getchar();
26 }
set_intersection()

_OutIt set_difference(_InIt1 _First1, _InIt1 _Last1,_InIt2 _First2, _InIt2 _Last2,_OutIt _Dest) //二个容器差集赋值给新容器,新容器中仅包含二个容器中不相同部份,resize剩余部份以默认值0填充

 1 inline void set_difference()
 2 {
 3     vector<int> vecIntA;
 4     vecIntA.push_back(1);
 5     vecIntA.push_back(3);
 6     vecIntA.push_back(5);
 7     vecIntA.push_back(7);
 8     vecIntA.push_back(9);
 9 
10     vector<int> vecIntB;
11     vecIntB.push_back(1);
12     vecIntB.push_back(3);
13     vecIntB.push_back(5);
14     vecIntB.push_back(6);
15     vecIntB.push_back(8);
16 
17     vector<int> vecIntC;
18     vecIntC.resize(10);
19     set_difference(vecIntA.begin(), vecIntA.end(), vecIntB.begin(), vecIntB.end(), vecIntC.begin());
20     for (size_t i = 0; i<vecIntC.size(); ++i)
21     {
22         cout<<vecIntC[i]<<" ";
23     }
24     //7 9 0 0 0 0 0 0 0 0
25     getchar();
26 };
set_difference()

 

遍历算法:

_Fn1 for_each(_InIt _First, _InIt _Last, _Fn1 _Func) //根据函数对象进行遍历容器

 1 void ShowOut(const int &item)
 2 {
 3     cout<<item<<" ";
 4 }
 5 inline void _for_each()
 6 {
 7     int nArray[] = {0,1,2,3,4};
 8     vector<int> vecInt(nArray, nArray+5);
 9     for_each(vecInt.begin(), vecInt.end(), ShowOut);
10     
11     getchar();
12 }
for_each()

_OutIt transform(_InIt _First, _InIt _Last,_OutIt _Dest, _Fn1 _Func) //根据函数对象进行遍历容器,可以通过函数对象返回的值对容器中的元素进行修改

 1 int AddOne(const int &item)
 2 {
 3     return item+1;
 4 }
 5 inline void _transform()
 6 {
 7     int nArray[] = {1,3,5,7,9};
 8     vector<int> vecInt(nArray, nArray+5);
 9     transform(vecInt.begin(), vecInt.end(), vecInt.begin(), AddOne);
10     for_each(vecInt.begin(),vecInt.end(),ShowOut);
11     //2 4 6 8 10
12     getchar();
13 };
transform
posted @ 2019-12-28 18:33  SmallOverFllow  阅读(272)  评论(0编辑  收藏  举报