一、常用拷贝和替换算法
1.copy

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void printVector(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(), printVector);
cout << endl;
}
int main()
{
test01();
return 0;
}
2.replace

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void printVector(int val)
{
cout << val << " ";
}
void test01()
{
vector<int>v1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
replace(v1.begin(), v1.end(), 5, 10);
for_each(v1.begin(), v1.end(), printVector);
cout << endl;
}
int main()
{
test01();
return 0;
}
3.replace_if

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class MyPrint
{
public:
void operator()(int val)
{
cout << val << " ";
}
};
class Greater5
{
public:
bool operator()(int val)
{
return val >= 5;
}
};
void test01()
{
vector<int>v1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
for_each(v1.begin(), v1.end(), MyPrint());
cout << endl;
replace_if(v1.begin(), v1.end(), Greater5(), 10);
for_each(v1.begin(), v1.end(), MyPrint());
cout << endl;
}
int main()
{
test01();
return 0;
}
4.swap

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class MyPrint
{
public:
void 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 + 100);
}
for_each(v1.begin(), v1.end(), MyPrint());
cout << endl;
for_each(v2.begin(), v2.end(), MyPrint());
cout << endl;
swap(v1, v2);
for_each(v1.begin(), v1.end(), MyPrint());
cout << endl;
for_each(v2.begin(), v2.end(), MyPrint());
cout << endl;
}
int main()
{
test01();
return 0;
}
二、常用算数生成算法
1.accumulate

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class MyPrint
{
public:
void operator()(int val)
{
cout << val << " ";
}
};
void test01()
{
vector<int>v1;
vector<int>v2;
for (int i = 0; i <= 100; i++)
{
v1.push_back(i);
}
int total = accumulate(v1.begin(), v1.end(), 1000);//5050+1000
//for_each(v1.begin(), v1.end(), MyPrint());
cout << total << endl;
}
int main()
{
test01();
return 0;
}
2.fill

#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
class MyPrint
{
public:
void operator()(int val)
{
cout << val << " ";
}
};
void test01()
{
vector<int>v1;
v1.resize(10);
for_each(v1.begin(), v1.end(), MyPrint());
cout << endl;
fill(v1.begin(), v1.end(), 10);
for_each(v1.begin(), v1.end(), MyPrint());
}
int main()
{
test01();
return 0;
}
三、常用集合算法
1.set_intersection

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void MyPrint(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 itEnd = set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
for_each(v3.begin(), itEnd, MyPrint);
cout << endl;
}
int main()
{
test01();
return 0;
}
2.set_union

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void MyPrint(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 itEnd = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
for_each(v3.begin(), itEnd, MyPrint);
cout << endl;
}
int main()
{
test01();
return 0;
}
3.set_difference

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void MyPrint(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 itEnd = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
for_each(v3.begin(), itEnd, MyPrint);
cout << endl;
vector<int>::iterator itEnd1 = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), v3.begin());
for_each(v3.begin(), itEnd1, MyPrint);
cout << endl;
}
int main()
{
test01();
return 0;
}