c++中函数放在等号右边_如何从C或C++中的函数返回多个值?
新程序員通常在尋找從函數返回多個值的方法。不幸的是,C和C++不允許直接這樣做。但是幸運的是,通過一些巧妙的編程,我們可以輕松實現這一目標。
下面是從C函數中返回多個值的方法:
- 通過使用指針。
- 通過使用結構。
- 通過使用數組。
示例:考慮一個示例,其中的任務是查找兩個不同數字中的較大和較小值。 我們可以編寫多個函數。主要問題是調用多個函數的麻煩,因為我們需要返回多個值,并且當然要鍵入更多的代碼行。
1)使用指針返回多個值:傳遞參數及其地址,并使用指針更改其值。
// Modified program using pointers #include // add is the short name for address void compare(int a, int b, int* add_great, int* add_small) { if (a > b) { // a is stored in the address pointed // by the pointer variable *add_great *add_great = a; *add_small = b; } else { *add_great = b; *add_small = a; } } // Driver code int main() { int great, small, x, y; printf("Enter two numbers: "); scanf("%d%d", &x, &y); // The last two arguments are passed // by giving addresses of memory locations compare(x, y, &great, &small); printf("The greater number is %d and the" "smaller number is %d", great, small); return 0; }輸出:
Enter two numbers: 5 8The greater number is 8 and the smaller number is 52)使用結構返回多個值:由于結構是用戶定義的數據類型。這個想法是用兩個整數變量定義一個結構,并將較大和較小的值存儲到這些變量中,然后使用該結構的值。
// Modified program using structures #include struct greaterSmaller { int greater, smaller; }; typedef struct greaterSmaller Struct; Struct findGreaterSmaller(int a, int b) { Struct s; if (a > b) { s.greater = a; s.smaller = b; } else { s.greater = b; s.smaller = a; } return s; } // Driver code int main() { int x, y; Struct result; printf("Enter two numbers: "); scanf("%d%d", &x, &y); // The last two arguments are passed // by giving addresses of memory locations result = findGreaterSmaller(x, y); printf("The greater number is %d and the" "smaller number is %d", result.greater, result.smaller); return 0; }輸出:
Enter two numbers: 5 8The greater number is 8 and the smaller number is 53)使用數組返回多個值(僅當返回的項屬于同一類型時有效):當數組作為參數傳遞時,其基地址將傳遞給函數,因此對數組副本所做的任何更改都將在原始數組中更改。
以下是使用數組返回多個值的程序,即在arr[0]存儲更大的值,在arr[1]存儲較小的值。
// Modified program using array #include // Store the greater element at 0th index void findGreaterSmaller(int a, int b, int arr[]) { // Store the greater element at // 0th index of the array if (a > b) { arr[0] = a; arr[1] = b; } else { arr[0] = b; arr[1] = a; } } // Driver code int main() { int x, y; int arr[2]; printf("Enter two numbers: "); scanf("%d%d", &x, &y); findGreaterSmaller(x, y, arr); printf("The greater number is %d and the" "smaller number is %d", arr[0], arr[1]); return 0; }輸出:
Enter two numbers: 5 8The greater number is 8 and the smaller number is 5僅C++方法
1)使用引用返回多個值:我們在C++中使用引用來存儲返回的值。
// Modified program using References in C++ #include void compare(int a, int b, int &add_great, int &add_small) { if (a > b) { add_great = a; add_small = b; } else { add_great = b; add_small = a; } } // Driver code int main() { int great, small, x, y; printf("Enter two numbers: "); scanf("%d%d", &x, &y); // The last two arguments are passed // by giving addresses of memory locations compare(x, y, great, small); printf("The greater number is %d and the" "smaller number is %d", great, small); return 0; }輸出:
Enter two numbers: 5 8The greater number is 8 and the smaller number is 52)使用Class和Object返回多個值:這個想法類似于結構。我們使用兩個整數變量創建一個類,并將較大和較小的值存儲到這些變量中,然后使用該結構的值。
// Modified program using class #include class GreaterSmaller { public: int greater, smaller; }; GreaterSmaller findGreaterSmaller(int a, int b) { GreaterSmaller s; if (a > b) { s.greater = a; s.smaller = b; } else { s.greater = b; s.smaller = a; } return s; } // Driver code int main() { int x, y; GreaterSmaller result; printf("Enter two numbers: "); scanf("%d%d", &x, &y); // The last two arguments are passed // by giving addresses of memory locations result = findGreaterSmaller(x, y); printf("The greater number is %d and the" "smaller number is %d", result.greater, result.smaller); return 0; }輸出:
Enter two numbers: 5 8The greater number is 8 and the smaller number is 53)使用STL元組返回多個值:這個想法類似于結構。我們用兩個整數變量創建一個元組并返回該元組,然后在main函數內部,我們使用tie fucntion將值分配給函數返回的min和max。
// Modified program using C++ STL tuple #include #include using namespace std; tuple findGreaterSmaller(int a, int b) { if (a < b) { return make_tuple(a, b); } else { return make_tuple(b, a); } } // Driver code int main() { int x = 5, y= 8; int max, min; tie(min, max) = findGreaterSmaller(x, y); printf("The greater number is %d and the " "smaller number is %d", max, min); return 0; }輸出:
The greater number is 8 and the smaller number is 5總結
以上是生活随笔為你收集整理的c++中函数放在等号右边_如何从C或C++中的函数返回多个值?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: idea源码注释乱码_idea中文注释出
- 下一篇: mysql主从切换gtid不一致_GTI