![The C++ Standard Library](/cpp_standard_library/images/cpp-mini-lib-logo.jpg)
- The C Standard Library
- The C Standard Library
- The C++ Standard Library
- 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>
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ Tuple::tie() Function
The C++ std::tuple::tie() function is used to help the binding of multiple variables to the elements of a tuple. Essentially, it allows for the unpacking of a tuple values directly into specified variables. It is mainly useful in the scenarios where you want to decompose the elements of the tuple into separate variables for further manipulation.
The tie() function can be employed in conjunction with other operations likes comparison and sorting.
Syntax
Following is the syntax for std::tuple::tie() function.
tie( Types&... args ) noexcept;
Parameters
- args − It contains a list of elements that the constructed tuple shall contain.
Example
Let's look at the following example, where we are going to swap the values of two variables using tie() with make_tuple().
#include <iostream> #include <tuple> using namespace std; int main() { int x = 11, y = 111; tie(x, y) = make_tuple(y, x); cout << "x: " << x << ", y: " << y << endl; return 0; }
Output
Let us compile and run the above program, this will produce the following result −
x: 111, y: 11
Example
Consider the another scenario, where we are going to unpack the tuple data into separate variables using tie() function.
#include <iostream> #include <tuple> using namespace std; int main() { tuple<float, string> data = make_tuple(0.01, "Welcome"); float x; string y; tie(x,y) = data; cout << "x: " << x << ", y: " << y <<endl; return 0; }
Output
If we run the above code it will generate the following output −
x: 0.01, y: Welcome
Example
In the following example, only the second element of the tuple data is unpacked into variable, while other elements are ignored.
#include <iostream> #include <tuple> using namespace std; int main() { tuple<int, string> data = make_tuple(42, "TutorialsPoint"); string x; tie(ignore, x) = data; cout << "x: " << x << endl; return 0; }
Output
Following is the output of the above code −
x: TutorialsPoint