C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c – 正确测试容器是否实现.at()成员访问/ std :: sort兼容的方法大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在寻找最佳/正确的方法来确定容器是否通过.at()实现随机元素访问.
在不同(stl)容器相对于彼此进行排序的场景中(比如对容器std :: vector< int>进行排序,相对于std :: vector< double>),@R_971_10673@下事情:
std::sort(toOrder.begin(),toOrder.end(),[&orderBy](int i,int j) -> bool {
    return orderBy.at(i) > orderBy.at(j);
});

哪里

std::vector<int> toOrder;
std::vector<double> orderBy

我可以将它包装在模板函数中,但不确定限制或测试具有随机访问迭代器/ .at()的容器的最佳方法(当它们没有时,需要做一些昂贵的事情).

我有这个

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

template <typename T,typename U>
void sorty(T& a,U const X) {
    std::sort(a.begin(),a.end(),[&x](int i,int j) -> bool { return x.at(i) > x.at(j); });
}

int main() {

    std::vector<int> toOrder(10);
    std::iota(toOrder.begin(),0);
    std::vector<double> orderBy{0.2,9.8,4.0,0.01,15.1,3.3,9.01,9.11,100.1,2.03};

    std::unordered_set<double> orderBy_s(orderBy.begin(),orderBy.end()); // no .at()

    sorty(toOrder,orderBy);

    for (auto i : toOrder) {
        std::cout << i << "\t";
    }

    return 0;
}

演示@L_262_7@here

更新:我匆匆发布,没有编辑标题.我关注任何容器类型,而不仅仅是STl.我的例子使用了STL容器以方便和重现.

解决方法

基本上,这不是应该实施通用 algorithms的正确方法.通常,可以使用 iteratorsstd::iterator_traits来确定基础类型和允许的操作.如果要根据容器提供的接口(随机访问,非随机访问)执行不同的算法(具有不同的复杂性),则应执行以下操作.

首先,您的通用算法应该在范围而不是容器上运行.也就是说,这应该看起来像< algorithm>的任何函数

template <typename Iterator>
void sorty(Iterator first,Iterator last);

其次,您应该编写应用不同排序方法的辅助函数,以尽可能多地利用容器的接口,从而以最有效的方式工作:

// O(N*lgN) complexity sorTing
template <typename Iterator>
void sorty_Helper(Iterator first,Iterator last,std::random_access_iterator_tag);

// O(N*N) complexity sorTing
template <typename Iterator>
void sorty_Helper(Iterator first,std::forWARD_iterator_tag);

现在,您的原始sorty函数实际上应该只根据通过std :: iterator_Traits获得的迭代器的类型将迭代器转发到适当的辅助函数

template <typename Iterator>
void sorty(Iterator first,Iterator last)
{
    sorty_Helper(first,last,typename std::iterator_Traits<Iterator>::iterator_category());
}

DEMO 1

另一种方法是使用SFINAE技术启用/禁用功能模板:

#include <iterator>
#include <type_Traits>    

template <typename Iterator>
typename std::enable_if<
    std::is_same<typename std::iterator_Traits<Iterator>::iterator_category,std::random_access_iterator_tag>::value
    >::type
    sorty(Iterator first,Iterator last)
{
    // O(N*lgN) complexity sorTing
}

template <typename T> struct Alwaysfalse : std::false_type {};

template <typename Iterator>
typename std::enable_if<
    !std::is_same<typename std::iterator_Traits<Iterator>::iterator_category,Iterator last)
{
    // other sorTing algorithm or print out a user-friendly error
    static_assert(Alwaysfalse<Iterator>{},"Iterator must be a random-access iterator!");
}

DEMO 2

其中enable_if和is_same是C 11的类型特征,C 03中的特征可以定义如下:

template <bool b,typename T = void>
struct enable_if {};
template <typename T>
struct enable_if<true,T> { typedef T type; };
template <typename T,typename U>
struct is_same { static const bool value = false; };
template <typename T,typename U>
const bool is_same<T,U>::value;
template <typename T>
struct is_same<T,T> { static const bool value = true; };
template <typename T>
const bool is_same<T,T>::value;

另一方面,如果您只想检查at成员函数的存在,并根据它做出编译时决策,您可能希望使用表达式SFINAE技术:

template <typename Container>
auto sorty_Helper(Container&& container,int)
    -> decltype(void(std::forWARD<Container>(container).at(0)))
{
    // O(N*lgN) complexity sorTing
}

template <typename Container>
void sorty_Helper(Container&& container,void*)
{
    // O(N*N) complexity sorTing
}

template <typename Container>
void sorty(Container&& container)
{
    sorty_Helper(std::forWARD<Container>(container),0);
}

DEMO 3

在C 03中,验证给定签名的成员函数的存在需要手写特征:

template <typename T>
struct has_at
{
    typedef char (&yes)[1];
    typedef char (&no)[2];

    template <typename U,U u>
    struct SFINAE {};

    template <typename U>
    static yes test(SFINAE<typename U::reference(U::*)(std::size_t),&U::at>*);

    template <typename U>
    static no test(...);

    static const bool value = sizeof(test<T>(0)) == sizeof(yes);
};

可以与enable_if结合使用:

template <bool b,typename T = void>
struct enable_if {};

template <typename T>
struct enable_if<true,T> { typedef T type; };

template <typename Container>
typename enable_if<has_at<Container>::value>::type
    sorty(Container& container)
{
    // O(N*lgN) complexity sorTing
}

template <typename Container>
typename enable_if<!has_at<Container>::value>::type
    sorty(Container& container)
{
    // O(N*N) complexity sorTing
}

DEMO 4

大佬总结

以上是大佬教程为你收集整理的c – 正确测试容器是否实现.at()成员访问/ std :: sort兼容的方法全部内容,希望文章能够帮你解决c – 正确测试容器是否实现.at()成员访问/ std :: sort兼容的方法所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。