c – 自定义迭代器与std :: sort一起使用但不与tbb :: parallel_sort一起使用?

我试图使用tbb :: parallel_sort同时排序2个数组.英特尔的文档在这里说 https://software.intel.com/en-us/node/506167对迭代器和序列的要求与std :: sort相同.这似乎不是这种情况.我的自定义迭代器与std :: sort完美配合,但是使用tbb :: parallel_sort产生编译错误.请看下面的代码
int main()//needs boost and tbb to compile
{
    int values_size = 6;
    int nums1[] = {5,8,7,89,56,4};
    int nums2[] = {2,1,4,9,2};

    //WORKS!
    std::sort(do_dual_sort.make_iter(nums1,nums2),do_dual_sort.make_iter(nums1+values_size,nums2+values_size),do_dual_sort.make_comp_desc(nums1,nums2));

    //DOESN'T COMPILE
    tbb::parallel_sort(do_dual_sort.make_iter(nums1,nums2));

    for(unsigned int i = 0; i < values_size; i++) cout << "nums1[" << i << "] " << nums1[i] << " | nums2[" << i << "] "  << nums2[i] << "\n";
    return 0;
}

class dual_sort
{
public:
    template <class T,class T2>
    struct helper_type {
        public:
            typedef boost::tuple<typename iterator_traits<T>::value_type,typename iterator_traits<T2>::value_type> value_type;
            typedef boost::tuple<typename iterator_traits<T>::value_type&,typename iterator_traits<T2>::value_type&> ref_type;
    };

    template <typename T1,typename T2>
    class dual_iterator : public boost::iterator_facade<dual_iterator<T1,T2>,typename helper_type<T1,T2>::value_type,boost::random_access_traversal_tag,T2>::ref_type> {
    public:
        explicit dual_iterator(T1 iter1,T2 iter2) : mIter1(iter1),mIter2(iter2) {}
        typedef typename iterator_traits<T1>::difference_type difference_type;
    private:
        void increment() { ++mIter1; ++mIter2; }
        void decrement() { --mIter1; --mIter2; }
        bool equal(dual_iterator const& other) const { return mIter1 == other.mIter1; }
        typename helper_type<T1,T2>::ref_type dereference() const { return (typename helper_type<T1,T2>::ref_type(*mIter1,*mIter2)); }
        difference_type distance_to(dual_iterator const& other) const { return other.mIter1 - mIter1; }
        void advance(difference_type n) { mIter1 += n; mIter2 += n; }

        T1 mIter1;
        T2 mIter2;
        friend class boost::iterator_core_access;
    };

    template <typename T1,typename T2>
    dual_iterator<T1,T2> make_iter(T1 t1,T2 t2) { return dual_iterator<T1,T2>(t1,t2); }

    template <class T1,class T2> struct iter_comp_desc {
        typedef typename helper_type<T1,T2>::value_type T;
        bool operator()(const T& t1,const T& t2) const { return get<0>(t1) > get<0>(t2); }
        bool operator()(const char*& t1,const char*& t2) const { return strcmp(get<0>(t1),get<0>(t2)) == 1; }
    };

    template <class T1,class T2> iter_comp_desc<T1,T2> make_comp_desc(T1 t1,T2 t2) { return iter_comp_desc<T1,T2>(); }

} do_dual_sort;

我得到的编译错误是:

error C2512: 'dual_sort::dual_iterator<T1,T2>' : no appropriate default constructor available
with
[
    T1=int *,T2=int *
]
tbb44_20150728oss\include\tbb/parallel_sort.h(201) : see reference to function template instantiation 'void tbb::internal::parallel_quick_sort<RandomAccessIterator,Compare>(RandomAccessIterator,RandomAccessIterator,const Compare &)' being compiled
with
[
    RandomAccessIterator=dual_sort::dual_iterator<int *,int *>,Compare=dual_sort::iter_comp_desc<int *,int *>
]
main.cpp(1125) : see reference to function template instantiation 'void tbb::parallel_sort<dual_sort::dual_iterator<T1,dual_sort::iter_comp_desc<T1,T2>>(RandomAccessIterator,const Compare &)' being compiled
with
[
    T1=int *,T2=int *,RandomAccessIterator=dual_sort::dual_iterator<int *,int *>
]

编辑:我使用的编译器是Visual Studio 2012.您可以尝试用std替换一些boost函数以使其在g上工作.

解决方法

对于RandomAccessIterator,引用必须是对value_type的引用.它不能是引用的元组.

因此,您的双迭代器不是有效的RandomAccessIterator.

许多算法仍然有效,但这并不能使您的代码有效.

要求相同并不意味着任何与std :: sort的给定实现一起使用的东西也可以与tbb :: parallel_sort一起使用:std :: sort的给定实现不必强制执行所有要求.标准.

无论文档如何,如果实现不适用于您的代码,它将无法与您的代码一起使用.

最简单的方法可能是在原始数组中创建一对伪索引(或迭代器)数组,然后对其进行排序.你只需要覆盖<正常.

相关文章

/** C+⬑ * 默认成员函数 原来C++类中,有6个默认成员函数: 构造函数 析构函数 拷贝...
#pragma once // 1. 设计一个不能被拷贝的类/* 解析:拷贝只会放生在两个场景中:拷贝构造函数以及赋值运...
C类型转换 C语言:显式和隐式类型转换 隐式类型转化:编译器在编译阶段自动进行,能转就转,不能转就编译...
//异常的概念/*抛出异常后必须要捕获,否则终止程序(到最外层后会交给main管理,main的行为就是终止) try...
#pragma once /*Smart pointer 智能指针;灵巧指针 智能指针三大件//1.RAII//2.像指针一样使用//3.拷贝问...
目录&lt;future&gt;future模板类成员函数:promise类promise的使用例程:packaged_task模板类例程...