1.sort
对于被排序的元素,需提供operator<
// sort algorithm example
#include <iostream> // std::cout
#include <algorithm> // std::sort
#include <vector> // std::vector
bool myfunction (int i,int j) { return (i<j); }
struct myclass {
bool operator() (int i,int j) { return (i<j);}
} myobject;
int main () {
int myints[] = {32,71,12,45,26,80,53,33};
std::vector<int> myvector (myints, myints+8); // 32 71 12 45 26 80 53 33
// using default comparison (operator <):
std::sort (myvector.begin(), myvector.begin()+4); //(12 32 45 71)26 80 53 33
// using function as comp
std::sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)
// using object as comp
std::sort (myvector.begin(), myvector.end(), myobject); //(12 26 32 33 45 53 71 80)
// print out content:
std::cout << "myvector contains:";
for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
2.partial_sort
对于[first,last)中的部分元素进行牌序,是的[first,middle)中的元素是有序的,而[middle,last)中的元素顺序市委定义的。
// partial_sort example
#include <iostream> // std::cout
#include <algorithm> // std::partial_sort
#include <vector> // std::vector
bool myfunction (int i,int j) { return (i<j); }
int main () {
int myints[] = {9,8,7,6,5,4,3,2,1};
std::vector<int> myvector (myints, myints+9);
// using default comparison (operator <):
std::partial_sort (myvector.begin(), myvector.begin()+5, myvector.end());
// using function as comp
std::partial_sort (myvector.begin(), myvector.begin()+5, myvector.end(),myfunction);
// print out content:
std::cout << "myvector contains:";
for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
3.binary_search
1.在[first,last)中查找等于val的元素
2.容器中的元素首先要排序
template <class ForwardIterator, class T>
bool binary_search (ForwardIterator first, ForwardIterator last, const T& val)
{
first = std::lower_bound(first,last,val);
return (first!=last && !(val<*first));
}
// binary_search example
#include <iostream> // std::cout
#include <algorithm> // std::binary_search, std::sort
#include <vector> // std::vector
bool myfunction (int i,int j) { return (i<j); }
int main () {
int myints[] = {1,2,3,4,5,4,3,2,1};
std::vector<int> v(myints,myints+9); // 1 2 3 4 5 4 3 2 1
// using default comparison:
std::sort (v.begin(), v.end());
std::cout << "looking for a 3... ";
if (std::binary_search (v.begin(), v.end(), 3))
std::cout << "found!\n"; else std::cout << "not found.\n";
// using myfunction as comp:
std::sort (v.begin(), v.end(), myfunction);
std::cout << "looking for a 6... ";
if (std::binary_search (v.begin(), v.end(), 6, myfunction))
std::cout << "found!\n"; else std::cout << "not found.\n";
return 0;
}
4.merge
将排好序的[first1,last1)与[first2,last2)合并到result
template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator merge (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result)
{
while (true) {
if (first1==last1) return std::copy(first2,last2,result);
if (first2==last2) return std::copy(first1,last1,result);
*result++ = (*first2<*first1)? *first2++ : *first1++;
}
}
include <iostream> // std::cout
#include <algorithm> // std::merge, std::sort
#include <vector> // std::vector
int main () {
int first[] = {5,10,15,20,25};
int second[] = {50,40,30,20,10};
std::vector<int> v(10);
std::sort (first,first+5);
std::sort (second,second+5);
std::merge (first,first+5,second,second+5,v.begin());
std::cout << "The resulting vector contains:";
for (std::vector<int>::iterator it=v.begin(); it!=v.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
5.includes
template <class InputIterator1, class InputIterator2>
bool includes (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2)
{
while (first2!=last2) {
if ( (first1==last1) || (*first2<*first1) ) return false;
if (!(*first1<*first2)) ++first2;
++first1;
}
return true;
}
1.判别[first1,last1)是否包含[fist2,last2)
2.[first1,last1)和[first2,last2)都是排好序的
// includes algorithm example
#include <iostream> // std::cout
#include <algorithm> // std::includes, std::sort
bool myfunction (int i, int j) { return i<j; }
int main () {
int container[] = {5,10,15,20,25,30,35,40,45,50};
int continent[] = {40,30,20,10};
std::sort (container,container+10);
std::sort (continent,continent+4);
// using default comparison:
if ( std::includes(container,container+10,continent,continent+4) )
std::cout << "container includes continent!\n";
// using myfunction as comp:
if ( std::includes(container,container+10,continent,continent+4, myfunction) )
std::cout << "container includes continent!\n";
return 0;
}
6.make_heap
1.将区间[first,last)转换成一个堆
2.堆结构采用max_heap,维持平衡二叉树
7.push_heap
1.向堆中添加一个元素,该算法的前提是假设[fist,last-1)已经是个堆,被添加到堆的元素为*(last-1)
2.堆结构采用max_heap,维持平衡二叉树
8.pop_headp
1.向堆中添加一个元素,该算法的前提是假设[fist,last)已经是个堆,被弹出的堆元素为根顶元素
2.堆结构采用max_heap,维持平衡二叉树
9.sort_heap
1.将堆[first,last)中的元素进行排序
2.堆结构采用max_heap,维持平衡二叉树
3.sort的做法是不断pop_heap,因为每次pop都可以获得堆中的最大元素
// range heap example
#include <iostream> // std::cout
#include <algorithm> // std::make_heap, std::pop_heap, std::push_heap, std::sort_heap
#include <vector> // std::vector
int main () {
int myints[] = {10,20,30,5,15};
std::vector<int> v(myints,myints+5);
std::make_heap (v.begin(),v.end());
std::cout << "initial max heap : " << v.front() << '\n';
std::pop_heap (v.begin(),v.end()); v.pop_back();
std::cout << "max heap after pop : " << v.front() << '\n';
v.push_back(99); std::push_heap (v.begin(),v.end());
std::cout << "max heap after push: " << v.front() << '\n';
std::sort_heap (v.begin(),v.end());
std::cout << "final sorted range :";
for (unsigned i=0; i<v.size(); i++)
std::cout << ' ' << v[i];
std::cout << '\n';
return 0;
}