This question already has an answer here:
这个问题已经有了答案:
- Return a “NULL” object if search result not found 8 answers
- 如果搜索结果没有找到8个答案,返回一个“NULL”对象。
I have a struct like this
我有一个这样的结构。
struct AUTO{
int weight;
string name;
}
and I have a function that return a struct by passing name
我有一个函数,它通过名称返回struct。
AUTO autoByName(String name, AUTO a[], int ll)
{
for(int i = 0; i < ll; i++)
{
if (name == a[i].name)
{
return a[i];
}
}
// AND HERE ?????
}
but I don't know what return at the end of the function. Is there a null
for struct ?
但我不知道函数结束时返回的是什么。结构是否为空?
1 个解决方案
#1
2
You have a lot of options:
你有很多选择:
- Give
AUTO
a way to represent a "no-value". For example by making weight -1. Or by adding a flag to it. This is pretty ugly; I don't recommend it, even though it's somewhat common in practice. - 给AUTO一种表示“无价值”的方法。举个例子,通过权重-1。或者添加一个标志。这是相当丑陋的;我不推荐它,尽管这在实践中很常见。
- Return a (smart) pointer instead of value. This way you can return a
nullptr
in the case of error / no value. This is also ugly because it forces you to deal with pointers for not a very good reason. - 返回一个(智能)指针,而不是值。这样,您可以在错误/没有值的情况下返回一个nullptr。这也很难看,因为它迫使你去处理指针,这不是一个很好的理由。
- Return a separate value to indicate success. For example adding a
bool &wasFound
argument to the function. - 返回一个单独的值来表示成功。例如,在函数中添加bool和wasfound参数。
- Return
boost::optional
orstd::pair<AUTO, bool>
to accomplish pretty much the same thing, but all wrapped up in the return value. This is probably the cleanest. This is howstd::map::find
works, for example. -
返回boost::可选或std::pair
完成几乎相同的事情,但都包在返回值中。这可能是最干净的。这就是std::map::find works,例如。 ,> - Throw an exception. It's possible that this is acceptable if callers don't try to find items that don't exist. In other words, if "not found" is just a return value then don't throw an exception. If "not found" is truly a program error condition, throwing an exception may actually make sense. If you're not sure, don't throw an exception.
- 抛出异常。如果调用者不尝试查找不存在的项,这是可以接受的。换句话说,如果“not found”只是一个返回值,那么不要抛出异常。如果“未找到”确实是一个程序错误条件,抛出异常可能是有意义的。如果你不确定,不要抛出异常。
I'd recommend #4, or in rarer cases #5.
我推荐#4,或者更少的案例#5。
#1
2
You have a lot of options:
你有很多选择:
- Give
AUTO
a way to represent a "no-value". For example by making weight -1. Or by adding a flag to it. This is pretty ugly; I don't recommend it, even though it's somewhat common in practice. - 给AUTO一种表示“无价值”的方法。举个例子,通过权重-1。或者添加一个标志。这是相当丑陋的;我不推荐它,尽管这在实践中很常见。
- Return a (smart) pointer instead of value. This way you can return a
nullptr
in the case of error / no value. This is also ugly because it forces you to deal with pointers for not a very good reason. - 返回一个(智能)指针,而不是值。这样,您可以在错误/没有值的情况下返回一个nullptr。这也很难看,因为它迫使你去处理指针,这不是一个很好的理由。
- Return a separate value to indicate success. For example adding a
bool &wasFound
argument to the function. - 返回一个单独的值来表示成功。例如,在函数中添加bool和wasfound参数。
- Return
boost::optional
orstd::pair<AUTO, bool>
to accomplish pretty much the same thing, but all wrapped up in the return value. This is probably the cleanest. This is howstd::map::find
works, for example. -
返回boost::可选或std::pair
完成几乎相同的事情,但都包在返回值中。这可能是最干净的。这就是std::map::find works,例如。 ,> - Throw an exception. It's possible that this is acceptable if callers don't try to find items that don't exist. In other words, if "not found" is just a return value then don't throw an exception. If "not found" is truly a program error condition, throwing an exception may actually make sense. If you're not sure, don't throw an exception.
- 抛出异常。如果调用者不尝试查找不存在的项,这是可以接受的。换句话说,如果“not found”只是一个返回值,那么不要抛出异常。如果“未找到”确实是一个程序错误条件,抛出异常可能是有意义的。如果你不确定,不要抛出异常。
I'd recommend #4, or in rarer cases #5.
我推荐#4,或者更少的案例#5。