本文主要涉及拷贝和替换算法,算术生成算法,常用集合算法
需要遍历,查找,排序算法的可以看我的上一篇文章
1、拷贝和替换算法
(1)copy
容器内指定范围的元素拷贝到另一容器中
函数原型
copy(iterator beg,iterator end,iterator dest);
beg 开始迭代器
end 结束迭代器
dest 目标起始迭代器
实例
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
void print(int val)
{
cout<<val<<" ";
}
void test01()
{
vector<int>v1;
for(int i=0;i<10;i++)
{
v1.push_back(i);
}
vector<int>v2;
v2.resize(v1.size());
copy(v1.begin(),v1.end(),v2.begin());
for_each(v2.begin(),v2.end(),print);
cout<<endl;
}
int main()
{
test01();
}
(2)replace
将容器内指定范围的旧元素修改为新元素
函数原型
replace(iterator beg,iterator end,oldvalue,newvalue);
将区间内旧元素替换成新元素
beg开始迭代器
end 结束迭代器
oldvalue 旧元素
newwalue 新元素
实例
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
class MyPrint
{
public:
bool operator()(int val)
{
cout<<val<<" ";
}
};
void test01()
{
vector<int>v1;
v1.push_back(1);
v1.push_back(5);
v1.push_back(3);
v1.push_back(5);
v1.push_back(5);
cout<<"替换前:"<<endl;
for_each(v1.begin(),v1.end(),MyPrint());
cout<<endl;
replace(v1.begin(),v1.end(),5,521);
cout<<"替换后:"<<endl;
for_each(v1.begin(),v1.end(),MyPrint());
}
int main()
{
test01();
}
把所有的旧元素都替换为新元素
(3)replace_if
将区间内满足条件的元素,替换成指定元素
函数原型
replace_if(iterator beg,iterator end,_pred,newvalue);
按条件替换元素,满足条件的替换成指定元素
beg开始迭代器
end 结束迭代器
pred谓词
newvalue替换的新元素
实例
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
class MyPrint
{
public:
bool operator()(int val)
{
cout<<val<<" ";
}
};
class Greater
{
public:
bool operator()(int val)
{
return val>3;
}
};
void test01()
{
vector<int>v1;
v1.push_back(1);
v1.push_back(5);
v1.push_back(3);
v1.push_back(5);
v1.push_back(5);
cout<<"替换前:"<<endl;
for_each(v1.begin(),v1.end(),MyPrint());
cout<<endl;
replace_if(v1.begin(),v1.end(),Greater(),1000);
cout<<"替换后:"<<endl;
for_each(v1.begin(),v1.end(),MyPrint());
}
int main()
{
test01();
}
(4)swap
互换两个容器的元素
函数原型
swap(container c1,container c2);
互换两个容器的元素
c1容器1 ,c2容器2
2、算术生成算法
算术生成算法属于小型算法,使用时包含的头文件为
#include <numeric>
(1)accumulate
计算区间内容器元素累计总和
函数原型
accumulate(iterator beg,iterator end,value);
计算容器元素累计总和
beg开始迭代器
end 结束迭代器
value起始累加值
实例
#include<iostream>
using namespace std;
#include<vector>
#include<numeric>
class Greater
{
public:
bool operator()(int val)
{
return val>3;
}
};
void test01()
{
vector<int>v;
for(int i=0;i<100;i++)
{
v.push_back(i+1);
}
int num=accumulate(v.begin(),v.end(),0);
cout<<"num="<<num<<endl;
}
int main()
{
test01();
}
(2)fill
向容器中填充指定的元素
函数原型
fill(iterator beg, iterator end, value);
beg 开始迭代器
end 结束迭代器
value填充的值
实例
#include<iostream>
using namespace std;
#include<vector>
#include<numeric>
#include<algorithm>
class MyPrint
{
public:
bool operator()(int val)
{
cout<<val<<" ";
}
};
void test01()
{
vector<int>v;
v.resize(10);
for_each(v.begin(),v.end(),MyPrint());
fill(v.begin(),v.end(),100);
cout<<endl;
for_each(v.begin(),v.end(),MyPrint());
}
int main()
{
test01();
}
3、常用集合算法
(1)set_intersection
求两个集合的交集
函数原型
set_intersection(iterator beg1,iterator end1,iterator beg2,iterator end2,iterator dest);
两个集合必须是有序序列
beg1 容器1开始迭代器
end1容器1结束迭代器
beg2容器2开始迭代器
end2容器2结束迭代器
dest 目标容器开始迭代器
实例
#include<iostream>
using namespace std;
#include<vector>
#include<numeric>
#include<algorithm>
class MyPrint
{
public:
bool operator()(int val)
{
cout<<val<<" ";
}
};
void test01()
{
vector<int>v1;
vector<int>v2;
for(int i=0;i<10;i++)
{
v1.push_back(i);
v2.push_back(i+5);
}
vector<int>v3;
v3.resize(min(v1.size(),v2.size()));
vector<int>::iterator it=set_intersection(v1.begin(),v1.end(),v2.begin(),v2.end(),v3.begin());
for_each(v3.begin(),it,MyPrint());
}
int main()
{
test01();
}
set_intersection函数返回的是迭代器,用一个迭代器接受,将其作为目标迭代器的结束
(2)set_union
求两个集合的并集
函数原型
set_union(iterator beg1,iterator end1,iterator beg2,iterator end2,iterator dest);
两个集合必须是有序序列
beg1容器1开始迭代器
end1容器1结束迭代器
beg2容器2开始迭代器
end2容器2结束迭代器
dest 目标容器开始迭代器
#include<iostream>
using namespace std;
#include<vector>
#include<numeric>
#include<algorithm>
class MyPrint
{
public:
bool operator()(int val)
{
cout<<val<<" ";
}
};
void test01()
{
vector<int>v1;
vector<int>v2;
for(int i=0;i<10;i++)
{
v1.push_back(i);
v2.push_back(i+5);
}
vector<int>v3;
v3.resize(v1.size()+v2.size());
vector<int>::iterator it=set_union(v1.begin(),v1.end(),v2.begin(),v2.end(),v3.begin());
for_each(v3.begin(),it,MyPrint());
}
int main()
{
test01();
}
(3)set_difference
求两个集合的差集
函数原型
set_difference(iterator beg1,iterator end1,iterator beg2,iterator end2,iterator dest);
两个集合必须是有序序列
beg1容器1开始迭代器
end1 容器1结束迭代器
beg2容器2开始迭代器
end2容器2结束迭代器
dest 目标容器开始迭代器
不过这里有一个前后顺序需要注意
差集:
v1: 0,1,2,3,4,5,6,7,8,9
v2: 5,6,7,8,9,10,11,12,13,14
v1对v2的差集为:0,1,2,3,4,
v2对v1的差集为:10,11,12,13,14
实例
#include<iostream>
using namespace std;
#include<vector>
#include<numeric>
#include<algorithm>
class MyPrint
{
public:
bool operator()(int val)
{
cout<<val<<" ";
}
};
void test01()
{
vector<int>v1;
vector<int>v2;
for(int i=0;i<10;i++)
{
v1.push_back(i);
v2.push_back(i+5);
}
vector<int>v3;
v3.resize(max(v1.size(),v2.size()));
vector<int>::iterator it=set_difference(v1.begin(),v1.end(),v2.begin(),v2.end(),v3.begin());
cout<<"v1对v2的差集为:"<<endl;
for_each(v3.begin(),it,MyPrint());
vector<int>v4;
v4.resize(max(v1.size(),v2.size()));
vector<int>::iterator it2=set_difference(v2.begin(),v2.end(),v1.begin(),v1.end(),v4.begin());
cout<<endl<<"v2对v1的差集为:"<<endl;
for_each(v4.begin(),it2,MyPrint());
}
int main()
{
test01();
}