如何通过地址获取元组中元素的索引
How to get the index of an element in a tuple via address?
例如:
std::tuple<int, double> t;
void* p = &std::get<1>(t);
现在我想通过这样的函数来获得p的索引
template<typename... Ts>
size_t index(void* p, std::tuple<Ts...> const& t)
{
...
}
当然,这个例子的结果是1。我感兴趣的是,当p是通过显式索引以外的方式获得时,如何实现获取索引的函数。
在C++14中不需要的东西,一个小型索引库:
template<unsigned...>struct indexes{using type=indexes;};
template<unsigned Cnt,unsigned...Is>
struct make_indexes:make_indexes<Cnt-1,Cnt-1,Is...>{};
template<unsigned...Is>
struct make_indexes<0,Is...>:indexes<Is...>{};
template<unsigned Cnt>
using make_indexes_t=typename make_indexes<Cnt>::type;
执行实际工作的函数。创建指向元素的数组指针。然后搜索CCD_ 1。nullptr
和-1
使其适用于空元组。
template<unsigned...Is,class Tuple>
unsigned index_of(indexes<Is...>,void const* p, Tuple const&t){
void const* r[]={ nullptr, &std::get<Is>(t)... };
auto it = std::find( std::begin(r), std::end(r), p );
if (it==std::end(r))
return -1;
else
return (it-std::begin(r))-1;
}
您可以将其放入details
命名空间中。
最终功能:
template<class...Ts>
unsigned index_of( void const*p, std::tuple<Ts...> const& t ){
return index_of( make_indexes_t<sizeof...(Ts)>{}, p, t );
}
失败时返回unsigned(-1)
。
在连续元素地址减少的假设下,比较的对数复杂性是可能的:
#include <iostream>
#include <algorithm>
#include <tuple>
// minimalistic index-list implementation
template <std::size_t...> struct index_list {using type = index_list;};
template <typename, typename> struct concat;
template <std::size_t... i, std::size_t... j>
struct concat<index_list<i...>, index_list<j...>> : index_list<i..., j...> {};
// (Inefficient) linear recursive definition of make_indices:
template <std::size_t N> struct make_indices :
concat<typename make_indices<N-1>::type, index_list<N-1>> {};
template <> struct make_indices<0> : index_list<> {};
template <> struct make_indices<1> : index_list<0> {};
// </>
template <typename T, typename = typename make_indices<std::tuple_size<T>::value>::type> struct offsets;
template <typename... T, std::size_t... indices>
struct offsets<std::tuple<T...>, index_list<indices...>>
{
static std::size_t index_of(void* p, std::tuple<T...> const& t)
{
static const std::tuple<T...> tuple;
static const std::ptrdiff_t array[]
{
reinterpret_cast<char const*>(&std::get<indices>(tuple)) - reinterpret_cast<char const*>(&tuple)...
};
auto offset_p_t = static_cast<char const*>(p) - reinterpret_cast<char const*>(&t);
auto iter = std::lower_bound( std::begin(array), std::end(array), offset_p_t, std::greater<>() );
if (iter == std::end(array)
|| *iter != offset_p_t)
return -1;
return iter - std::begin(array);
}
};
template <typename... T>
std::size_t index_of(void* p, std::tuple<T...> const& t) {return offsets<std::tuple<T...>>::index_of(p, t);}
int main ()
{
std::tuple<int, double, std::string> t;
void* p = &std::get<2>(t);
std::cout << index_of(p, t);
}
演示。
相关文章:
- 在对向量中查找元素的索引
- 当该数组的索引中没有元素时,指针指向什么?
- 如何在 STL 函数中找到传递给谓词的元素的索引?
- 如何在C++中递归地找到max元素的索引?
- 编译器(Visual C++)如何优化按索引访问矢量元素?
- 保持排序的数据结构,允许log N插入时间,并且可以返回我在log N中查找的元素的索引
- 使用推力根据索引更改某些元素的值
- 为什么 min_element() 返回最小元素的索引,而不是迭代器?
- CPtrList - 如何获取元素的索引?
- 获取结构 c++ 中元素的索引
- C++ STL 数据结构常时按索引推送/弹出/随机访问,并具有指向元素的可靠指针
- 矢量指针索引处的读取元素
- C++函数返回两个 char 数组的相同索引元素
- 特征:类似 numpy 的元素索引
- 如何在另一个数组中找到一个未排序数组的不同元素索引?
- C 无法使用嵌套环将数组的元素索引
- Opencv Mat的元素索引产生垃圾
- 在c++中,当元组的元素索引在运行时已知时,是否有可能获得该元素的类型?
- 按内容访问QVector元素索引
- C++ 矢量元素索引