检查在向量向量中搜索时 std::find 是否失败

发布于 2024-12-09 09:23:06 字数 712 浏览 0 评论 0 原文

我面临的问题是 std::find 如果失败则返回 .end() ,但是如果您不知道 该怎么办> ??

要搜索的结构:hashtable(X 类向量的向量 H) 使用哈希函数找到外向量 H 的索引(i) std::find 用于通过 H[i] 进行搜索 函数签名: Lookup (hashtable H, ) [ 是查找到的元素的句柄,以便我们可以更改它的根据需要值]

vector<X>::iterator lookup (vector <vector <X>> &H, X some_value)
{
     int index = hashFn(some_value);
    return std::find(H[index].begin(), H[index].end(), some_value);
}

在这种情况下我如何检查查找是否失败? (如果我将向量更改为数组/指针,我可以简单地返回 NULL 并且交易完成,但是在返回迭代器时我该如何执行此操作?)

The problem I am facing is that std::find returns <someVector>.end() if it fails, but what if you don't know that <someVector> ??

Structure to search : hashtable (vector H of vectors of class X)
the index(i) of outer vector H is found using hash function
std::find is used to search through H[i]
function signature : <return val> lookup (hashtable H, ) [<return val> is a handle to the element found by lookup, so that we can change it's value as needed]

vector<X>::iterator lookup (vector <vector <X>> &H, X some_value)
{
     int index = hashFn(some_value);
    return std::find(H[index].begin(), H[index].end(), some_value);
}

how do i check in this case if lookup fails ?? (if I change vector to arrays/pointers, I can simply return NULL and the deal is done, but how do i do this when returning iterators ??)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

椒妓 2024-12-16 09:23:06

std::find(a,b, V) 不返回 .end();它返回第二个参数。由于您(调用者)传递了该参数,因此您始终知道这一点。

现在,在更复杂的示例中,问题似乎是 std::find() 的调用者应该返回给其调用者的内容。这确实是我们不能说的;我们甚至不知道签名。您可以返回一个空向量,默认构造(请参阅 Monish 的答案)。您可以抛出异常(如果这在概念上有意义)。您可以返回对静态空向量的 const 引用。

std::find(a,b, V) doesn't return .end(); it returns its second argument. Since you, the caller, passed that argument, you always know it.

Now, in your more complex example, it appears that the problem is what the caller of std::find() should return to its caller. That's something we can't really say; we don't even know the signature. You could return an empty vector, default constructed (see Monish's answer). You could throw an exception (if that makes sense, conceptually). You could return a const reference to a static, empty vector.

兮子 2024-12-16 09:23:06

无法访问向量本身,您可以使用迭代器做的唯一事情就是检索此迭代器指向的元素。那么为什么你不想返回 const X* 或 X* 而不是 vector::iterator 呢?如果失败,您可能只返回 0(如果您使用的是 C++11,则返回 nullptr)。那是关于界面的。您可以在搜索算法中检测 std::find 失败的方式怎么样 - 我同意 MSalters - 您的代码中的某个地方有 std::find 调用,并且参数可用 - 所以您只需将结果与该位置的第二个参数进行比较即可相等时返回零指针。

X * lookup ( vector <vector <X>> &H, X some_value )
{
  int index = hashFn(some_value);
  vector<X> &v = H[index];
  const vector<X>::iterator result = std::find( v.begin(), v.end(), some_value );
  if ( result != v.end() ) {
    return &(*result);
  } else {
    return 0;
  }
}

用法:

X * value = hash_table.lookup(...);
if ( X != 0 ) {
  // use result
} else {
  // value not found
}

再说一遍 - 如果不访问向量,您将无法以任何其他方式使用迭代器,因此使用向量 :: 迭代器作为查找的返回类型是没有意义的;

W/o being able to access vector itself the only thing you can do with iterator is to retrieve element that is pointed by this iterator. So why you don't want to return const X* or X* instead of vector::iterator? In case of fail you may just return 0 (or nullptr if you are using C++11). Thats about interface. What about the way you can detect std::find fail in your search algorithm - I agree with MSalters - there is std::find call somewhere in your code with arguments available - so you can just compare result with second argument in that place and return zero-pointer on equality.

X * lookup ( vector <vector <X>> &H, X some_value )
{
  int index = hashFn(some_value);
  vector<X> &v = H[index];
  const vector<X>::iterator result = std::find( v.begin(), v.end(), some_value );
  if ( result != v.end() ) {
    return &(*result);
  } else {
    return 0;
  }
}

Usage:

X * value = hash_table.lookup(...);
if ( X != 0 ) {
  // use result
} else {
  // value not found
}

And again - you would not be able to use iterator in any other way w/o access to vector, so it's pointless to use vector::iterator as lookup's return type;

杯别 2024-12-16 09:23:06

如果返回类型是向量,那么您可以返回空向量,以防查找失败。

返回向量();

If the return type is vector<X>, then you can return empty vector in case lookup fails.

return vector<X>();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文