常用的算术生成算法
accumlate()函数
对指定范围内元素求和,然后再加上一个有 valuelnit 指定的初始值。
需要引用头文件:#include <numeric>
accumulate(begin, end, value_int);
fill()函数
将输入值赋给只等范围元素
fill(begin, end, value);
vector<int> zploo; zploo.push_back(1); zploo.push_back(2); zploo.push_back(3); fill(zploo.begin(), zploo.end(), 8); for_each(zploo.begin(), zploo.end(), showInt); cout<<endl; //输出结果为: 8 8 8
算术生成算法练习演示代码:
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
using namespace std;
void show(int &value)
{
cout << value << " ";
}
void test_accumualte()
{
vector<int> vec;
for (int i = 0; i < 10; i++) {
vec.push_back(i + 1);
}
int sum = accumulate(vec.begin(), vec.end(), 100);
cout << sum << endl;
}
void test_fill()
{
vector<int> vec;
for (int i = 0; i < 10; i++) {
vec.push_back(i + 1);
}
fill(vec.begin(), vec.end(), 0);
for_each(vec.begin(), vec.end(), show);
cout << endl;
}
int main(void)
{
//test_accumualte();
test_fill();
return 0;
}
常用的集合算法
set_union()函数
构造一个有序序列
包含两个有序序列的并集
set_union(beginA, endA, beginB, endB, beginC);
set_intersection()函数
构造一个有序序列
包含两个有序序列的交集
set_intersection(beginA, endA, beginB, endB beginC);
set_difference()函数
构造一个有序序列
保留第一个序列中存在,第二个序列中不存在的元素(差集)。
set_different(beginA, endA, beginB endB, beginC);
集合算法练习演示代码:
#include <iostream>
#include <algorithm>
#include <numeric>
#include <functional>
#include <vector>
#include <set>
using namespace std;
void show(const int &value)
{
cout << value << " ";
}
void test_set_union()
{
//并集
set<int, less<int>> s1;
set<int, less<int>> s2;
for (int i = 0; i < 10; i++) {
s1.insert(rand() % 10);
s2.insert(rand() % 20);
}
cout << "set1:";
for_each(s1.begin(), s1.end(), show);
cout << endl;
cout << "set2:";
for_each(s2.begin(), s2.end(), show);
cout << endl;
vector<int> vec;
vec.resize(s1.size() + s2.size());
//集合算法的返回值 是 目标容器最后一个元素的下一个位置的迭代器
vector<int>::iterator it = set_union(s1.begin(), s1.end(), s2.begin(), s2.end(), vec.begin());
vec.resize(it - vec.begin());
cout << "vec:";
for_each(vec.begin(), vec.end(), show);
cout << endl;
}
void test_set_intersection()
{
//交集
set<int, less<int>> s1;
set<int, less<int>> s2;
for (int i = 0; i < 10; i++) {
s1.insert(rand() % 10);
s2.insert(rand() % 20);
}
cout << "set1:";
for_each(s1.begin(), s1.end(), show);
cout << endl;
cout << "set2:";
for_each(s2.begin(), s2.end(), show);
cout << endl;
vector<int> vec;
vec.resize(min(s1.size(), s2.size()));
vector<int>::iterator it = set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), vec.begin());
vec.resize(it - vec.begin());
cout << "vec:";
for_each(vec.begin(), vec.end(), show);
cout << endl;
}
void test_set_difference()
{
//差集 s1 差 s2
set<int, less<int>> s1;
set<int, less<int>> s2;
for (int i = 0; i < 10; i++) {
s1.insert(rand() % 10);
s2.insert(rand() % 20);
}
cout << "set1:";
for_each(s1.begin(), s1.end(), show);
cout << endl;
cout << "set2:";
for_each(s2.begin(), s2.end(), show);
cout << endl;
vector<int> vec;
vec.resize(s1.size());
vector<int>::iterator it = set_difference(s1.begin(), s1.end(), s2.begin(), s2.end(), vec.begin());
vec.resize(it - vec.begin());
cout << "vec:";
for_each(vec.begin(), vec.end(), show);
cout << endl;
int cnt = count_if(s1.begin(), s1.end(), bind2nd(less<int>(), 3));
}
int main(void)
{
//test_set_union();
//test_set_intersection();
test_set_difference();
return 0;
}