2
A
回答
3
映射一個元組很容易給出a pack of indices,如:
#include <tuple>
#include <functional>
#include <iostream>
template <int...>
struct Seq {};
template <int n, int... s>
struct Gens : Gens<n-1, n-1, s...> {};
template <int... s>
struct Gens<0, s...> {
typedef Seq<s...> Type;
};
// The above are taken from https://stackoverflow.com/q/7858817
使用指標,我們可以使用std::get
適用std::ref
的元組的每一個元素:
template <int... s, typename Tuple>
auto ref_tuple_impl(Seq<s...> seq, Tuple& tup)
-> std::tuple<
std::reference_wrapper<
typename std::tuple_element<s, Tuple>::type
>...
>
{
return std::make_tuple(std::ref(std::get<s>(tup))...);
}
template <typename Tuple>
auto ref_tuple(Tuple& tup)
-> decltype(ref_tuple_impl(typename Gens<std::tuple_size<Tuple>::value>::Type>(), tup))
{
return ref_tuple_impl(typename Gens<std::tuple_size<tuple>::value>::Type(), tup);
}
用法示範:
int main() {
auto t = std::make_tuple(1, 4.5, "66");
auto rt = ref_tuple(t);
std::get<0>(rt).get() = 123;
std::get<1>(rt).get() = 5.67;
std::get<2>(rt).get() = "34";
std::cout << std::get<0>(t) << std::endl;
std::cout << std::get<1>(t) << std::endl;
std::cout << std::get<2>(t) << std::endl;
}
相關問題
- 1. 如何獲取元組元組中的最大元素?
- 2. 從元組元組中獲取值
- 3. 如何從數組中獲取元素。
- 4. 如何從表單元素的值中的數組中獲取數組元素?
- 5. 獲取列表中的元組元素
- 6. 獲取元素(元組)由2組
- 7. 如何從SML中的元組列表中獲取元素?
- 8. (Swift)如何從元組數組[[Date,MyClass]]中獲取元組元素(Date,MyClass)的索引?
- 9. 引用元組列表中的元素
- 10. 從元組元組中區分元組
- 11. 如何從Python中的元組獲取第二個元素?
- 12. C++ 11。如何從沒有const的元組中獲取元素
- 13. 從plist中的一組元素中獲取獨特元素
- 14. HIVE,如何從數組中獲取元素,元素本身也是數組
- 15. 如何從元組中創建元組?
- 16. 如何從一個可變元組中獲取元素?
- 17. Boost.Range的C++元組 - 獲取元素類型的元組?
- 18. 組合元組列表中的元組元素。元組元素是元組和列表
- 19. 獲取組成元素引用的html?
- 20. 獲取一個元組的第一個元素,在元組
- 21. 如何過濾元組元組中的第二個元素?
- 22. 如何從列表中讀取元組中的每個元素
- 23. 如何訪問元組中的元素?
- 24. 如何交換元組中的元素?
- 25. 從數組中獲取某些元素
- 26. 從數組中獲取元素?
- 27. 從事件中獲取元素數組
- 28. 從數組中獲取單個元素
- 29. 從F#中的元組集合中獲取元素
- 30. 從Python中的元組中減去元組的元組?
進行了一項更改,以便'ref_tuple'還可以與'std :: tuple const'一起使用。 –