
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ Tuple::get() Function
The C++ std::tuple::get() function allows you to access the elements stored in the tuple by index. It takes the tuple and an index as an arguments and returns the value at that index. The index must be known at compile time and must be within the range of tuples. For instance, std::get<0>(mytuple) retrieves the first element.
When we try to access the index that is of out of range will lead to a run time error.
Syntax
Following is the syntax for std::tuple::get() function.
typename tuple_element< I, tuple<Types...> >::type& get(tuple<Types...>& tpl) noexcept;
Parameters
- I − It indicates the position of element.
- Types − It indicates the type of element.
- tpl − It indicates the tuple object with more than I elements.
Return Value
This funcion returns a reference to the Ith element of tuple tpl.
Example
Let's look at the following example, where we are goign to access the first element of the tuple.
#include <iostream> #include <tuple> int main() { std::tuple<int, float, std::string> x(1, 0.04, "Welcome"); int firstElement = std::get<0>(x); std::cout << " " << firstElement << std::endl; return 0; }
Output
If we run the above code it will generate the following output −
1
Example
Consider the another scenario, where we are going to access the third element of the tuple.
#include <iostream> #include <tuple> int main() { std::tuple<int, float, std::string> x(10, 3.14, "TutorialsPoint"); std::string thirdElement = std::get<2>(x); std::cout << " " << thirdElement << std::endl; return 0; }
Output
Let us compile and run the above program, this will produce the following result −
TutorialsPoint
Example
In the following example, we are going to use the std::tie to extract multiple elements from a tuple.
#include <iostream> #include <tuple> int main() { std::tuple<int, float, std::string> x(1, 0.04, "TP"); int first; float second; std::tie(first, second, std::ignore) = x; std::cout << "1st Element: " << first << ", 2nd Element: " << second << std::endl; return 0; }
Output
Following is the output of the above code −
1st Element: 1, 2nd Element: 0.04
Example
Following is the example, where we are going to use the get() function and modifying the tuple.
#include <iostream> #include <tuple> int main() { std::tuple<int, std::string, double> x(1, "TP", 0.02); std::get<1>(x) = "TutorialsPoint"; std::cout << "After Modification: " << std::get<0>(x) << ", " << std::get<1>(x) << ", " << std::get<2>(x) << std::endl; return 0; }
Output
Output of the above code is as follows −
After Modification: 1, TutorialsPoint, 0.02