02 算法

2.1 非操作类算法

  • sort(first_iterator, last_iterator)

  • reverse(first_iterator, last_iterator)

  • *max_element(first_iterator, last_iterator)

  • *min_element(first_iterator, last_iterator)

  • accumulate(first_iterator, last_iterator, initial value of sum) – Does the summation of vector elements

#include <algorithm>
#include <iostream>
#include <vector>
#include <numeric> //For accumulate operation
using namespace std;
int main()
{
vector<int> vect = {10, 20, 5, 23, 42, 15};
cout << "Vector is: ";
for (int i = 0; i < vect.size(); i++)
cout << vect[i] << " ";
cout << endl;
sort(vect.begin(), vect.end());
cout << "Vector after sorting is: ";
for (int i = 0; i < vect.size(); i++)
cout << vect[i] << " ";
cout << endl;
reverse(vect.begin(), vect.end());
cout << "Vector after reversing is: ";
for (int i = 0; i < vect.size(); i++)
cout << vect[i] << " ";
cout << endl;
cout << "Maximum element of vector is: ";
cout << *max_element(vect.begin(), vect.end());
cout << endl;
cout << "Minmum element of vector is: ";
cout << *min_element(vect.begin(), vect.end());
cout << endl;
cout << "The summation of vector elements is: ";
cout << accumulate(vect.begin(), vect.end(), 0);
cout << endl;
return 0;
}
  • count(first_iterator, last_iterator,x)

  • find(first_iterator, last_iterator, x)

  • binary_search(first_iterator, last_iterator, x)

  • lower_bound(first_iterator, last_iterator, x) – 返回>=x的第一个元素的位置

  • upper_bound(first_iterator, last_iterator, x) – 返回>x的第一个元素的位置