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



我在C++11:中看到了这个例子

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

然而,这对我来说在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)>'

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

下面是一个不会为我编译的代码示例:

#include <iostream>
#include <vector>
#include <algorithm>
int main(){
std::vector<float> v;
v.push_back(1.0);
v.push_back(1.0);
v.push_back(3.5);
v.push_back(3.5);
struct approx_equal
{
bool operator()(float l, float r)
{
return std::abs(l - r) < 0.01;
}
};
approx_equal f;
std::unique(v.begin(), v.end(),f);
}

这是它产生的错误:

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)'

这是我的一面旗帜:

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

我认为Lambdas可能已经存在了,

否。Lambdas已在C++11中引入。

。。。并且存在函子/函数对象,对吧?

函子只是具有operator()的对象,因此它们一直存在(尽管不确定"函子"一词是何时真正引入的(。

对于正式的正确表达方式,我推荐你参考其他参考文献,草率地说这个

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

相当于

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

也就是说,您总是可以用手写的函子类来替换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 << " ";  
}

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

Lambdas在C++03中不存在,它们是在C++11中引入的。

由于您的lambda示例不需要捕获值,您可以用一个简单的独立函数来替换它,您不需要函子,例如:

#include <iostream>
#include <vector>
#include <algorithm>
bool approx_equal(float l, float r) {
return std::abs(l - r) < 0.01;
}
int main(){
std::vector<float> v;
v.push_back(1.0);
v.push_back(1.0);
v.push_back(3.5);
v.push_back(3.5);
std::unique(v.begin(), v.end(), approx_equal);
}

但是,您也可以使用函子。在尝试使用函子时,您只需在main()本身内部定义函子类型,使其成为本地类型,编译器对此表示不满。改为在全局范围内定义函子类型(您仍然可以使用局部变量来实例化类型(,例如:

#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;
v.push_back(1.0);
v.push_back(1.0);
v.push_back(3.5);
v.push_back(3.5);
approx_equal f;
std::unique(v.begin(), v.end(), f);
// or:
// std::unique(v.begin(), v.end(), approx_equal{});
}

最新更新