概述:不变序列算法,参见http://www.cplusplus.com/reference/algorithm/

  1. /*
  2. std::for_each
  3. template <class InputIterator, class Function>
  4. Function for_each (InputIterator first, InputIterator last, Function fn);
  5.  
  6. Apply function to range
  7. Applies function fn to each of the elements in the range [first,last).
  8. [对[first, last)中的所有元素调用函数fn]
  9. The behavior of this template function is equivalent to:
  10. [该模板函数的行为与以下操作等价:]
  11. template<class InputIterator, class Function>
  12. Function for_each(InputIterator first, InputIterator last, Function fn)
  13. {
  14. while (first!=last) {
  15. fn (*first);
  16. ++first;
  17. }
  18. return fn; // or, since C++11: return move(fn);
  19. }
  20. */
  21.  
  22. #include <iostream>
  23. #include <vector>
  24. #include <algorithm>
  25.  
  26. class myclass
  27. {
  28. public:
  29. void operator() (int i)
  30. {
  31. std::cout<<' '<<i;
  32. }
  33. };
  34.  
  35. void myfunctoin (int i)
  36. {
  37. std::cout<<' '<<i;
  38. }
  39.  
  40. int main()
  41. {
  42. std::vector<int> myvec;
  43. std::vector<int>::iterator it;
  44.  
  45. for(int i=; i<; i++)
  46. myvec.push_back(i+);
  47.  
  48. std::cout<<"myvec contains:";
  49. for_each(myvec.begin(), myvec.end(), myfunctoin);
  50. std::cout<<'\n';
  51.  
  52. //or
  53. myclass obj;
  54. std::cout<<"myvec contains:";
  55. for_each(myvec.begin(), myvec.end(), obj);
  56. std::cout<<'\n';
  57.  
  58. system("pause");
  59. return ;
  60. }
  1. /*
  2. std::find
  3. template <class InputIterator, class T>
  4. InputIterator find (InputIterator first, InputIterator last, const T& val);
  5.  
  6. Find value in range
  7. Returns an iterator to the first element in the range [first,last) that compares equal to val. If no such element is found, the function returns last.
  8. [返回[first, last)中第一个指向val的迭代器,如果没有找到则返回last]
  9. The function uses operator== to compare the individual elements to val.
  10. [该函数使用operator==来比较元素元素是否为val]
  11. The behavior of this function template is equivalent to:
  12. [该模板函数的行为与以下操作等价:]
  13. template<class InputIterator, class T>
  14. InputIterator find (InputIterator first, InputIterator last, const T& val)
  15. {
  16. while(first != last){
  17. if(*first == val) return first;
  18. ++first;
  19. }
  20. return last;
  21. }
  22. */
  23.  
  24. #include <iostream>
  25. #include <vector>
  26. #include <algorithm>
  27.  
  28. int main()
  29. {
  30. // using std::find with array and pointer.
  31. int myints[] = {, , , };
  32. int *p;
  33.  
  34. p = std::find(myints, myints+, );
  35. if(p != myints+)
  36. std::cout<<"Element found in myints: "<<*p<<'\n';
  37. else
  38. std::cout<<"Element not found in myints\n";
  39.  
  40. // using std::find with vector and iterator.
  41. std::vector<int> myvector (myints, myints+);
  42. std::vector<int>::iterator it;
  43.  
  44. it = std::find(myvector.begin(), myvector.end(), );
  45. if(it != myvector.end())
  46. std::cout<<"Element found in myints: "<<*it<<'\n';
  47. else
  48. std::cout<<"Element not found in myints\n";
  49.  
  50. system("pause");
  51. return ;
  52. }
  1. /*
  2. std::find_if
  3. template<class InputIterator, class UnaryPredicate>
  4. InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred);
  5.  
  6. Find element in range
  7. Returns an iterator to the first element in the range [first,last) for which pred returns true. If no such element is found, the function returns last.
  8. [返回[first, last)中第一个使得pred返回真的迭代器,如果没有找到,则返回last]
  9. The behavior of this function template is equivalent to:
  10. [该函数模板的行为等价于以下操作:]
  11. template <class InputIterator, class UnaryPredicate>
  12. InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred)
  13. {
  14. while(first != last){
  15. if(pred(*first)) return first;
  16. ++first;
  17. }
  18. return last;
  19. }
  20. */
  21.  
  22. #include <iostream>
  23. #include <algorithm>
  24. #include <vector>
  25.  
  26. bool IsOdd (int i)
  27. {
  28. return ((i%) == );
  29. }
  30.  
  31. int main()
  32. {
  33. std::vector<int> myvector;
  34.  
  35. myvector.push_back();
  36. myvector.push_back();
  37. myvector.push_back();
  38. myvector.push_back();
  39.  
  40. std::vector<int>::iterator it = std::find_if(myvector.begin(), myvector.end(), IsOdd);
  41.  
  42. if(it != myvector.end())
  43. std::cout<<"The first odd value in myvector is "<<*it<<'\n';
  44. else
  45. std::cout<<"There is no odd value in myvector."<<'\n';
  46.  
  47. system("pause");
  48. return ;
  49. }
  1. /*
  2. //std::adjacent_find
  3. template <class FowardIterator>
  4. FowardIterator adjacent_find (FowardIterator first, FowardIterator last);
  5.  
  6. template <class FowardIterator, class BinaryPredicate>
  7. FowardIterator adjacent_find (FowardIterator first, FowardIterator last, BinaryPredicate pred);
  8.  
  9. Find equal adjacent elements in range
  10. Searches the range [first,last) for the first occurrence of two consecutive elements that match, and returns an iterator to the first of these two elements, or last if no such pair is found.
  11. [在[fisrt, last)中寻找第一次匹配成功的两个连续元素,并返回指向其中第一个元素的迭代器,如果没有匹配成功,则返回last]
  12. Two elements match if they compare equal using operator== (or using pred, in version (2)).
  13. [默认使用operator==来判断两个元素是否匹配成功,或者使用pred来判断]
  14. The behavior of this function template is equivalent to:
  15. [该函数模板的行为等价于以下操作:]
  16. template <class FowardIterator>
  17. FowardIterator adjacent_find (FowardIterator first, FowardIterator last)
  18. {
  19. if(first != last)
  20. {
  21. FowardIterator next = first;
  22. ++next;
  23. while(next != last){
  24. if(*first == *next) return first;
  25. ++first; ++next;
  26. }
  27. return last;
  28. }
  29. }
  30. */
  31.  
  32. #include <iostream>
  33. #include <algorithm>
  34. #include <vector>
  35.  
  36. bool myfunctoin(int i, int j)
  37. {
  38. return (i == j);
  39. }
  40.  
  41. int main()
  42. {
  43. int myints[] = {, , , , , , , , };
  44. std::vector<int> myvector(myints, myints+);
  45. std::vector<int>::iterator it;
  46.  
  47. it = std::adjacent_find(myvector.begin(), myvector.end());
  48.  
  49. if(it != myvector.end())
  50. std::cout<<"The first pair of repeated elements are "<<*it<<'\n';
  51.  
  52. it = std::adjacent_find(++it, myvector.end(), myfunctoin);
  53.  
  54. if(it != myvector.end())
  55. std::cout<<"The second pair of repeated elements are "<<*it<<'\n';
  56.  
  57. system("pause");
  58. return ;
  59. }
  1. /*
  2. std::find_end
  3. template <class ForwardIterator1, class ForwardIterator2>
  4. ForwardIterator1 find_end (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2);
  5.  
  6. template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
  7. ForwardIterator1 find_end (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
  8.  
  9. Find last subsequence in range
  10. Searches the range [first1,last1) for the last occurrence of the sequence defined by [first2,last2), and returns an iterator to its first element, or last1 if no occurrences are found.
  11. [在序列[first1, last2)中搜索出最后一个与序列[first2, last2)相匹配的子序列,并返回指向搜索到的子序列的第一个元素的迭代器,如果没有搜索到则返回last1]
  12. The elements in both ranges are compared sequentially using operator== (or pred, in version (2)): A subsequence of [first1,last1) is considered a match only when this is true for all the elements of [first2,last2).
  13. [比较两个序列中的元素是通过operator==来进行的(或者使用pred)]
  14. This function returns the last of such occurrences. For an algorithm that returns the first instead, see search.
  15. [该函数按返回的是最后一个匹配的子序列,如果想要得到第一个匹配的子序列,请看search算法]
  16. The behavior of this function template is equivalent to:
  17. [该函数模板的行为等价于以下操作:]
  18. template <class ForwardIterator1, class ForwardIterator2>
  19. ForwardIterator1 find_end (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2)
  20. {
  21. if (first2==last2) return last1; // specified in C++11
  22.  
  23. ForwardIterator1 ret = last1;
  24.  
  25. while(first1 != last1)
  26. {
  27. ForwardIterator1 it1 = first1;
  28. ForwardIterator2 it2 = first2;
  29. while(*it1 == *it2){ // or: while (pred(*it1,*it2)) for version (2)
  30. ++it1; ++it2;
  31. if(it2 == last2) {ret = first1; break;}
  32. if(it1 == last1) return ret;
  33. }
  34. ++first1;
  35. }
  36. return ret;
  37. }
  38. */
  39.  
  40. #include <iostream>
  41. #include <algorithm>
  42. #include <vector>
  43.  
  44. bool myfunctoin(int i, int j){
  45. return (i==j);
  46. }
  47.  
  48. int main()
  49. {
  50. int myints[] = {, , , , , , , , , };
  51. std::vector<int> myvector(myints, myints+);
  52.  
  53. int needle1[] = {, , };
  54.  
  55. // using default comparison.
  56. std::vector<int>::iterator it;
  57. it = std::find_end(myvector.begin(), myvector.end(), needle1, needle1+);
  58.  
  59. if(it != myvector.end())
  60. std::cout<<"needle1 last found at position "<<(it - myvector.begin())<<'\n';
  61.  
  62. int needle2[] = {, , };
  63.  
  64. // using predicate comparison.
  65. it = std::find_end(myvector.begin(), myvector.end(), needle2, needle2+, myfunctoin);
  66.  
  67. if(it != myvector.end())
  68. std::cout<<"needle2 last found at position "<<(it - myvector.begin())<<'\n';
  69.  
  70. system("pause");
  71. return ;
  72. }
  1. /*
  2. std::find_first_of
  3. template <class ForwardIterator1, class ForwardIterator2>
  4. ForwardIterator1 find_first_of (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2);
  5.  
  6. template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
  7. ForwardIterator1 find_first_of (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);
  8.  
  9. Find element from set in range
  10. Returns an iterator to the first element in the range [first1,last1) that matches any of the elements in [first2,last2). If no such element is found, the function returns last1.
  11. [返回指向[first1, last1)中第一个与[first2, last2)中任一元素相匹配的元素的迭代器,如果没有找到则返回last1]]
  12. The elements in [first1,last1) are sequentially compared to each of the values in [first2,last2) using operator== (or pred, in version (2)), until a pair matches.
  13. [元素的比较是通过operator==来进行的(或者通过pred)]
  14. The behavior of this function template is equivalent to:
  15. [该函数模板的行为等价于以下操作:]
  16.  
  17. template <class ForwardIterator1, class ForwardIterator2>
  18. ForwardIterator1 find_first_of (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2)
  19. {
  20. while(first1 != last1){
  21. for(ForwardIterator2 it2 = first2; it2 != last2; ++it2){
  22. if(*it2 == *first1) // or: if (pred(*it,*first)) for version (2)
  23. return first1;
  24. }
  25. ++first1;
  26. }
  27. return last1;
  28. }
  29. */
  30.  
  31. #include <iostream> // std::cout
  32. #include <algorithm> // std::find_first_of
  33. #include <vector> // std::vector
  34. #include <cctype> // std::tolower
  35.  
  36. bool comp_case_insensitive (char c1, char c2) {
  37. return (std::tolower(c1)==std::tolower(c2));
  38. }
  39.  
  40. int main () {
  41. int mychars[] = {'a','b','c','A','B','C'};
  42. std::vector<char> haystack (mychars,mychars+);
  43. std::vector<char>::iterator it;
  44.  
  45. int needle[] = {'A','B','C'};
  46.  
  47. // using default comparison:
  48. it = find_first_of (haystack.begin(), haystack.end(), needle, needle+);
  49.  
  50. if (it!=haystack.end())
  51. std::cout << "The first match is: " << *it << '\n';
  52.  
  53. // using predicate comparison:
  54. it = find_first_of (haystack.begin(), haystack.end(),
  55. needle, needle+, comp_case_insensitive);
  56.  
  57. if (it!=haystack.end())
  58. std::cout << "The first match is: " << *it << '\n';
  59.  
  60. system("pause");
  61. return ;
  62. }
  1. /*
  2. std::equal
  3. template <class InputItertor1, class InputIterator2>
  4. bool equal (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);
  5.  
  6. template <class InputIterator1, class InputIterator2, class BinaryPredicate>
  7. bool equal (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate pred);
  8.  
  9. Test whether the elements in two ranges are equal
  10. [比较两个范围中的元素是否相等]
  11. Compares the elements in the range [first1,last1) with those in the range beginning at first2, and returns true if all of the elements in both ranges match.
  12. [将[first1, last1)中的元素与以first2为起点的相同范围内的元素进行比较,如果两个范围中的元素全部相等则返回真]
  13. The elements are compared using operator== (or pred, in version (2)).
  14. [元素间通过operator==进行比较(或者使用pred)]
  15. The behavior of this function template is equivalent to:
  16. [该函数模板的行为等价于以下操作:]
  17. template <class InputIterator1, class InputIterator2>
  18. bool equal (InputIterator1 first1, InputIterator2 last1, InputIterator2 first2)
  19. {
  20. while(first1 != last1){
  21. if(!(*first1 == *first2)) // or: if (!pred(*first1,*first2)), for version 2
  22. return false;
  23. ++first1; ++first2;
  24. }
  25. return true;
  26. }
  27. */
  28.  
  29. #include <iostream>
  30. #include <algorithm>
  31. #include <vector>
  32.  
  33. bool mypredicate(int i, int j)
  34. {
  35. return (i == j);
  36. }
  37.  
  38. int main()
  39. {
  40. int myints[] = {, , , , };
  41. std::vector<int> myvector(myints, myints+);
  42.  
  43. if(std::equal(myvector.begin(), myvector.end(), myints))
  44. std::cout<<"The contents of both sequences are equal.\n";
  45. else
  46. std::cout<<"The contents of both sequences differ.\n";
  47.  
  48. myvector[] = ;
  49.  
  50. if(std::equal(myvector.begin(), myvector.end(), myints, mypredicate))
  51. std::cout<<"The contents of both sequences are equal.\n";
  52. else
  53. std::cout<<"The contents of both sequences differ.\n";
  54.  
  55. system("pause");
  56. return ;
  57. }
  1. /*
  2. std::mismatch
  3. template <class InputIterator1, class InputIterator2>
  4. pair<InputIterator1, InputIterator2> mismatch (InputIterator1 first1, InputIterator1 last1, InputIterator1 first2);
  5.  
  6. template <class InputIterator1, class InputIterator2, class BinaryPredicate>
  7. pair<InputIterator1, InputIterator2> mismatch (InputIterator1 first1, InputIterator1 last1, InputIterator1 first2, BinaryPredicate pred);
  8.  
  9. Return first position where two ranges differ
  10. Compares the elements in the range [first1,last1) with those in the range beginning at first2, and returns the first element of both sequences that does not match.
  11. [比较[first1, last1)与[fist2, last2)之间的元素,并返回两者第一个不匹配的元素]
  12. The elements are compared using operator== (or pred, in version (2)).
  13. [元素间的比较通过operator==来进行(或者通过pred)]
  14. The function returns a pair of iterators to the first element in each range that does not match.
  15. [该函数的返回值类型是pair,其中的两个参数是指向两个区间中不匹配元素的迭代器]
  16. The behavior of this function template is equivalent to:
  17. [该函数模板的行为等价于以下操作:]
  18.  
  19. template <class InputIterator1, class InputIterator2>
  20. pair<InputIterator1, InputIterator2> mismatch (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2 )
  21. {
  22. while ( (first1!=last1) && (*first1==*first2) ) // or: pred(*first1,*first2), for version 2
  23. { ++first1; ++first2; }
  24. return std::make_pair(first1,first2);
  25. }
  26. */
  27.  
  28. #include <iostream>
  29. #include <algorithm>
  30. #include <vector>
  31. #include <utility> //std::pair
  32.  
  33. bool mypredicate(int i, int j){
  34. return (i==j);
  35. }
  36.  
  37. int main()
  38. {
  39. std::vector<int> myvector;
  40. for(int i=; i<; i++) myvector.push_back(i*);
  41.  
  42. int myints[] = {, , , , };
  43.  
  44. std::pair<std::vector<int>::iterator, int*> mypair;
  45.  
  46. // using default comparison
  47. mypair = std::mismatch(myvector.begin(), myvector.end(), myints);
  48. std::cout<<"First mismatch elements: "<<*mypair.first<<" and "<<*mypair.second<<'\n';
  49.  
  50. // using predicate comparison
  51. mypair = std::mismatch(++mypair.first, myvector.end(), ++mypair.second, mypredicate);
  52. std::cout<<"Second mismatch elements: "<<*mypair.first<<" and "<<*mypair.second<<'\n';
  53.  
  54. system("pause");
  55. return ;
  56. }
  1. /*
  2. template <class ForwardIterator1, class ForwardIterator2>
  3. ForwardIterator1 search (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator1 first2, ForwardIterator1 last2);
  4.  
  5. template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
  6. ForwardIterator1 search (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator1 first2, ForwardIterator1 last2, BinaryPredicate pred);
  7.  
  8. Search range for subsequence
  9. Searches the range [first1,last1) for the first occurrence of the sequence defined by [first2,last2), and returns an iterator to its first element, or last1 if no occurrences are found.
  10. [在[first1, last1)中搜索第一个与序列[first2, last2)相匹配的子序列,并返回指向搜索到的子序列的第一个元素的迭代器,如果没有搜索到则返回last1]
  11. The elements in both ranges are compared sequentially using operator== (or pred, in version (2)): A subsequence of [first1,last1) is considered a match only when this is true for all the elements of [first2,last2).
  12. [元素的比较是通过operator==来进行的(或者通过pred)]
  13. This function returns the first of such occurrences. For an algorithm that returns the last instead, see find_end.
  14. [该函数返回第一个与给定序列匹配的子序列,如果想要返回最后一个,请参看find_end.]
  15. The behavior of this function template is equivalent to:
  16. [该函数模板的行为等价于以下操作:]
  17.  
  18. template <class ForwardIterator1, class ForwardIterator1>
  19. ForwardIterator1 search(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator1 first2, ForwardIterator1 last2)
  20. {
  21. if(first2 == last2) return first1; // specified in C++11
  22.  
  23. while(first1 != last1)
  24. {
  25. ForwardIterator1 it1 = first1;
  26. ForwardIterator2 it2 = first2;
  27. while(*it1 == *it2){ // or pred(*it1, *it2) for verson 2
  28. ++it1; ++it2;
  29. if(it2 == last2) return first1;
  30. if(it1 == last1) return last1;
  31. }
  32. ++first1;
  33. }
  34. return last1;
  35. }
  36. */
  37.  
  38. #include <iostream>
  39. #include <algorithm>
  40. #include <vector>
  41.  
  42. bool mypredicate(int i, int j){
  43. return (i==j);
  44. }
  45.  
  46. int main()
  47. {
  48. std::vector<int> myvector;
  49. for(int i=; i<; i++) myvector.push_back(i*);
  50. std::vector<int>::iterator it;
  51.  
  52. // using default comparison.
  53. int needle1[] = {, , , };
  54. it = std::search(myvector.begin(), myvector.end(), needle1, needle1+);
  55.  
  56. if(it != myvector.end())
  57. std::cout<<"needle1 found at position "<<it-myvector.begin()<<'\n';
  58. else
  59. std::cout<<"needle1 not found.\n";
  60.  
  61. // using predicate comparison.
  62. int needle2[] = {, , };
  63. it = std::search(myvector.begin(), myvector.end(), needle2, needle2+, mypredicate);
  64.  
  65. if(it != myvector.end())
  66. std::cout<<"needle2 found at position "<<it-myvector.begin()<<'\n';
  67. else
  68. std::cout<<"needle2 not found.\n";
  69.  
  70. system("pause");
  71. return ;
  72. }
  1. /*
  2. template <class ForwardIterator, class Size, class T>
  3. ForwardIterator search_n (ForwardIterator first, ForwardIterator last, Size count, const T& val);
  4.  
  5. template <class ForwardIterator, class Size, class T, class BinaryPredicate>
  6. ForwardIterator search_n (ForwardIterator first, ForwardIterator last, Size count, const T& val, BinaryPredicate pred);
  7.  
  8. Search range for elements
  9. Searches the range [first,last) for a sequence of count elements, each comparing equal to val (or for which pred returns true).
  10. [在[first, last)中搜索是否连续出现count次val]
  11. The function returns an iterator to the first of such elements, or last if no such sequence is found.
  12. [该函数返回指向第一个等于val的元素的迭代器,如果没有找到则返回last]
  13. The behavior of this function template is equivalent to:
  14. [该函数模板的行为等价于以下操作:]
  15.  
  16. template<class ForwardIterator, class Size, class T>
  17. ForwardIterator search_n (ForwardIterator first, ForwardIterator last, Size count, const T& val)
  18. {
  19. ForwardIterator it, limit;
  20. Size i;
  21.  
  22. limit=first; std::advance(limit,std::distance(first,last)-count); //注:私认为应该是std::advance(limit, std::distance(first, last)-count+1)
  23.  
  24. while (first!=limit)
  25. {
  26. it = first; i=0;
  27. while (*it==val) // or: while (pred(*it,val)) for the pred version
  28. { ++it; if (++i==count) return first; }
  29. ++first;
  30. }
  31. return last;
  32. }
  33. */
  34.  
  35. #include <iostream> // std::cout
  36. #include <algorithm> // std::search_n
  37. #include <vector> // std::vector
  38.  
  39. bool mypredicate (int i, int j) {
  40. return (i==j);
  41. }
  42.  
  43. int main () {
  44. int myints[]={,,,,,,,};
  45. std::vector<int> myvector (myints,myints+);
  46.  
  47. std::vector<int>::iterator it;
  48.  
  49. // using default comparison:
  50. it = std::search_n (myvector.begin(), myvector.end(), , );
  51.  
  52. if (it!=myvector.end())
  53. std::cout << "two 30s found at position " << (it-myvector.begin()) << '\n';
  54. else
  55. std::cout << "match not found\n";
  56.  
  57. // using predicate comparison:
  58. it = std::search_n (myvector.begin(), myvector.end(), , , mypredicate);
  59.  
  60. if (it!=myvector.end())
  61. std::cout << "two 10s found at position " << int(it-myvector.begin()) << '\n';
  62. else
  63. std::cout << "match not found\n";
  64.  
  65. system("pause");
  66. return ;
  67. }
  1. /*
  2. std::count
  3. template <class InputIterator, class T>
  4. typename iterator_traits<InputIterator>::difference_type count (InputIterator first, InputIterator last, const T& val);
  5.  
  6. Count appearances of value in range
  7. Returns the number of elements in the range [first,last) that compare equal to val.
  8. [返回[first, last)中与val相等的元素的个数]
  9. The function uses operator== to compare the individual elements to val.
  10. [该函数使用operator==来判断元素是否等于val]
  11. The behavior of this function template is equivalent to:
  12. [该函数模板的行为等价于以下操作:]
  13. template <class InputIterator, class T>
  14. typename iterator_traits<InputIterator>::difference_type
  15. count (InputIterator first, InputIterator last, const T& val)
  16. {
  17. typename iterator_traits<InputIterator>::difference_type ret = 0;
  18. while(first != last){
  19. if(*first == val) ++ret;
  20. ++first;
  21. }
  22. return ret;
  23. }
  24. */
  25.  
  26. #include <iostream>
  27. #include <algorithm>
  28. #include <vector>
  29.  
  30. int main()
  31. {
  32. int myints[] = {, , , , , , , };
  33. int mycount = std::count(myints, myints+, );
  34. std::cout<<"10 appears "<<mycount<<" times.\n";
  35.  
  36. std::vector<int> myvector(myints, myints+);
  37. mycount = std::count(myvector.begin(), myvector.end(), );
  38. std::cout<<"20 appears "<<mycount<<" times.\n";
  39.  
  40. system("pause");
  41. return ;
  42. }
  1. /*
  2. template <class InputIterator, class UnaryPredicate>
  3. typename iterator_traits<InputIterator>::difference_type count_if (InputIterator first, InputIterator last, UnaryPredicate pred);
  4.  
  5. Return number of elements in range satisfying condition
  6. [返回区间中满足条件的元素个数]
  7. Returns the number of elements in the range [first,last) for which pred is true.
  8. [返回[first, last)中使得pred返回真的元素个数]
  9. The behavior of this function template is equivalent to:
  10. [该函数模板的行为等价于以下操作:]
  11.  
  12. template <class InputIterator, class UnaryPredicate>
  13. typename iterator_traits<InputIterator>::difference_type count_if (InputIterator first, InputIterator last, UnaryPredicate pred)
  14. {
  15. typename iterator_traits<InputIterator>::difference_type ret = 0;
  16. while(first != last)
  17. {
  18. if(pred(*first)) ++ret;
  19. first++;
  20. }
  21. return ret;
  22. }
  23. */
  24.  
  25. #include <iostream>
  26. #include <algorithm>
  27. #include <map>
  28. #include <string>
  29. #include <utility>
  30.  
  31. class stuRecord
  32. {
  33. public:
  34. class stuInfo
  35. {
  36. public:
  37. void setName(std::string m_name) {name = m_name;}
  38. void setAge(int m_age) {age = m_age;}
  39. void setAddr(std::string m_addr) {addr = m_addr;}
  40.  
  41. std::string getName() const {return name;}
  42. int getAge() const {return age;}
  43. std::string getAddr() const {return addr;}
  44.  
  45. private:
  46. std::string name;
  47. int age;
  48. std::string addr;
  49. };
  50.  
  51. private:
  52. int id;
  53. stuInfo m_stuInfo;
  54.  
  55. public:
  56. stuRecord(int m_id, std::string m_name, int m_age, std::string m_addr)
  57. {
  58. id = m_id;
  59. m_stuInfo.setName(m_name);
  60. m_stuInfo.setAge(m_age);
  61. m_stuInfo.setAddr(m_addr);
  62. }
  63.  
  64. int getId() const {return id;}
  65. stuInfo getStuInfo() const {return m_stuInfo;}
  66. };
  67.  
  68. typedef stuRecord::stuInfo stuRI;
  69.  
  70. bool setRange(std::pair<int, stuRI> stu)
  71. {
  72. return ((stu.second.getAge() > ) && (stu.second.getAge() < ));
  73. }
  74.  
  75. int main()
  76. {
  77. stuRecord stu1(, "张三", , "上海");
  78. stuRecord stu2 = stuRecord(, "李四", , "上海");
  79. stuRecord stu3 = stuRecord(, "王五", , "深圳");
  80. stuRecord stu4 = stuRecord(, "赵六", , "长沙");
  81. stuRecord stu5 = stuRecord(, "孙七", , "广东");
  82.  
  83. std::map<int, stuRI> mymap;
  84. mymap.insert(std::make_pair(stu1.getId(), stu1.getStuInfo()));
  85. mymap.insert(std::make_pair(stu2.getId(), stu2.getStuInfo()));
  86. mymap.insert(std::make_pair(stu3.getId(), stu3.getStuInfo()));
  87. mymap.insert(std::make_pair(stu4.getId(), stu4.getStuInfo()));
  88. mymap.insert(std::make_pair(stu5.getId(), stu5.getStuInfo()));
  89.  
  90. int mycount = std::count_if(mymap.begin(), mymap.end(), setRange);
  91. std::cout<<"The number of students whose age between 20 and 30 is "<<mycount<<'\n';
  92.  
  93. system("pause");
  94. return ;
  95. }

algorithm之不变序列操作的更多相关文章

  1. 【BZOJ-2962】序列操作 线段树 + 区间卷积

    2962: 序列操作 Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 678  Solved: 246[Submit][Status][Discuss] ...

  2. 【BZOJ-1858】序列操作 线段树

    1858: [Scoi2010]序列操作 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1961  Solved: 991[Submit][Status ...

  3. BZOJ 1858: [Scoi2010]序列操作( 线段树 )

    略恶心的线段树...不过只要弄清楚了AC应该不难.... ---------------------------------------------------------------- #inclu ...

  4. [bzoj]2962序列操作

    [bzoj]2962序列操作 标签: 线段树 题目链接 题意 给你一串序列,要你维护三个操作: 1.区间加法 2.区间取相反数 3.区间内任意选k个数相乘的积 题解 第三个操作看起来一脸懵逼啊. 其实 ...

  5. bzoj 2962 序列操作

    2962: 序列操作 Time Limit: 50 Sec  Memory Limit: 256 MB[Submit][Status][Discuss] Description 有一个长度为n的序列, ...

  6. SCOI2010 序列操作

    2421 序列操作 http://codevs.cn/problem/2421/ 2010年省队选拔赛四川   题目描述 Description lxhgww最近收到了一个01序列,序列里面包含了n个 ...

  7. bzoj1858[Scoi2010]序列操作 线段树

    1858: [Scoi2010]序列操作 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 3079  Solved: 1475[Submit][Statu ...

  8. BZOJ_1858_[Scoi2010]序列操作_线段树

    BZOJ_1858_[Scoi2010]序列操作_线段树 Description lxhgww最近收到了一个01序列,序列里面包含了n个数,这些数要么是0,要么是1,现在对于这个序列有五种变换操作和询 ...

  9. BZOJ1500: [NOI2005]维修数列 [splay序列操作]【学习笔记】

    以前写过这道题了,但我把以前的内容删掉了,因为现在感觉没法看 重写! 题意: 维护一个数列,支持插入一段数,删除一段数,修改一段数,翻转一段数,查询区间和,区间最大子序列 splay序列操作裸题 需要 ...

随机推荐

  1. leetcode 110

    110. Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, ...

  2. 【转】Linux模式设计5-位图操作

    通过位图提供的两种状态可以在非常节约内存的情况下表示开关变量,并且同类这类变量可以紧凑而高效的统一进行处理.有很多内核子系统都需要位图的支持,但是不同的情况又需要不同的位图个数,比如SMP系统上的CP ...

  3. [leetcode]_Remove Duplicates from Sorted Array II

    题目:一个有序数组,要求保证数组中的每个元素不能超过2个.  输入:A = [1,1,1,2,2,3]  输出:length = 5, and A is now [1,1,2,2,3] 思路:双指针 ...

  4. 2)main函数在执行前和执行后有哪些操作

    main函数执行之前,主要就是初始化系统相关资源:      1. 设置栈指针      2. 初始化static静态和global全局变量,即data段的内容      3. 将未初始化部分的全局变 ...

  5. PHP实例 表单数据插入数据库及数据提取 用户注册验证

    网站在进行新用户注册时,都会将用户的注册信息存入数据库中,需要的时候再进行提取.今天写了一个简单的实例. 主要完成以下几点功能: (1)用户进行注册,实现密码重复确认,验证码校对功能. (2)注册成功 ...

  6. 黑白棋游戏 (codevs 2743)题解

    [问题描述] 黑白棋游戏的棋盘由4×4方格阵列构成.棋盘的每一方格中放有1枚棋子,共有8枚白棋子和8枚黑棋子.这16枚棋子的每一种放置方案都构成一个游戏状态.在棋盘上拥有1条公共边的2个方格称为相邻方 ...

  7. C# 平时碰见的问题【2】

    问题1 修改命名空间后 .ashx 类型创建失败 [情景] 在调整前后台项目结构的时候,修改了默认命名空间(XXX.Admin 修改成XXX.Web),结果调试的时候发现XXX.Admin.Ajax. ...

  8. Eclipse 4.6 Neon, could not create the java virtual machine

    下了eclipse 4.6,打开报错:could not create the java virtual machine. a fatal exception has occurred. 命令行用 e ...

  9. STM32F0_新建软件工程详细过程

    前言 由于ST公司推出比STM32F1性价比更高的F0芯片,现在市面上F0芯片的占有率也非常高.F0芯片属于M0内核,主频48M(当然,可以超频的,但尽量不要超的太多),资源大小可根据项目需求来选型. ...

  10. 17.python自定义函数

    什么是函数,函数说白了就是将一系列代码封装起来,实现代码的重用. 什么是代码重用? 假设我有这样的需求: 但是我还是觉得太麻烦了,每次想吃饭的时候都要重复这样的步骤.此时,我希望有这样的机器: