Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,939 questions

51,876 answers

573 users

How to get tuple values in C++

2 Answers

0 votes
#include <iostream>
#include <tuple> 
  
std::tuple<int, double, std::string> func() {
    std::tuple<int, double, std::string> values(23, 3.14, "c++");
   
    return values;
}
   
int main()
{
    std::tuple<int, double, std::string> t = func();
   
    int n = std::get<0>(t);
   
    double d = std::get<1>(t);
   
    std::string s = std::get<2>(t);
   
    std::cout << n << std::endl;
    std::cout << d << std::endl;
    std::cout << s << std::endl;
  
    const int idx = 1;
    std::cout << std::get<idx>(t);
   
    return 0;
}
  
  
  
/*
run:
  
23
3.14
c++
3.14
  
*/

 



answered Feb 7, 2020 by avibootz
edited Feb 8, 2020 by avibootz
0 votes
#include <iostream>
#include <tuple> 
 
using namespace std; 
 
int main()
{
    tuple <char, int, float, string> t; 
 
    t = make_tuple('z', 89, 3.14, "c++"); 
   
    cout << get<0>(t) << endl;
    cout << get<1>(t) << endl;
    cout << get<2>(t) << endl;    
    cout << get<3>(t) << endl;
     
    return 0;
}
 
 
 
/*
run:
 
z
89
3.14
c++
 
*/

 



answered Feb 7, 2020 by avibootz
edited Feb 8, 2020 by avibootz

Related questions

2 answers 243 views
1 answer 120 views
120 views asked Feb 8, 2020 by avibootz
1 answer 128 views
128 views asked Feb 8, 2020 by avibootz
1 answer 184 views
184 views asked Jun 3, 2021 by avibootz
...