STL中的常用算法:
之前已经基本介绍了一些 C++ STL
中的常用容器,接下来就是 C++ STL
中的常用算法了;
这次主要介绍 C++ STL
中的常用拷贝和替换算法、常用算术生成算法、常用集合算法;
常用拷贝和替换算法:
1.copy()
算法:将容器内指定范围的元素拷贝到另一个容器中;
函数原型:copy(iterator begin,iterator end,iterator dest);
begin
是源容器的起始迭代器,end
是源容器的结束迭代器,dest
是目标容器的起始迭代器;
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void myPrint(int value)
{
cout << value << " ";
}
void test_1() //常用拷贝和替换算法 copy()
{
int i = 0;
vector<int> v1; //源容器
for(i=0;i<10;i++)
{
v1.push_back(i+1);
}
vector<int> v2; //目标容器
//目标容器提前分配好内存空间
v2.resize(v1.size());
//将容器v1里面的元素拷贝到容器v2中
copy(v1.begin(),v1.end(),v2.begin());
for_each(v2.begin(),v2.end(),myPrint);
cout << endl;
}
int main()
{
test_1();
system("pause");
return 0;
}
2.replace()
算法:将容器内指定的范围中的旧元素都替换为新元素;
函数原型:replace(iterator begin,iterator end,old_value,new_value);
begin
是起始迭代器,end
是结束迭代器,old_value
是旧元素(要被替换掉的元素),new_value
是新元素;
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class MyPrint
{
public:
void operator()(int value)
{
cout << value << " ";
}
};
void test_1() //常用拷贝和替换算法 replace()
{
vector<int> v1;
v1.push_back(10);
v1.push_back(30);
v1.push_back(40);
v1.push_back(20);
v1.push_back(50);
v1.push_back(10);
v1.push_back(30);
v1.push_back(20);
v1.push_back(10);
cout << "替换前:" << endl;
for_each(v1.begin(),v1.end(),MyPrint());
cout << endl;
//把容器v1中的全部的10替换为0
replace(v1.begin(),v1.end(),10,0);
cout << "替换后:" << endl;
for_each(v1.begin(),v1.end(),MyPrint());
cout << endl;
}
int main()
{
test_1();
system("pause");
return 0;
}
3.replace_if()
算法:根据条件将指定范围的满足条件的元素替换为指定的新元素;
函数原型:replace_if(iterator begin,iterator end,_Pred,new_value);
begin
是起始迭代器,end
是结束迭代器,_Pred
是谓词,new_value
是新元素;
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void myPrint(int value)
{
cout << value << " ";
}
class GreaterThirty
{
public:
bool operator()(int value)
{
if(value>30)
{
return true;
}
else
{
return false;
}
}
};
void test_1() //常用拷贝和替换算法 replace_if()
{
vector<int> v1;
v1.push_back(10);
v1.push_back(40);
v1.push_back(20);
v1.push_back(30);
v1.push_back(50);
v1.push_back(20);
v1.push_back(40);
v1.push_back(50);
v1.push_back(30);
cout << "替换前:" << endl;
for_each(v1.begin(),v1.end(),myPrint);
cout << endl;
//将大于30的元素替换为0
replace_if(v1.begin(),v1.end(),GreaterThirty(),0);
cout << "替换后:" << endl;
for_each(v1.begin(),v1.end(),myPrint);
cout << endl;
}
int main()
{
test_1();
system("pause");
return 0;
}
4.swap()
算法:互换两个容器的元素;
函数原型:swap(container c1,container c2);
c1
是容器一,c2
是容器二,需要注意的是,这里发生互换的两个容器必须是同种类型的;
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class MyPrint
{
public:
void operator()(int value)
{
cout << value << " ";
}
};
void test_1() //常用拷贝和替换算法 swap()
{
int i = 0;
vector<int> v1; //容器v1
vector<int> v2; //容器v2
for(i=0;i<10;i++)
{
v1.push_back(i+1); //1 2 3 4 5 6 7 8 9 10
v2.push_back(i+101); //101 102 103 104 105 106 107 108 109 110
}
cout << "互换前:" << endl;
for_each(v1.begin(),v1.end(),MyPrint());
cout << endl;
for_each(v2.begin(),v2.end(),MyPrint());
cout << endl;
cout << "——————————————————————" << endl;
//将容器v1和容器v2进行互换
swap(v1,v2);
cout << "互换后:" << endl;
for_each(v1.begin(),v1.end(),MyPrint());
cout << endl;
for_each(v2.begin(),v2.end(),MyPrint());
cout << endl;
}
int main()
{
test_1();
system("pause");
return 0;
}
常用算术生成算法:算术生成算法属于小型的算法,使用时需要包含它的头文件 #include <numeric>
;
1.accumulate()
算法:计算指定范围内的元素的累积总和;
函数原型:accumulate(iterator begin,iterator end,value);
begin
是起始迭代器,end
是结束迭代器,value
是起始累加值,该算法返回该范围内的元素总和;
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
void test_1() //常用算术生成算法 accumulate()
{
int i = 0;
vector<int> v1;
for(i=0;i<=100;i++)
{
v1.push_back(i); //1~101
}
//计算容器v1所有元素的总和(起始累加值为0)
int total_1 = accumulate(v1.begin(),v1.end(),0); //5050
//计算容器v1所有元素的总和(起始累加值为1000)
int total_2 = accumulate(v1.begin(),v1.end(),1000); //6050
cout << "容器内所有元素总和为:" << total_1 << endl;
cout << "容器内所有元素总和为:" << total_2 << endl;
}
int main()
{
test_1();
system("pause");
return 0;
}
2.fill()
算法:向指定范围内填充指定元素;
函数原型:fill(iterator begin,iterator end,value);
begin
是起始迭代器,end
是结束迭代器,value
是指定的用于填充的元素;
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
void myPrint(int value)
{
cout << value << " ";
}
void test_1() //常用算术生成算法 fill()
{
vector<int> v1;
//指定容器v1的大小为10,默认会以0进行填充
v1.resize(10);
cout << "填充值修改前:" << endl;
for_each(v1.begin(),v1.end(),myPrint);
cout << endl;
//后期修改填充值,用10来填充容器v1
fill(v1.begin(),v1.end(),10);
cout << "填充值修改后:" << endl;
for_each(v1.begin(),v1.end(),myPrint);
cout << endl;
}
int main()
{
test_1();
system("pause");
return 0;
}
常用集合算法:
1.set_intersection()
算法:求两个集合的交集;
函数原型:set_intersection(iterator begin_1,iterator end_1,iterator begin_2,iterator end_2,iterator dest);
begin_1
是容器 1
的起始迭代器,end_1
是容器 1
的结束迭代器,begin_2
是容器 2
的起始迭代器,end_2
是容器 2
的结束迭代
器,dest
是目标容器的起始迭代器,该算法会返回交集中最后一个元素的迭代器;
注意:
1.要求交集的两个容器必须为有序序列,且排序规则一致;
2.目标容器需要提前分配好内存空间,否则是无法获得两个集合的交集的;
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
class MyPrint
{
public:
void operator()(int value)
{
cout << value << " ";
}
};
void test_1() //常用集合算法 set_intersection()
{
int i = 0;
vector<int> v1; //容器v1
vector<int> v2; //容器v2
//向容器v1和容器v2有序地插入元素
for(i=0;i<10;i++)
{
v1.push_back(i); //0 1 2 3 4 5 6 7 8 9
v2.push_back(i+5); //5 6 7 8 9 10 11 12 13 14
}
vector<int> v_Target; //目标容器
//目标容器需要提前开辟内存空间
//交集中,元素数量最大的情况就是,大集合包含小集合,此时该交集的大小为小集合的大小,因此我们只要开辟到空间为小集合的大小即可,其他的情况下,开辟的大小均小于该情况
v_Target.resize(min(v1.size(),v2.size()));
//求容器v1和容器v2的交集
vector<int>::iterator itEnd =set_intersection(v1.begin(),v1.end(),v2.begin(),v2.end(),v_Target.begin());
//输出:5 6 7 8 9
for_each(v_Target.begin(),itEnd,MyPrint());
cout << endl;
//输出:5 6 7 8 9 0 0 0 0 0
for_each(v_Target.begin(),v_Target.end(),MyPrint());
cout << endl;
}
int main()
{
test_1();
system("pause");
return 0;
}
2.set_union()
算法:求两个集合的并集;
函数原型:set_union(iterator begin_1,iterator end_1,iterator begin_2,iterator end_2,iterator dest);
begin_1
是容器 1
的起始迭代器,end_1
是容器 1
的结束迭代器,begin_2
是容器 2
的起始迭代器,end_2
是容器 2
的结束迭代
器,dest
是目标容器的起始迭代器,该算法会返回并集中最后一个元素的迭代器;
注意:
1.要求并集的两个容器必须为有序序列,且排序规则一致;
2.目标容器需要提前分配好内存空间,否则是无法获得两个集合的并集的;
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
void myPrint(int value)
{
cout << value << " ";
}
void test_1() //常用集合算法 set_union()
{
int i = 0;
vector<int> v1; //容器v1
vector<int> v2; //容器v2
//向容器v1和容器v2有序地插入元素
for(i=0;i<10;i++)
{
v1.push_back(i); //0 1 2 3 4 5 6 7 8 9
v2.push_back(i+5); //5 6 7 8 9 10 11 12 13 14
}
vector<int> v_Target; //目标容器
//目标容器需要提前开辟内存空间
//并集中,元素数量最大的情况就是,两个集合完全不同,此时该并集的大小为原来两个集合的大小的总和,因此我们只要开辟到空间为两个集合的大小的总和即可,其他的情况下,开辟的大小均小于该情况
v_Target.resize(v1.size()+v2.size());
//求容器v1和容器v2的并集
vector<int>::iterator itEnd = set_union(v1.begin(),v1.end(),v2.begin(),v2.end(),v_Target.begin());
//输出:0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
for_each(v_Target.begin(),itEnd,myPrint);
cout << endl;
//输出:0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 0 0 0 0 0
for_each(v_Target.begin(),v_Target.end(),myPrint);
cout << endl;
}
int main()
{
test_1();
system("pause");
return 0;
}
3.set_difference
算法:求两个集合的差集;
函数原型:set_difference(iterator begin_1,iterator end_1,iterator begin_2,iterator end_2,iterator dest);
begin_1
是容器 1
的起始迭代器,end_1
是容器 1
的结束迭代器,begin_2
是容器 2
的起始迭代器,end_2
是容器 2
的结束迭代
器,dest
是目标容器的起始迭代器,该算法会返回差集中最后一个元素的迭代器;
注意:
1.要求并集的两个容器必须为有序序列,且排序规则一致;
2.目标容器需要提前分配好内存空间,否则是无法获得两个集合的差集的;
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
//常用集合算法 set_difference()
class MyPrint
{
public:
void operator()(int value)
{
cout << value << " ";
}
};
void test_1() //容器v1和容器v2的差集(v1中除去v1和v2的交集的剩下的元素)
{
int i = 0;
vector<int> v1; //容器v1
vector<int> v2; //容器v2
//向容器v1和容器v2有序地插入元素
for(i=0;i<10;i++)
{
v1.push_back(i); //0 1 2 3 4 5 6 7 8 9
v2.push_back(i+5); //5 6 7 8 9 10 11 12 13 14
}
vector<int> v_Target; //目标容器
//目标容器需要提前开辟内存空间
//差集中,最特殊的情况有两种情况(大集合包含小集合或两个集合完全不同),但差集中元素数量最多的情况是,两个集合完全不同的情况
//此时差集的大小应该设置为两个集合中较大的那个集合的大小,因此我们只要开辟到空间为较大的集合的大小即可,其他的情况下,开辟的大小均小于特殊的情况
v_Target.resize(max(v1.size(),v2.size()));
//求容器v1和容器v2的差集
vector<int>::iterator itEnd = set_difference(v1.begin(),v1.end(),v2.begin(),v2.end(),v_Target.begin());
//输出:0 1 2 3 4
for_each(v_Target.begin(),itEnd,MyPrint());
cout << endl;
//输出:0 1 2 3 4 0 0 0 0 0
for_each(v_Target.begin(),v_Target.end(),MyPrint());
cout << endl;
}
void test_2() //容器v2和容器v1的差集(v2中除去v1和v2的交集的剩下的元素)
{
int i = 0;
vector<int> v1; //容器v1
vector<int> v2; //容器v2
//向容器v1和容器v2有序地插入元素
for(i=0;i<10;i++)
{
v1.push_back(i); //0 1 2 3 4 5 6 7 8 9
v2.push_back(i+5); //5 6 7 8 9 10 11 12 13 14
}
vector<int> v_Target; //目标容器
//目标容器需要提前开辟内存空间
//差集中,最特殊的情况有两种情况(大集合包含小集合或两个集合完全不同),但差集中元素数量最多的情况是,两个集合完全不同的情况
//此时差集的大小应该设置为两个集合中较大的那个集合的大小,因此我们只要开辟到空间为较大的集合的大小即可,其他的情况下,开辟的大小均小于特殊的情况
v_Target.resize(max(v1.size(),v2.size()));
//求容器v1和容器v2的差集
vector<int>::iterator itEnd = set_difference(v2.begin(),v2.end(),v1.begin(),v1.end(),v_Target.begin());
//输出:10 11 12 13 14
for_each(v_Target.begin(),itEnd,MyPrint());
cout << endl;
//输出:10 11 12 13 14 0 0 0 0 0
for_each(v_Target.begin(),v_Target.end(),MyPrint());
cout << endl;
}
int main()
{
test_1();
test_2();
system("pause");
return 0;
}
STL
常用算法篇幅较长,我总共将其分为了两个部分,大致介绍了一些常用的遍历算法、查找算法、排序算法、拷贝和替换算法、算术生成算法、集合算法,希望对大家有帮助,共勉!
笔记中有错误的地方,欢迎指出,欢迎大家讨论!