C++ 选择第一个非空元素
[问题根据更新的要求更新]
我已经实现了以下函数,它应该返回第一个非空元素或抛出异常。
你还能发明更经典、更短的名字,比如“max”、“min”、“pair”吗?
template <typename T>
T select_first_not_empty( const T& a, const T&b )
{
static T null = T();
if ( a == null && b == null )
throw std::runtime_error( "null" );
return
a != null ? a : b;
}
int main()
{
const int a1 = 2;
const int b1 = 0;
const int* a2 = 0;
const int* b2 = new int(5);
const std::string a3 = "";
const std::string b3 = "";
std::cout << select_first_not_empty( a1, b1 ) << std::endl;
std::cout << select_first_not_empty( a2, b2 ) << std::endl;
std::cout << select_first_not_empty( a3, b3 ) << std::endl;
return 0;
}
[question update according to updated requirements]
I've implemented following function which should return either first not null element or throw an exception.
Also could you invent more classic and shorter name like 'max', 'min', 'pair'?
template <typename T>
T select_first_not_empty( const T& a, const T&b )
{
static T null = T();
if ( a == null && b == null )
throw std::runtime_error( "null" );
return
a != null ? a : b;
}
int main()
{
const int a1 = 2;
const int b1 = 0;
const int* a2 = 0;
const int* b2 = new int(5);
const std::string a3 = "";
const std::string b3 = "";
std::cout << select_first_not_empty( a1, b1 ) << std::endl;
std::cout << select_first_not_empty( a2, b2 ) << std::endl;
std::cout << select_first_not_empty( a3, b3 ) << std::endl;
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你可以尝试下一步
you can try do next
如果 T 的 ctor 做了任何重要的事情,那么看起来您每次都通过“select_first_not_empty”执行了三次。如果您正在寻找更好的名称,Oracle 会调用类似的“COALESCE” 。
但我不确定重点是什么。 如果我真的想知道某些内容是否已设置,我会使用可为空的指针而不是引用。 与使用带内值(如 0 或“”)相比,“NULL”更能表明不设置变量的意图。
If the ctor for T does anything significant, it appears like you're doing it three times every time through "select_first_not_empty".Oracle calls something similar "COALESCE", if you're looking for a better name.
I'm not sure what the point is, though. If I really wanted to know whether something was set or not, I'd use nullable pointers rather than references. "NULL" is a far better indicator of the intent to not have the variable set than to use an in-band value like 0 or "".
C# 有一个类似功能的内置运算符
??
,我认为它称为合并。Perl 的
||
(短路逻辑 OR)运算符也具有类似的功能:它返回第一个参数的值,而不是返回 0 或 1:返回 7,而不是 1 或
true
作为 C\C++ 或 C# 程序员的期望。C++ 内置的与此最接近的算法是 find_if 算法:
C# has a similarly functioning built-in operator
??
, which I believe is called coalesce.Perl's
||
(short-circuit logical OR) operator also has similar functionality: instead of return 0 or 1, it returns the value of the first argument evaluating to true:returns 7, not 1 or
true
as a C\C++ or C# programmer would expect.The closest thing to this that C++ has built in is the find_if algorithm: