忽略 std::min 中的零并计算其他数字的最小值
Ignore zero in std::min and calculate minimum of other numbers
非零数字的最小值:
#include <iostream>
#include <algorithm>
int main() {
double s1 = 1000;
double s2 = 400;
double s3 = 300;
double s4 = 10;
double minimum = std::min({s1, s2, s3, s4});
std::cout<<minimum<<"\n";
double var = 1/minimum;
std::cout<<var;
}
这很好用 returns:
10
0.1
问题是当其中一个数字为零时:
#include <iostream>
#include <algorithm>
int main() {
double s1 = 1000;
double s2 = 400;
double s3 = 300;
double s4 = 0;
double minimum = std::min({s1, s2, s3, s4});
std::cout<<minimum<<"\n";
double var = 1/minimum;
std::cout<<var;
}
它returns:
0
inf
预期结果:
300
0.00333333
如何在计算中忽略零?
确保您没有将 0 传递给最小值。我想这是在不知道您的输入内容的情况下唯一的一般性答案。
std::min
正常工作,如果您有特殊要求,您可以传递具有以下签名的比较函数:
bool cmp(const Type1 &a, const Type2 &b);
作为第二个 std::min
函数参数(Compare
模板参数):
#include <iostream>
#include <algorithm>
int main()
{
double s1 = 1000;
double s2 = 400;
double s3 = 300;
double s4 = 0;
auto const ignore_zero = [](auto const& a, auto const& b) -> bool {
if(0 == a)
{
return false;
}
else if(0 == b)
{
return true;
}
return a < b;
};
double minimum = std::min({s1, s2, s3, s4}, ignore_zero);
std::cout << minimum << "\n";
}
但是很奇怪,如果你对代码有这样的特殊要求,我建议你:
- 尝试重新设计您的程序。
- 使用 function/functor 助手:
#include <algorithm>
#include <iostream>
#include <vector>
namespace core::helper
{
template<typename T>
struct special_find final
{
std::vector<T> nums;
explicit special_find(std::initializer_list<T> nums_)
: nums(std::move(nums_))
{
}
auto min() const
{
return *std::min_element(nums.cbegin(), nums.cend());
}
special_find& ignore(T const& value)
{
nums.erase(std::remove_if(nums.begin(), nums.end(), [&value](auto const & item)
{
return item == value;
}), nums.end());
return *this;
}
};
} // namespace core::helper
int main()
{
double s1 = 1000;
double s2 = 400;
double s3 = 300;
double s4 = 0;
auto const min = core::helper::special_find({s1, s2, s3, s4}).ignore(0).min();
std::cout << min << "\n";
}
或者像这样
#include <algorithm>
#include <limits>
#include <vector>
template<std::size_t N>
constexpr double min_no_zero(const double (&values)[N])
{
static_assert(N>0,"There must be at least one number");
double min = std::numeric_limits<double>::max();
for (const auto value : values)
{
if ( value != 0.0 )
{
min = std::min(min, value);
}
}
return min;
}
int main()
{
constexpr double s1 = 1000.0;
constexpr double s2 = 400.0;
constexpr double s3 = 0.0;
constexpr double s4 = 10.0;
static_assert( min_no_zero({ s1, s2, s3, s4 }) == 10.0);
}
非零数字的最小值:
#include <iostream>
#include <algorithm>
int main() {
double s1 = 1000;
double s2 = 400;
double s3 = 300;
double s4 = 10;
double minimum = std::min({s1, s2, s3, s4});
std::cout<<minimum<<"\n";
double var = 1/minimum;
std::cout<<var;
}
这很好用 returns:
10
0.1
问题是当其中一个数字为零时:
#include <iostream>
#include <algorithm>
int main() {
double s1 = 1000;
double s2 = 400;
double s3 = 300;
double s4 = 0;
double minimum = std::min({s1, s2, s3, s4});
std::cout<<minimum<<"\n";
double var = 1/minimum;
std::cout<<var;
}
它returns:
0
inf
预期结果:
300
0.00333333
如何在计算中忽略零?
确保您没有将 0 传递给最小值。我想这是在不知道您的输入内容的情况下唯一的一般性答案。
std::min
正常工作,如果您有特殊要求,您可以传递具有以下签名的比较函数:
bool cmp(const Type1 &a, const Type2 &b);
作为第二个 std::min
函数参数(Compare
模板参数):
#include <iostream>
#include <algorithm>
int main()
{
double s1 = 1000;
double s2 = 400;
double s3 = 300;
double s4 = 0;
auto const ignore_zero = [](auto const& a, auto const& b) -> bool {
if(0 == a)
{
return false;
}
else if(0 == b)
{
return true;
}
return a < b;
};
double minimum = std::min({s1, s2, s3, s4}, ignore_zero);
std::cout << minimum << "\n";
}
但是很奇怪,如果你对代码有这样的特殊要求,我建议你:
- 尝试重新设计您的程序。
- 使用 function/functor 助手:
#include <algorithm>
#include <iostream>
#include <vector>
namespace core::helper
{
template<typename T>
struct special_find final
{
std::vector<T> nums;
explicit special_find(std::initializer_list<T> nums_)
: nums(std::move(nums_))
{
}
auto min() const
{
return *std::min_element(nums.cbegin(), nums.cend());
}
special_find& ignore(T const& value)
{
nums.erase(std::remove_if(nums.begin(), nums.end(), [&value](auto const & item)
{
return item == value;
}), nums.end());
return *this;
}
};
} // namespace core::helper
int main()
{
double s1 = 1000;
double s2 = 400;
double s3 = 300;
double s4 = 0;
auto const min = core::helper::special_find({s1, s2, s3, s4}).ignore(0).min();
std::cout << min << "\n";
}
或者像这样
#include <algorithm>
#include <limits>
#include <vector>
template<std::size_t N>
constexpr double min_no_zero(const double (&values)[N])
{
static_assert(N>0,"There must be at least one number");
double min = std::numeric_limits<double>::max();
for (const auto value : values)
{
if ( value != 0.0 )
{
min = std::min(min, value);
}
}
return min;
}
int main()
{
constexpr double s1 = 1000.0;
constexpr double s2 = 400.0;
constexpr double s3 = 0.0;
constexpr double s4 = 10.0;
static_assert( min_no_zero({ s1, s2, s3, s4 }) == 10.0);
}