gpt4 book ai didi

C++ 返回 std::pair?

转载 作者:搜寻专家 更新时间:2023-10-31 00:59:59 27 4
gpt4 key购买 nike

#include <iostream>
#include <string>
#include <utility>
using namespace std;
string num1="123456789123456789";
std::pair<int*,int*> cpy(){
int a[(num1.size()%9==0)? num1.size()/9 : num1.size()/9+1];
int b[(num1.size()%9==0)? num1.size()/9 : num1.size()/9+1];
return make_pair(a,b);
}
int main(void){
return 0;
}
-------------------------------------------------------
//if by this style, it can be compiled
std::pair<int*,int*> cpy(){
const int N=5;
int a[5];
int b[5];
return make_pair(a,b);

我正在编写一个程序来计算大数,例如 19933231289234783278,所以我需要使用 1 000 000 000 系统拆分数字

为什么不能通过这种方式返回?

最佳答案

您不能以这种方式返回一对,因为您传递了错误的类型。在这种情况下,数组不会衰减为指针。

如果将函数的最后一行更改为:

return make_pair(&a[0],&b[0]);

它会编译,但它仍然无法工作,因为您将返回指向数组的指针,一旦函数 cpy() 结束,这些数组就会被销毁。

顺便说一下,可变长度数组不是标准的 C++。

关于C++ 返回 std::pair<int *,int *>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32197535/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com