C++ 存储一个函数的迭代结果,并在被另一个函数调用时使用它们

C++ 存储一个函数的迭代结果,并在被另一个函数调用时使用它们,c++,loops,C++,Loops,我有三个功能,例如: void firstFunc(entry) { this function returns list of entries that are stored in an array } void secondFunc() { while (loop x number of times) { firstFunc(the value here gets updated with the next entry every loop);

我有三个功能,例如:

void firstFunc(entry) {
     this function returns list of entries that are stored in an array
}

void secondFunc() {
    while (loop x number of times) {
        firstFunc(the value here gets updated with the next entry every loop);
        compare the updated entry with another local parameter that is set within this method;
        if lesser { do something; }
    }     
}

void thirdFunc() {
    while (loop x number of times) {
        firstFunc(the value here gets updated with the next entry every loop);
        compare the updated entry with another local parameter that is set within this method;
        if greater { do something; }
    }     
}
这里,“firstFunc”循环两次(一次在“secondFunc”中,然后在“thirdFunc”中)。我想要的是这样的东西:

void firstFunc(entry) {
     this function returns list of entries that are stored in an array
}

while (loop x number of times) {
        firstFunc(the value here gets updated with the next entry every loop);
}

void secondFunc() {
    while (loop x number of times) {
        use the value next updated value from firstFunc;
        compare the updated entry with another local parameter that is set within this method;
        if lesser { do something; }
    }     
}

void thirdFunc() {
    while (loop x number of times) {
        use the value next updated value from firstFunc;
        compare the updated entry with another local parameter that is set within this method;
        if greater { do something; }
    }     
}

在上面的代码中,firstFunc的循环只执行一次,而不是在secondFunc和thirdFunc中执行。我的问题是如何实现这一点?

您可以使用数组来记忆每个firstFunc()的结果


在第二个和第三个函数中,使用数组中的值,而不是再次调用该函数。

您可以使用数组来记录每个firstFunc()的结果


在第二个和第三个函数中,使用数组中的值,而不是再次调用函数。

您的意思是存储
firstFunc
的结果,以便在以后给定相同的参数时,它只重用上次的结果吗?如上所述,值在“secondFunc”中进行迭代,然后在“thirdFunc”中从头开始进行迭代。这花了太多时间。我只是不想在“thirdFunc”中从头再重复一遍。secondFunc和thirdFunc中b的值永远不会改变…那么你又在问什么?我在第一次写的时候犯了错误。我刚刚编辑了firstFunc()、secondFunc和thirdFunc。因此,在secondFunc和thirdFunc的每次迭代中,我们将在数组b中获得下一个值,该值将与s1和s2进行比较。您能否发布一些实际的代码,以便我们不会对您试图执行的操作感到困惑?您的意思是存储
firstFunc
的结果,以便在以后给定相同的参数时,它只是重用上一次的结果?在上面,值在“secondFunc”中被迭代,然后在“thirdFunc”中从一开始就被迭代。这花了太多时间。我只是不想在“thirdFunc”中从头再重复一遍。secondFunc和thirdFunc中b的值永远不会改变…那么你又在问什么?我在第一次写的时候犯了错误。我刚刚编辑了firstFunc()、secondFunc和thirdFunc。因此,在secondFunc和thirdFunc中的每次迭代中,我们将在数组b中获得下一个值,该值将与s1和s2进行比较。您能否发布一些实际代码,以便我们不会对您尝试执行的操作感到困惑?
memoArr = []
while (loop x number of times) {
    memoArr[x] = firstFunc(the value here gets updated with the next entry every loop);
}