Click the above“Mechanical and Electronic Engineering Technology” to follow us
In programming, we often encounter scenarios where a function needs to return multiple values. Although C++ functions cannot directly return multiple parameters, we can easily achieve this requirement through some indirect methods. This article will detail several common implementation methods and analyze their advantages, disadvantages, and applicable scenarios.

1. Introduction
In C++, the return value of a function is usually a single value. However, in actual development, we often need to return multiple values, such as calculation results, status codes, error messages, etc. To meet this demand, C++ provides various methods to indirectly implement returning multiple parameters. This article will help you master these methods through code examples and detailed analysis.
2. Using <span>std::pair</span>
or <span>std::tuple</span>
2.1 <span>std::pair</span>
<span>std::pair</span>
is a template class in the C++ standard library used to store two values. It is very suitable for scenarios where two values need to be returned.
Example Code:
#include <iostream>
#include <utility> // std::pair
std::pair<int, int> fit(int c, int d, int e) {
int a = c + d;
int b = d + e;
return {a, b};
}
int main() {
auto result = fit(1, 2, 3);
int a = result.first;
int b = result.second;
std::cout << "a = " << a << ", b = " << b << std::endl;
return 0;
}
Output:
a = 3, b = 5
2.2 <span>std::tuple</span>
<span>std::tuple</span>
is an extension of <span>std::pair</span>
that can store multiple values. It is suitable for scenarios where three or more values need to be returned.
Example Code:
#include <iostream>
#include <tuple> // std::tuple
std::tuple<int, int, int> fit(int c, int d, int e) {
int a = c + d;
int b = d + e;
int f = c + e;
return {a, b, f};
}
int main() {
auto result = fit(1, 2, 3);
int x, y, z;
std::tie(x, y, z) = result;
std::cout << "x = " << x << ", y = " << y << ", z = " << z << std::endl;
return 0;
}
Output:
x = 3, y = 5, z = 4
2.3 Advantages and Disadvantages
-
Advantages:
- Simple and easy to use, suitable for quick implementation.
- Standard library support, no need to define additional structures.
-
Disadvantages:
- The semantics of return values are not clear enough, especially when there are many return values.
- Not suitable for returning a large number of values or complex data structures.
3. Using Reference Parameters
By passing parameters as references to the function, we can directly modify the values of these parameters within the function, thus achieving the effect of returning multiple values.
Example Code:
#include <iostream>
void fit(int c, int d, int e, int& a, int& b) {
a = c + d;
b = d + e;
}
int main() {
int a, b;
fit(1, 2, 3, a, b);
std::cout << "a = " << a << ", b = " << b << std::endl;
return 0;
}
Output:
a = 3, b = 5
3.1 Advantages and Disadvantages
-
Advantages:
- Simple and intuitive, suitable for returning a small number of values.
- No additional memory allocation is needed, resulting in higher performance.
-
Disadvantages:
- The function call style is not elegant, and the readability of the code decreases when there are too many parameters.
- Not suitable for returning complex data structures.
4. Using Custom Structures or Classes
Defining a custom structure or class to encapsulate multiple return values is a semantically clear and flexible method.
Example Code:
#include <iostream>
struct Result {
int a;
int b;
};
Result fit(int c, int d, int e) {
Result res;
res.a = c + d;
res.b = d + e;
return res;
}
int main() {
Result res = fit(1, 2, 3);
std::cout << "a = " << res.a << ", b = " << res.b << std::endl;
return 0;
}
Output:
a = 3, b = 5
4.1 Advantages and Disadvantages
-
Advantages:
- Clear semantics, easy to understand and maintain.
- Can encapsulate complex data structures, suitable for returning a large number of values.
-
Disadvantages:
- Requires additional definitions of structures or classes, increasing the amount of code.
- For simple scenarios, it may seem overly complicated.
5. Using C++17 Structured Bindings
C++17 introduced structured bindings, which can simplify the process of unpacking values from <span>std::pair</span>
or <span>std::tuple</span>
.
Example Code:
#include <iostream>
#include <utility> // std::pair
std::pair<int, int> fit(int c, int d, int e) {
int a = c + d;
int b = d + e;
return {a, b};
}
int main() {
auto [a, b] = fit(1, 2, 3);
std::cout << "a = " << a << ", b = " << b << std::endl;
return 0;
}
Output:
a = 3, b = 5
5.1 Advantages and Disadvantages
-
Advantages:
- Simplified syntax, easy to read and maintain.
- Combined with
<span>std::pair</span>
or<span>std::tuple</span>
, it can quickly implement returning multiple values. -
Disadvantages:
- Requires support for C++17 or higher.
- For complex scenarios, it may need to be combined with other methods.
6. Applicable Scenarios for Methods
Depending on different needs, different methods can be chosen:
-
Returning two values:
- Use
<span>std::pair</span>
or structured bindings.
Returning three or more values:
- Use
<span>std::tuple</span>
or custom structures.
Returning a small number of values with high performance requirements:
- Use reference parameters.
Returning complex data structures or requiring clear semantics:
- Use custom structures or classes.
7. Conclusion
In C++, although functions cannot directly return multiple parameters, through <span>std::pair</span>
, <span>std::tuple</span>
, reference parameters, custom structures, and C++17 structured bindings, we can easily achieve the effect of returning multiple values. Each method has its applicable scenarios and advantages and disadvantages, and choosing the right method can improve the readability and maintainability of the code.
Through this article, you have mastered the implementation and usage skills of these methods. I hope this knowledge can help you handle the need to return multiple values more flexibly in actual development.
Want to learn more?
Quickly scan the code to follow us