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,855 questions

51,776 answers

573 users

How to return multiple values from function in C++

3 Answers

0 votes
#include <iostream>

auto f() {
  struct values {        
    int a, b;
    std::string s;
  };
  return values {12, 837, "c++"}; 
}

int main() {
  auto [value1, value2, value3] = f(); 
  
  std::cout << value1 << ", " << value2 << ", " << value3;

  return 0;
}



/*
run:

12, 837, c++

*/

 



answered May 14, 2021 by avibootz
0 votes
#include <iostream>
#include <tuple>

std::tuple<int, float, std::string> f() {
  return {12, 3.14, "c++"};
}

int main() {
  auto [value1, value2, value3] = f(); 
  
  std::cout << value1 << ", " << value2 << ", " << value3;

  return 0;
}




/*
run:

12, 3.14, c++

*/

 



answered May 14, 2021 by avibootz
0 votes
#include <iostream>

std::pair<int, int> f() {
    std::pair<int, int> pr;

    pr.first = 23;
    pr.second = 848;
    
    return pr;
}


int main() {
    auto pr = f();
    
    std::cout << pr.first << " " << pr.second;

    return 0;
}




/*
run:

23 848

*/

 



answered May 14, 2021 by avibootz

Related questions

1 answer 134 views
1 answer 140 views
1 answer 84 views
4 answers 110 views
4 answers 118 views
1 answer 60 views
1 answer 78 views
...