在这方面,newfunction的目的是什么?

在这方面,newfunction的目的是什么?,function,c++11,pointers,iterator,find,Function,C++11,Pointers,Iterator,Find,在这方面,newfunction的目的是什么? 为什么它的存在没有任何改变? 有人能帮我吗 #include <iostream> #include <algorithm> #include <vector> bool newfunction (int m, int n) { return (m==n); } int main () { int newints[] = {1,2,3,4,5,1,2,3,4,5}; std::vector<in

在这方面,newfunction的目的是什么? 为什么它的存在没有任何改变?
有人能帮我吗

#include <iostream>
#include <algorithm>
#include <vector>
bool newfunction (int m, int n)
{
  return (m==n);
}
int main ()
{
  int newints[] = {1,2,3,4,5,1,2,3,4,5};
  std::vector<int> haystack (newints,newints+10);
  int patt1[] = {1,2,3};
  std::vector<int>::iterator ti;
  ti = std::find_end (haystack.begin(), haystack.end(), patt1, patt1+3);
  if (ti!=haystack.end())
  std::cout << "patt1 last found at position " << (ti-haystack.begin()) << '\n';
  int patt2[] = {4,5,1};
  ti = std::find_end (haystack.begin(), haystack.end(), patt2, patt2+3, newfunction);
  if (ti!=haystack.end())
  std::cout << "patt2 last found at position " << (ti-haystack.begin()) << '\n';
  return 0;
}
#包括
#包括
#包括
布尔函数(int m,int n)
{
返回值(m==n);
}
int main()
{
int newints[]={1,2,3,4,5,1,2,3,4,5};
标准::矢量干草堆(newints,newints+10);
int patt1[]={1,2,3};
std::vector::迭代器ti;
ti=std::find_end(haystack.begin(),haystack.end(),patt1,patt1+3);
if(ti!=haystack.end())

std::cout
newFunction
在这里用作比较函数。正如Igor所提到的,调用
std::find\u end
时可以省略参数
newFunction
,并且不会改变任何内容。因此,在这种情况下,
newFunction
的目的是无用的

但是,如果您决定使用不同于运算符
==
和/或其他类似逻辑的顺序

bool newfunction (int m, int n)
{
  // for some reason you want to do this
  int m_rem = m % 5;
  int n_rem = n % 3;
  return (m_rem > n_rem);
}
将执行新的排序/逻辑,而不是
std::find_end
中的默认
=


我找到了一篇关于比较函数/比较器的文章。看一看2。希望这有帮助!

在这个例子中,
newfunction
没有任何意义。如果省略了comparator参数,
std::find_end
使用
=
操作符来比较元素。
newfunction
是否完全一样。