首页 > 代码库 > STL-算法

STL-算法

#include <algorithm>

1. max_element(v.begin(), v.end());

2. min_element(v.begin(), v.end());

3. find(v.begin(), v.end(), 3);

4. sort(v.begin(), v.end());

5. reverse(pos, v.end();

 1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 using namespace std; 5  6 int main() { 7     vector<int> v; 8     vector<int>::iterator pos; 9     10     for(int i = 6; i >= 1; i--)11         v.push_back(i);12 13     pos = max_element(v.begin(), v.end());14     cout << "the max element is: " << *pos << endl;15 16     pos = min_element(v.begin(), v.end());17     cout << "the min element is: " << *pos << endl;18     19     sort(v.begin(), v.end());20 21     pos = find(v.begin(), v.end(), 3);22 23     reverse(pos, v.end());24 25     for(pos = v.begin(); pos != v.end(); pos++)26         cout << *pos << " ";27 28 29     return 0;30 }

输出:

$ ./a.exethe max element is: 6the min element is: 11 2 3 6 5 4