Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从函数原型返回未知数量的值 我是C++初学者。我试图创建一个返回数字因子的函数原型。然而,我见过的所有从函数原型返回多个值的方法都需要提前知道将返回的值的数量,这对于因式分解是不可能的_C++ - Fatal编程技术网

从函数原型返回未知数量的值 我是C++初学者。我试图创建一个返回数字因子的函数原型。然而,我见过的所有从函数原型返回多个值的方法都需要提前知道将返回的值的数量,这对于因式分解是不可能的

从函数原型返回未知数量的值 我是C++初学者。我试图创建一个返回数字因子的函数原型。然而,我见过的所有从函数原型返回多个值的方法都需要提前知道将返回的值的数量,这对于因式分解是不可能的,c++,C++,我在Python中所做的是: def findfac(n): factors = [1, n] # Make an array i = 2.0 while i < n: if i not in factors and (n/i).is_integer(): factors += [i, n/i] i += 1 return factors def findfac(n): 因子=[1,n]#构成一个

我在Python中所做的是:

def findfac(n):
    factors = [1, n]   # Make an array
    i = 2.0
    while i < n:
        if i not in factors and (n/i).is_integer():
            factors += [i, n/i]
        i += 1
    return factors
def findfac(n):
因子=[1,n]#构成一个数组
i=2.0
而i
这将返回一个包含n的所有因子的数组

问题是,尽管我可以将不确定的元素数量转储到Python数组中,但是C++数组的最大大小。那么,有没有一种方法可以在C++中返回动态数组,或者我应该重新思考我的方法吗?

< p>你在寻找:

#包括
标准::向量findfac(int n)
{
std::向量因子{1,n};//因子=[1,n]
int i;
//待办事项
factors.push_back(i);//factors=[1,n,…,i]
//待办事项
返回因子;//因子=[n的因子…]
}

您正在寻找:

#包括
标准::向量findfac(int n)
{
std::向量因子{1,n};//因子=[1,n]
int i;
//待办事项
factors.push_back(i);//factors=[1,n,…,i]
//待办事项
返回因子;//因子=[n的因子…]
}


返回一个指针(只要它是动态地分配了
malloc()
系列中的内容)应该可以,但是长度是未知的,除非这可能是函数(作为指针)的一个参数,该函数也填充了大小。尝试返回一个指针(只要它是动态地分配了
malloc()
系列中的内容)应该可以,但是长度是未知的,除非这可能是函数(作为指针)的一个参数,该函数也填充了大小。试试看。我发现了以下错误:
错误:无法将'std::ostream{aka std::basic_ostream}'左值绑定到'std::basic_ostream&&'
错误:初始化'std::basic_ostream&std:'的参数1:operator@thequantumforge不能直接输出向量。您必须单独输出每个元素:
for(inti:factors){std::cout我得到了这些错误:
error:cannotbind'std::ostream{aka std::basic_ostream}'左值为'std::basic_ostream&&'
错误:初始化'std::basic_ostream&std:'的参数1:operator@thequantumforge不能直接输出向量。必须单独输出每个元素:
for(inti:factors){std::cout
#include <vector>

std::vector<int> findfac(int n)
{
    std::vector<int> factors{1, n}; // factors = [1, n]
    int i;
    // TODO
    factors.push_back(i); // factors = [1, n, ..., i]
    // TODO
    return factors; // factors = [factors of n...]
}