Returning multiple values from a C++ function

后端 未结 21 2982
别跟我提以往
别跟我提以往 2020-11-22 01:04

Is there a preferred way to return multiple values from a C++ function? For example, imagine a function that divides two integers and returns both the quotient and the rema

21条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 01:40

    std::pair divide(int dividend, int divisor)
    {
       // :
       return std::make_pair(quotient, remainder);
    }
    
    std::pair answer = divide(5,2);
     // answer.first == quotient
     // answer.second == remainder
    

    std::pair is essentially your struct solution, but already defined for you, and ready to adapt to any two data types.

提交回复
热议问题