c++ - 比较 4 个变量以找到最低的 C++

标签 c++

我想找到四个中最小的数字,但这看起来有点奇怪,难道没有更聪明、更短的方法吗?

这就是我所拥有的:

int findlowest(int one, int two, int three, int four) {
    int output = one //as of now , we will be outputting one , except if we find a lower score.
    if(output > two) { out = two;} // if output is proven to be bigger than two, two is our new output.
    if(output > three){ output = three;} //same operation with three
    if(output > four){ output = four;} // same operation with four
    return output;
}

最佳答案

std::min(a, std::min(b, std::min(c, d)));

包括<algorithm> .

关于c++ - 比较 4 个变量以找到最低的 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6578841/

相关文章:

c++ - 在C++中乱序乘以 vector 中的数字

c++ - 如何将内存图像分配给 DLIB array2d 或图像?

c++ - 假设没有公共(public)代码,将公共(public)代码移动到基类是好习惯吗?

c++ - OpenCL:clSetKernelArg中的CL_INVALID_ARG_SIZE

c++ - 什么是快速有效的查找项目的方法?

c++ - 返回+重置成员变量的最有效方法?

C++ SDL,为什么 SDL_LoadBMP() 返回 NULL?

c++ - 使用绝对路径加载带有 LoadLibrary() 和 GetModuleHandle() 的系统 DLL 是否能更好地防止 DLL 劫持?

c# - 在 C# 中引发事件并在 C++ MFC 中捕获

c++ - std::mem_fun 表达式的可移植性