[英]Correct way to test if a container implements .at() member access / std::sort compatible
我正在尋找最佳/正確的方法來確定容器是否通過.at()
實現隨機元素訪問。 在不同(stl)容器相對於彼此進行排序的場景中(比如對std::vector<int>
排序,相對於std::vector<double>
),我做了以下事情:
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(), toOrder.end(), 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;
}
演示代碼在這里
更新:我匆匆發布,沒有編輯標題。 我關注任何容器類型,而不僅僅是STL。 我的例子使用了STL容器以方便和重現。
基本上,這不是應該實現通用算法的正確方法。 通常,可以使用iterator
和std::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, Iterator last,
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());
}
另一種方法是使用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, std::random_access_iterator_tag>::value
>::type
sorty(Iterator first, Iterator last)
{
// other sorting algorithm or print out a user-friendly error
static_assert(AlwaysFalse<Iterator>{}, "Iterator must be a random-access iterator!");
}
其中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);
}
在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
}
您可以執行以下操作
namespace detail
{
template<typename T>
decltype(std::declval<T>().at(0), std::true_type{}) has_at(int);
template<typename T>
std::false_type has_at(...);
}
// traits to detect if T::at(int) is valid
template<typename T> struct has_at : public decltype(detail::has_at<T>(0)) {};
template<typename T, typename U>
typename std::enable_if<has_at<U>::value>::type
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);});
}
然后你可以在!has_at<U>::value
時實現替代函數:
template<typename T, typename U>
typename std::enable_if<!has_at<U>::value>::type
sorty(T& a, U const x)
{
// Alternative implementation
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.