目录
算术生成算法accumulate
算术生成算法file
常用集合算法
常用集合算法
常用集合算法set_difference
算术生成算法accumulate
算术生成算法属于小型算法,使用时包含的头文件为 include <numeric>
accumulate(iterator beg, iterator end, value);
// 计算容器元素累计总和
// beg开始选代器//end 结束选代器
// value 起始值
#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include <functional>
#include <numeric>using namespace std;void test01()
{vector<int>v; for(int i =0;i<=100;i++){v.push_back(i);}//参数三是起始的累加值int n = accumulate(v.begin(),v.end(),0);cout << "累加总和为:"<< n << endl;//5050
}int main()
{test01();return 0;
}
算术生成算法file
fill(iterator beg, iterator end, value);
// 向容器中填充元素
// beg开始选代器
//end结束选代器
// value 填充的值
#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include <functional>
#include <numeric>using namespace std;void myprintf(int val)
{cout << val;
}void test01()
{vector<int>v; v.reaize(10);fill(v.begin(),v.end(),6);for_each(v.begin(),v.end(),myprintf);//6666666666cout << endl;
}int main()
{test01();return 0;
}
常用集合算法
set_intersection(iterator begl, iterator end1, iterator beg2, iterator end2, iterator dest);
//求两个集合的交集
//注意:两个集合必须是有序序列 返回值是交集最后一个元素的位置(迭代器)
//beg1容器1开始迭代器
//end1容器1结束选代器
// beg2容器2开始选代器
//end2容器2结束选代器
//dest目标容器开始选代器
#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include <functional>
#include <numeric>using namespace std;void myprintf(int val)
{cout << val;
}void test01()
{vector<int>v; v.push_back(1);v.push_back(2);v.push_back(3);v.push_back(4);v.push_back(5);v.push_back(6);vector<int>t; t.push_back(2);t.push_back(3);t.push_back(4);t.push_back(5);t.push_back(6);t.push_back(7);vector<int>r;r.resize(t.size());//min(v.size(),t.size());//取小的值返回set_intersection(v.begin(),v.end(),t.begin(),t.end(),r.begin());//输出 2 3 4 5 6 0 剩余空间补零//不想输出零可以将遍历算法的r.end换成集合算法返回的迭代器for_each(r.begin(),r.end(),myprintf);cout << endl;
}int main()
{test01();return 0;
}
编译运行
常用集合算法
set_union(iterator begl, iterator end1, iterator bep2, iterator end2, iterator dest);
//求两个集合的并集// 注意:两个集合必须是有序序列
// beg1容器1开始选代器//end1容器1结束选代器//beg2容器2开始选代器//end2容器2结束选代器// dest目标容器开始迭代器
#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include <functional>
#include <numeric>using namespace std;void myprintf(int val)
{cout << val;
}void test01()
{vector<int>v;v.push_back(1);v.push_back(2);v.push_back(3);v.push_back(4);v.push_back(5);v.push_back(6);vector<int>t;t.push_back(2);t.push_back(3);t.push_back(4);t.push_back(5);t.push_back(6);t.push_back(7);vector<int>r;//两个集合的并集的大小小于等于两个集合总的大小r.resize(v.size()+t.size());set_union(v.begin(),v.end(),t.begin(),t.end(),r.begin());for_each(r.begin(),r.end(),myprintf);cout << endl;
}int main()
{test01();return 0;
}
常用集合算法set_difference
set_difference(iterator begl, iterator end1, iterator beg2, iterator end2, iterator dest);
//求两个集合的差集
//注意:两个集合必须是有序序列
//beg1容器1开始选代器//end1容器1结束选代器
// beg2容器2开始选代器
//end2容器2结束选代器
// dest目标容器开始选代器
#include <iostream>
#include <string.h>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <bitset>
#include <ctime>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include <functional>
#include <numeric>using namespace std;void myprintf(int val)
{cout << val;
}void test01()
{vector<int>v1;v1.push_back(1);v1.push_back(2);v1.push_back(3);v1.push_back(4);v1.push_back(5);v1.push_back(6);vector<int>v2;v2.push_back(2);v2.push_back(3);v2.push_back(4);v2.push_back(5);v2.push_back(6);v2.push_back(7);vector<int>t1; vector<int>t2;t1.resize(v1.size()+v2.size());t2.resize(v1.size()+v2.size());//v1和v2的差集set_difference(v1.begin(),v1.end(),v2.begin(),v2.end(),t1.begin());cout << "v1和v2的差集: "<< endl;for_each(t1.begin(),t1.end(),myprintf);cout << endl;//v2和v1的差集 set_difference(v2.begin(),v2.end(),v1.begin(),v1.end(),t2.begin());cout << "v2和v1的差集: "<< endl;for_each(t2.begin(),t2.end(),myprintf);cout << endl;}int main()
{test01();return 0;
}
编译运行