如何在C ++ 03中使用自定义谓词调用std :: unique?

Tyl*_*erg 2 c++ lambda functor c++03

我看到了如何在C ++ 11中执行此操作的示例:

std::unique(v.begin(), v.end(), [](float l, float r)
{ 
  return std::abs(l - r) < 0.01; 
});
Run Code Online (Sandbox Code Playgroud)

但是,这对于我在C ++ 03中失败:

error: template argument for 'template<class _FIter, class _BinaryPredicate> _FIter std::unique(_FIter, _FIter, _BinaryPredicate)' uses local type 'CRayTracer::myFunc()::<lambda(float, float)>'
Run Code Online (Sandbox Code Playgroud)

如何在C ++ 03中做到这一点?我认为Lambda可能已经存在,并且函子/函数对象已经存在,对吗?只是寻找一个简单的解决方案,并不需要是可扩展的-它只会在这里使用。

这是无法为我编译的代码示例:

error: template argument for 'template<class _FIter, class _BinaryPredicate> _FIter std::unique(_FIter, _FIter, _BinaryPredicate)' uses local type 'CRayTracer::myFunc()::<lambda(float, float)>'
Run Code Online (Sandbox Code Playgroud)

这是它产生的错误:

testUnique.cpp: In function 'int main()':
testUnique.cpp:21:37: error: no matching function for call to 'unique(std::vector<float>::iterator, std::vector<float>::iterator, main()::approx_equal&)'
   21 |     std::unique(v.begin(), v.end(),f);
      |                                     ^
In file included from C:/msys64/mingw64/include/c++/9.2.0/algorithm:62,
                 from testUnique.cpp:3:
C:/msys64/mingw64/include/c++/9.2.0/bits/stl_algo.h:995:5: note: candidate: 'template<class _FIter> _FIter std::unique(_FIter, _FIter)'
  995 |     unique(_ForwardIterator __first, _ForwardIterator __last)
      |     ^~~~~~
C:/msys64/mingw64/include/c++/9.2.0/bits/stl_algo.h:995:5: note:   template argument deduction/substitution failed:
testUnique.cpp:21:37: note:   candidate expects 2 arguments, 3 provided
   21 |     std::unique(v.begin(), v.end(),f);
      |                                     ^
In file included from C:/msys64/mingw64/include/c++/9.2.0/algorithm:62,
                 from testUnique.cpp:3:
C:/msys64/mingw64/include/c++/9.2.0/bits/stl_algo.h:1025:5: note: candidate: 'template<class _FIter, class _BinaryPredicate> _FIter std::unique(_FIter, _FIter, _BinaryPredicate)'
 1025 |     unique(_ForwardIterator __first, _ForwardIterator __last,
      |     ^~~~~~
C:/msys64/mingw64/include/c++/9.2.0/bits/stl_algo.h:1025:5: note:   template argument deduction/substitution failed:
testUnique.cpp: In substitution of 'template<class _FIter, class _BinaryPredicate> _FIter std::unique(_FIter, _FIter, _BinaryPredicate) [with _FIter = __gnu_cxx::__normal_iterator<float*, std::vector<float> >; _BinaryPredicate = main()::approx_equal]':
testUnique.cpp:21:37:   required from here
testUnique.cpp:21:37: error: template argument for 'template<class _FIter, class _BinaryPredicate> _FIter std::unique(_FIter, _FIter, _BinaryPredicate)' uses local type 'main()::approx_equal'
   21 |     std::unique(v.begin(), v.end(),f);
      |                                     ^
testUnique.cpp:21:37: error:   trying to instantiate 'template<class _FIter, class _BinaryPredicate> _FIter std::unique(_FIter, _FIter, _BinaryPredicate)'
Run Code Online (Sandbox Code Playgroud)

这是我的一组标志:

g++ -c -g -O3 -Wp,-D_FORTIFY_SOURCE=2 -m64 -Wshadow -Wall -DMX_COMPAT_32 -fexceptions -fno-omit-frame-pointer -D__WIN32__ -std=c++03 testUnique.cpp -o testUnique.o
Run Code Online (Sandbox Code Playgroud)

for*_*818 5

我认为Lambdas可能已经存在,

不。Lambda已在C ++ 11中引入。

...并且存在函子/函数对象,对吗?

operator()函子只是带有的对象,因此它们一直都在那儿(尽管不确定何时为它们引入“函子”一词)。

对于正式的正确表达方式,我引荐您参考其他参考文献,草率地说

auto f = [](float l, float r){ 
  return std::abs(l - r) < 0.01; 
};
f(0.1,0.2);
Run Code Online (Sandbox Code Playgroud)

相当于

struct unnamed {
    bool operator()(float l, float r) {
        return std::abs(l - r) < 0.01; 
    }
};
unnamed f;
f(0.1,0.2);
Run Code Online (Sandbox Code Playgroud)

即,您始终可以用手写仿函数类替换lambda。创建函子的实例,然后传递该函子而不是lambda。

完整的例子:

#include <iostream>
#include <vector>
#include <algorithm>

struct approx_equal{
    bool operator()(float l, float r) {
        return std::abs(l - r) < 0.01; 
    }
};

int main(){
    std::vector<float> v{1.0, 1.0, 3.5, 3.5 };

    approx_equal f;

    v.erase(std::unique(v.begin(), v.end(),f),v.end());

    // sorry this is c++11, replace with iterator loop to see output pre-c++11
    for (const auto& x : v) std::cout << x << " ";  
}
Run Code Online (Sandbox Code Playgroud)

PS:在C ++ 03中,您不能在本地定义functor类,然后将其用作模板参数(请注意,您没有明确将其作为模板参数传递,而unique必须从您传入的参数中推断出其类型)。

  • @TylerShellberg为什么要通过`f()`?f()正在尝试调用operator(),该操作将返回布尔(bool)但缺少参数。该算法期望`f`,而不是`f()` (4认同)