日韩性视频-久久久蜜桃-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++中的函数返回多个值?的全部內容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 国产免费av网站 | 大战熟女丰满人妻av | 啪网站 | 国产日韩欧美电影 | 欧美成人三级伦在线观看 | 亚洲在线网站 | 精品一区二区av | 视频一区欧美 | 校园春色综合网 | 色婷婷av一区二区三区大白胸 | 精品综合网 | 青青草手机视频在线观看 | 黄色免费网站 | 韩国久久久 | 好吊妞在线观看 | 久久国产色av | 亚洲图片综合区 | 丰满肥臀噗嗤啊x99av | 久久免费网 | 波多野结衣视频在线观看 | 欧美日韩精 | 国产精品日韩欧美一区二区三区 | 国产精品海角社区 | 成人91在线| 91精品视频网 | 久久久久久草 | 欧美一区二区三区在线观看视频 | 97在线免费观看视频 | 丁香激情网 | 老司机一区| 亚洲国产第一页 | 午夜精品久久久久久久99黑人 | 亚洲欧美影院 | 亚洲精品日产精品乱码不卡 | 久久免费激情视频 | 欧美 亚洲 视频 | 丰满岳乱妇一区二区三区 | 久久成人久久爱 | 91亚洲精品国偷拍 | 精品人妻无码一区二区性色 | 蜜桃色999 | 无码人妻精品一区二区三区温州 | 好吊一区二区三区视频 | 国产精品人八做人人女人a级刘 | 成人乱码一区二区三区av | 丰满少妇在线观看资源站 | youjizz.com国产| 人人艹在线观看 | 91玉足脚交白嫩脚丫 | 靠逼网站在线观看 | 日韩精品在线视频免费观看 | 日韩视频精品在线 | 午夜亚洲av永久无码精品 | 国产精品500部 | 中文字幕在线2018 | 国产系列在线观看 | 久久99精品久久久久久琪琪 | 国产一区二区三区在线免费观看 | 一区二区三区xxx | 国产精品免费在线 | 在线视频观看一区二区 | 人妻内射一区二区在线视频 | 日日拍拍| 国产精品毛片久久久久久久av | 日本内谢少妇xxxxx少交 | 99免费国产 | 国产精品麻豆一区二区三区 | 在线黄色网页 | 美女大逼| 九九九九九精品 | 中文在线国产 | 欧美又粗又深又猛又爽啪啪九色 | 国产女主播视频 | 正在播放一区 | 日本泡妞xxxx免费视频软件 | 欧美一区二区高清视频 | 少妇人妻好深好紧精品无码 | 日韩久久精品 | 91在线观看免费视频 | 欧美久久久久久久 | 嫩草在线观看 | 精品国产乱子伦 | 午夜久久久精品 | 波多野结衣视频在线 | 午夜视频在线播放 | 欧美理论在线观看 | 国产成人片 | 国产成人无码一区二区在线观看 | 国产高清免费在线观看 | 久久精品黄aa片一区二区三区 | 亚洲精品久久久蜜桃 | 一区二区三区在线观看av | 国产chinese| 国产午夜不卡 | 亚洲超碰av| 国产人成| 国产欧美一区二区精品性色99 | 亚洲女同志亚洲女同女播放 | 免费精品一区二区 |