排序算法和拷贝替换的运用
#include <ctime>
#include <vector>
#include <algorithm>
#include <functional>
class Myprint
{
public:
void operator()(int val)
{
cout << val << " ";
}
};
void test5()
{
vector<int> v;
v.push_back(10);
v.push_back(100);
v.push_back(30);
v.push_back(40);
for_each(v.begin(), v.end(), Myprint());
cout << endl;
sort(v.begin(), v.end(), less<int>());
for_each(v.begin(), v.end(), Myprint());
cout << endl;
random_shuffle(v.begin(), v.end());
for_each(v.begin(), v.end(), Myprint());
}
class MyPrint
{
public:
void operator()(int val)
{
cout << val << " ";
}
};
class Less100
{
public:
bool operator()(int val)
{
return val < 100;
}
};
void test6()
{
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i + 10);
}
vector<int> vtarget;
vtarget.resize(v.size());
copy(v.begin(), v.begin() + 5, vtarget.begin());
for_each(vtarget.begin(), vtarget.end(), MyPrint());
cout << endl;
replace(vtarget.begin(), vtarget.end(), 0, 100);
for_each(vtarget.begin(), vtarget.end(), MyPrint());
cout << endl;
replace_if(vtarget.begin(), vtarget.end(), Less100(), 2000);
for_each(vtarget.begin(), vtarget.end(), MyPrint());
cout << endl;
}
int main()
{
test6();
system("pause");
return 0;
}