Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.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++,我需要做一个函数,命名为fun1,它将返回2个值。它将每10次返回A,否则将返回B 然后,在另一个函数中,我们称之为fun2,我需要检查 iffun1==A 返回变量; 其他的 返回变量; 使用string/int/which函数返回值对我来说不起作用,因为它总是需要一个参数,并且只返回一个。我认为空虚是一条路要走。我曾考虑过使用数组,但对此我不是很确定 提前感谢。您可以使用“按引用传递”来返回参数中的值 void fun1( int &firstReturnVal, int &

我需要做一个函数,命名为fun1,它将返回2个值。它将每10次返回A,否则将返回B

然后,在另一个函数中,我们称之为fun2,我需要检查 iffun1==A 返回变量; 其他的 返回变量;

使用string/int/which函数返回值对我来说不起作用,因为它总是需要一个参数,并且只返回一个。我认为空虚是一条路要走。我曾考虑过使用数组,但对此我不是很确定


提前感谢。

您可以使用“按引用传递”来返回参数中的值

void fun1( int &firstReturnVal, int &secondReturnVal, int pass ) {
   firstReturnVal = ...; // will be viewable by caller
   secondReturnVal = ...; // will be viewable by caller
}

int main() {
   int a, b;
   fun1( a, b, 2 );
   std::cout << "return values: " << a << b << endl;
}

它将每10次返回A,否则将返回B

如果您希望一次只返回一个值,但要计算函数已被调用的次数,那么您需要的是一个静态变量。 您可以使用这个在两次调用之间保持其值的静态变量来计算函数已被调用的次数,然后使用if-else语句来确定应该返回哪个函数

如果您想一次返回多个值,除了返回一个数组(实际上只返回一个元素,即指向数组的指针)之外,没有其他方法,但是您可以在该数组中存储任意多的元素,然后在函数外部检索它们

int fun1()
{
   static int times = 0;
   times++;
   if(times % 10 == 0)
   {
       //return 10 if on multiples of 10
       return 10;
   }

   //return something else.
   return 1;
}
对于复杂的东西,使用else


我想这就是你想要的,但是你可能想回顾一下静态和任何看起来不熟悉的东西。你的学校/tut/老师只给出了解决作业所需的最低要求,然后以简单的方式进行教学,通常的做法是C++是一种强类型语言,因此首先需要确定a和B是否为同一类型。你不能有一个函数,返回两个不同的东西没有一些诡计,除非这些东西可以很容易地转换

long f(bool condition) {
    if (condition)
        return 2.0; // type double
    else
        return 1; // type int
}
编译器可能会生成有关此代码的警告,但它会编译。但是,返回的内容由函数的指纹决定:此函数返回long,因此2.0转换为2,2从int转换为long

int f(bool condition) {
    if (condition)
        return 1;
    else
        return "hello"; // illegal: no conversion possible
}
下一步:您指定的这些符号是否表示字符“a”或字符串a或在其他地方定义并命名为大写字母a的变量

我只能假设,但看起来你想要一个角色“a”。如果您熟悉其他语言,比如Python,您可能没有意识到字符a和包含字符a的字符串a、'a'和a之间有很大的区别

根据你的课程有多早,你需要的可能是:

int times = 0; // caveat: this value can become negative.
// caveat: this value can only hold positive numbers 0 thru 2^31

char fn1() {
    ++times;
    if (times < 10)
        return 'A';
    else {
        //times = 0; // reset for next time
        return 'B';
    }
}
#include <iostream>

int aVariableINeed = 10;
int bVariableINeed = 20;

int times = 0;

char fn1() {
    ++times;
    if (times < 10)
        return 'A';
    else {
        return 'B';
    }
}

int fn2() {
    if (fn1() == 'A') {
        return aVariableINeed;
    } else {
        return bVariableINeed;
    }
}

int main() {
    for (int i = 0; i < 10; ++i) {
        std::cout << i << ": " << fn2() << "\n";
    }
}
我们可以把所有这些放在一个这样的程序中:

int times = 0; // caveat: this value can become negative.
// caveat: this value can only hold positive numbers 0 thru 2^31

char fn1() {
    ++times;
    if (times < 10)
        return 'A';
    else {
        //times = 0; // reset for next time
        return 'B';
    }
}
#include <iostream>

int aVariableINeed = 10;
int bVariableINeed = 20;

int times = 0;

char fn1() {
    ++times;
    if (times < 10)
        return 'A';
    else {
        return 'B';
    }
}

int fn2() {
    if (fn1() == 'A') {
        return aVariableINeed;
    } else {
        return bVariableINeed;
    }
}

int main() {
    for (int i = 0; i < 10; ++i) {
        std::cout << i << ": " << fn2() << "\n";
    }
}
<>这是不健康的,现代C++。这就是你的教授在你的舞台上可能想要的

#include <iostream>
#include <algorithm>

// Returns "first" for the first N times and then returns "second.
// Thus Countdown('A', 'B', 10) returns 'A' 9 times and then 'B' the 10th.
class Countdown
{
    char m_first, m_second;
    size_t m_times = 0;  // How many times we've displayed
public:
    Countdown(char first_, char second_, size_t times_) noexcept
        : m_first(first_), m_second(second_)
        , m_times(std::max(times_, size_t(1)) - 1)
        {
        }

    char first() const { return m_first; }
    char second() const { return m_second; }

    char next() noexcept {
        if (m_times > 0) {
            --m_times;
            return m_first;
        }
        return m_second;
    }
};

int fn2(Countdown& countdown, int aVariable, int bVariable) {
    return (countdown.next() != countdown.second())
        ? aVariable : bVariable;
}

int main()
{
    Countdown countdown('A', 'B', 10);
    for (size_t i = 1; i <= 10; ++i) {
        std::cout << i << ": " << fn2(countdown, 10, 20) << "\n";
    }
}

C标记与此相关吗?函数不需要参数,尽管它只返回0或1个值。但是你的问题还不清楚——你在编写一个只返回十分之一次a的函数时有问题吗?还是别的?A和B应该是相同的类型。static,您可能希望在函数中使用static。它有很多用途,这取决于它的位置used@RSahu当前位置我认为工会不会有帮助。OP需要一个函数,在某些调用中返回一个值,在其他调用中返回不同的值。这两个值大概是同一类型的。这是一个比我发布的更好的方法。当他学习内存管理和速度时,这会更好。你忘记了增加次数