在线文档教程
C++
应用 | Utilities

std::get(std::tuple)

STD:获取%28 std::tuple%29

(1)
template< std::size_t I, class... Types > typename std::tuple_element<I, tuple<Types...> >::type& get( tuple<Types...>& t (since C++11) (until C++14)
template< std::size_t I, class... Types > constexpr std::tuple_element_t<I, tuple<Types...> >& get( tuple<Types...>& t (since C++14)
(2)
template< std::size_t I, class... Types > typename std::tuple_element<I, tuple<Types...> >::type&& get( tuple<Types...>&& t (since C++11) (until C++14)
template< std::size_t I, class... Types > constexpr std::tuple_element_t<I, tuple<Types...> >&& get( tuple<Types...>&& t (since C++14)
(3)
template< std::size_t I, class... Types > typename std::tuple_element<I, tuple<Types...> >::type const& get( const tuple<Types...>& t (since C++11) (until C++14)
template< std::size_t I, class... Types > constexpr std::tuple_element_t<I, tuple<Types...> >const& get( const tuple<Types...>& t (since C++14)
template< std::size_t I, class... Types > constexpr std::tuple_element_t<I, tuple<Types...> >const&& get( const tuple<Types...>&& t (4)(since C++17)
template< class T, class... Types > constexpr T& get(tuple<Types...>& t(5)(since C++14)
template< class T, class... Types > constexpr T&& get(tuple<Types...>&& t(6)(since C++14)
template< class T, class... Types > constexpr const T& get(const tuple<Types...>& t(7)(since C++14)
template< class T, class... Types > constexpr const T&& get(const tuple<Types...>&& t(8)(since C++17)

1-4%29提取Ith元组中的元素。I中的整数值。[0, sizeof...(Types))...

5-8%29提取元组的元素。t他的类型是T如果元组有多个该类型的元素,则无法编译。

参数

t-tuple whose contents to extract

返回值

的选定元素的引用。t...

例外

noexcept规格:

noexcept

二次

#include <iostream> #include <string> #include <tuple> int main() { auto t = std::make_tuple(1, "Foo", 3.14 // index-based access std::cout << "(" << std::get<0>(t) << ", " << std::get<1>(t) << ", " << std::get<2>(t) << ")\n"; // type-based access std::cout << "(" << std::get<int>(t) << ", " << std::get<const char*>(t) << ", " << std::get<double>(t) << ")\n"; }

二次

产出:

二次

(1, Foo, 3.14) (1, Foo, 3.14)

二次

另见

std::get(std::array)accesses an element of an array (function template)
std::get(std::pair) (C++11)accesses an element of a pair (function template)
std::get(std::variant) (C++17)reads the value of the variant given the index or the type (if the type is unique), throws on error (function template)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/实用程序/tuple/get