代码如下:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;void myPrint(int val)
{cout << val << " ";
}void test01()
{vector<int>v;v.push_back(10);v.push_back(30);v.push_back(50);v.push_back(20);v.push_back(40);v.push_back(60);sort(v.begin(), v.end());for_each(v.begin(), v.end(), myPrint);cout << endl;sort(v.begin(), v.end(), greater<int>());for_each(v.begin(), v.end(), myPrint);cout << endl;
}int main()
{test01();return 0;
}
测试结果:
总结:
代码如下:
#include <iostream>
#include <algorithm>
#include <vector>
#include <ctime>
using namespace std;void myPrint(int val)
{cout << val << " ";
}void test01()
{srand((unsigned int)time(nullptr));vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}for_each(v.begin(), v.end(), myPrint);cout << endl;random_shuffle(v.begin(), v.end());for_each(v.begin(), v.end(), myPrint);cout << endl;
}int main()
{test01();return 0;
}
测试结果:
总结:
代码如下:
#include <iostream>
#include <algorithm>
#include <vector>
#include <ctime>
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 + 1);}vector<int>vTarget;vTarget.resize(v1.size() + v2.size());merge(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());for_each(vTarget.begin(), vTarget.end(), myPrint);cout << endl;
}int main()
{test01();return 0;
}
测试结果:
总结:
代码如下:
#include <iostream>
#include <algorithm>
#include <vector>
#include <ctime>
using namespace std;void myPrint(int val)
{cout << val << " ";
}void test01()
{vector<int>v;v.push_back(10);v.push_back(30);v.push_back(40);v.push_back(50);v.push_back(60);for_each(v.begin(), v.end(), myPrint);cout << endl;reverse(v.begin(), v.end());cout << "------------------------------" << endl;for_each(v.begin(), v.end(), myPrint);cout << endl;
}int main()
{test01();return 0;
}
测试结果: