日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

c++中函数放在等号右边_如何从C或C++中的函数返回多个值?

發布時間:2025/3/20 c/c++ 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 5

2)使用結構返回多個值:由于結構是用戶定義的數據類型。這個想法是用兩個整數變量定義一個結構,并將較大和較小的值存儲到這些變量中,然后使用該結構的值。

// 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 5

3)使用數組返回多個值(僅當返回的項屬于同一類型時有效):當數組作為參數傳遞時,其基地址將傳遞給函數,因此對數組副本所做的任何更改都將在原始數組中更改。

以下是使用數組返回多個值的程序,即在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 5

2)使用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 5

3)使用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++中的函数返回多个值?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。