How to unpack the tuple values into separate variables in C++

2 Answers

0 votes
#include <iostream>
#include <tuple> 
  
using namespace std; 
  
int main()
{
    tuple <char, int, float, string> t('z', 89, 3.14, "c++");
 
    char ch; 
    int i; 
    float f;  
    string s;  
       
    tie(ch, i, f, s) = t; 
    
    cout << ch << endl;
    cout << i << endl;
    cout << f << endl;    
    cout << s << endl;
 
    return 0;
}
  
  
  
/*
run:
  
z
89
3.14
c++
 
*/

 



answered Feb 8, 2020 by avibootz
0 votes
#include <iostream>
#include <tuple> 
 
using namespace std; 
 
int main()
{
    tuple <char, int, float, string> t('z', 89, 3.14, "c++");

    int i; 
    string s;  
      
    tie(ignore, i, ignore, s) = t; 
   
    cout << i << endl;
    cout << s << endl;

    return 0;
}
 
 
 
/*
run:
 
89
c++

*/

 



answered Feb 8, 2020 by avibootz

Related questions

3 answers 308 views
1 answer 122 views
3 answers 198 views
1 answer 167 views
2 answers 151 views
...