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

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

#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
maximum_prime_is_463035818

我认为Lambda可能已经存在,

不可以。C++ 11中引入了Lambda。

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

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中,您不能在本地定义functor类,然后将其用作模板参数(请注意,您没有显式将其作为模板参数传递,而unique必须从您传入的参数中推断出其类型)。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在C ++ 11中返回包含自定义删除器的std :: unique_ptr?

如何在Python中使用自定义谓词排序

如何使用带有 std::make_unique 的 WinAPI 使用自定义删除器?

如何在C ++中返回unique_ptr的列表?

C ++ 11:使用自定义Lambda Deleter返回std :: unique_ptr

如何制作将函数包装在noexcept可检测的可调用对象中的类模板,以用作std :: unique_ptr自定义删除器?

如何在std :: copy中使用unique_ptr?

如何在自定义UserChangeForm中覆盖用户名的django'unique'错误消息

如何在调用使用 unique_lock 的其他函数的类中使用 unique_lock?

如何在dask DataFrame上调用unique()

如何在clang 3.4中使用make_unique?

如何将自定义删除器与std :: unique_ptr成员一起使用?

如何在C ++中使用自定义比较器创建std :: set?

如何安全地重载std :: unique_ptr的自定义删除程序?

未调用std :: unique_ptr中的自定义删除器

C ++ 03中std :: unique_ptr的仿真

如何从Java的自定义谓词列表中创建谓词?

如何在c中使用自定义类型从节点列表中读取数据结构

如何在结构上使用std :: unique_ptr?

如何在构造函数中使用unique_ptr?

如何在 CASE STATEMENT (SQL ORACLE) 中使用 Unique 或 Distinct?

如何在joomla 2.5中使用ajax在自定义模块的帮助文件中调用函数?

是否可以在C#中使用自定义值或模板值创建谓词

如何使用lambda和功能作为unique_ptr的自定义删除器

如何从自定义帖子中获取数组的值并在我的ajax调用中使用

如何在Java Web App中创建自定义标签?如何在JSP中使用自定义标签?

如何在自定义类中使用std :: unordered_multiset?

如何在自定义目标调用的子进程中使用 CMake 缓存变量?

如何在 Django 模型中改变 unique true 的行为?