首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

看不懂一个泛型算法,大家帮帮小弟我

2013-08-14 
看不懂一个泛型算法,大家帮帮我#include iostream// std::cout#include algorithm// std::mismatch#in

看不懂一个泛型算法,大家帮帮我

#include <iostream>     // std::cout
#include <algorithm>    // std::mismatch
#include <vector>       // std::vector
#include <utility>      // std::pair

bool mypredicate (int i, int j) {
  return (i==j);
}

int main () {
  std::vector<int> myvector;
  for (int i=1; i<6; i++) myvector.push_back (i*10); // myvector: 10 20 30 40 50

  int myints[] = {10,20,80,320,1024};                //   myints: 10 20 80 320 1024

  std::pair<std::vector<int>::iterator,int*> mypair;

  // using default comparison:
  mypair = std::mismatch (myvector.begin(), myvector.end(), myints);
  std::cout << "First mismatching elements: " << *mypair.first;
  std::cout << " and " << *mypair.second << '\n';

  ++mypair.first; ++mypair.second;

  // using predicate comparison:
  mypair = std::mismatch (mypair.first, myvector.end(), mypair.second, mypredicate);
  std::cout << "Second mismatching elements: " << *mypair.first;
  std::cout << " and " << *mypair.second << '\n';

  return 0;
}

 mypair = std::mismatch (mypair.first, myvector.end(), mypair.second, mypredicate);这段代码我看不懂请大家解释一下,谓词函数不是比较相等的吗,怎么出来2个不相等的,想不通啊
[解决办法]
等价于

template <class InputIterator1, class InputIterator2, class BinaryPredicate>
  pair<InputIterator1, InputIterator2>
    mismatch (InputIterator1 first1, InputIterator1 last1,


              InputIterator2 first2, BinaryPredicate pred)
{
  while ( (first1!=last1) && pred(*first1,*first2) )
  { ++first1; ++first2; }
  return std::make_pair(first1,first2);
}

热点排行