算法之旅,直奔<algorithm>之十九 includes
2013-12-21 15:27
357 查看
includes(vs2010)
这是我学习总结<algorithm>的第二十篇。学习有时候很累,因为不会学习;学习有时候很轻松,因为用适合你的方式。Just do it。
作用
includes 的作用是检测数据A中,是否包含数据B。A,B代表的是数据集合。
原型
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; }
实验
includes 可以检测container中是否包含continent中的所有数据
代码
test.cpp
#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"; system("pause"); return 0; }
相关文章推荐
- 算法之旅,直奔<algorithm>之十四 fill_n
- 算法之旅,直奔<algorithm>之二十三 none_of
- 算法之旅,直奔<algorithm>之十一 equal
- 算法之旅,直奔<algorithm>之八 copy_n
- 算法之旅,直奔<algorithm>之七 copy_if
- 算法之旅,直奔<algorithm>之二十一 max
- 算法之旅,直奔<algorithm>之九 count
- 算法之旅,直奔<algorithm>之十 count_if
- 算法之旅,直奔<algorithm>之十四 fill_n
- 算法之旅,直奔<algorithm>之二十二 sort
- 算法之旅,直奔<algorithm>之十五 find
- 算法之旅,直奔<algorithm>之十七 find_first_of
- 算法之旅,直奔<algorithm>之十五 find
- 算法之旅,直奔<algorithm>之二 adjacent_find
- 算法之旅,直奔<algorithm>之十二 equal_range
- 算法之旅,直奔<algorithm>之十三 fill
- 算法之旅,直奔<algorithm>之三 any_of
- 算法之旅,直奔<algorithm>之十八 for_each
- 算法之旅,直奔<algorithm>之一 all_of
- 算法之旅,直奔<algorithm>之四 binary_search