为什么在 C++ 中找不到向量

发布于 2024-09-05 05:26:16 字数 32 浏览 3 评论 0 原文

还有什么选择呢?

我应该自己写吗?

what's the alternative?

Should I write by myself?

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

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

发布评论

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

评论(6

最终幸福 2024-09-12 05:26:16

std::find() 算法,它在迭代器范围内执行线性搜索,例如,

std::vector<int> v;

// Finds the first element in the vector that has the value 42:
// If there is no such value, it == v.end()
std::vector<int>::const_iterator it = std::find(v.begin(), v.end(), 42);

如果您的向量已排序,则可以使用 std::binary_search() code> 测试向量中是否存在某个值,使用 std::equal_range() 获取向量中具有该值的元素范围的开始和结束迭代器。

There is the std::find() algorithm, which performs a linear search over an iterator range, e.g.,

std::vector<int> v;

// Finds the first element in the vector that has the value 42:
// If there is no such value, it == v.end()
std::vector<int>::const_iterator it = std::find(v.begin(), v.end(), 42);

If your vector is sorted, you can use std::binary_search() to test whether a value is present in the vector, and std::equal_range() to get begin and end iterators to the range of elements in the vector that have that value.

我也只是我 2024-09-12 05:26:16

没有 vector::find 的原因是因为没有相对于 std::find 的算法优势(std::find >O(N) 并且一般来说,对于向量你不能做得更好)。

但是你拥有 map::find 的原因是因为它可以更高效(map::findO(log N) 所以你总是想在地图上使用它而不是 std::find)。

The reason there is no vector::find is because there is no algorithmic advantage over std::find (std::find is O(N) and in general, you can't do better for vectors).

But the reason you have map::find is because it can be more efficient (map::find is O(log N) so you would always want to use that over std::find for maps).

梦屿孤独相伴 2024-09-12 05:26:16

谁告诉你的? C++中有一个向量的“查找”算法。 讽刺的是巧合的是,它被称为std::find。或者也许是std::binary_search。或者其他什么,取决于向量中存储的数据的属性。

仅当算法的有效实现以某种方式与容器的内部细节相关联时,容器才会获得自己特定版本的通用算法(作为容器方法实现)。 std::list<>::sort 就是一个例子。

在所有其他情况下,算法都是由独立函数实现的。

Who told you that? There's is "find" algorithm for vector in C++. Ironically Coincidentally, it is called std::find. Or maybe std::binary_search. Or something else, depending on the properties of the data stored in your vector.

Containers get their own specific versions of generic algorithms (implemented as container methods) only when the effective implementation of the algorithm is somehow tied to the internal details of the container. std::list<>::sort would be one example.

In all other cases, the algorithms are implemented by standalone functions.

撩心不撩汉 2024-09-12 05:26:16

在容器类中具有“查找”功能违反了“SRP'(单一职责原则)。容器的核心功能是提供用于存储、检索容器中元素的接口。 “查找”、“排序”、“迭代”等不是任何容器的核心功能,因此不是其直接接口的一部分。

然而,正如 'Herb' 在命名空间原则中所述,'find' 是接口通过在与“vector”相同的命名空间中定义,即“std”。

Having a 'find' functionality in the container class violates 'SRP' (Single Responsibility Principle). A container's core functionality is to provide interfaces for storage, retrieval of elements in the container. 'Finding', 'Sorting', 'Iterating' etc are not core functionality of any container and hence not part of it's direct interface.

However as 'Herb' states in Namespace Principle, 'find' is a part of the interface by being defined in the same namespace as 'vector' namely 'std'.

疧_╮線 2024-09-12 05:26:16

使用 std::find(vec.begin(), vec.end(), value) 。

并且不要忘记包含

Use std::find(vec.begin(), vec.end(), value).

And don't forget to include <algorithm>

风铃鹿 2024-09-12 05:26:16

有什么替代方案吗?

该标准提供了 std::find,用于对相似元素(或类似元素)的任意序列进行顺序搜索。

这可以应用于所有支持迭代器的容器,但对于内部排序的容器(如 std::map),可以优化搜索。在这种情况下,容器提供它自己的 find 成员函数。

为什么C++中找不到向量?

创建 std::vector::find 是没有意义的,因为实现与 std::find(vector.begin(), vector. end(), value_to_find);

我应该自己写吗?

不可以。除非您有特定的限制或要求,否则您应该尽可能使用 STL 实现。

what's the alternative?

The standard offers std::find, for sequential search over arbitrary sequences of like-elements (or something like that).

This can be applied to all containers supporting iterators, but for internally sorted containers (like std::map) the search can be optimized. In that case, the container offers it's own find member function.

why there is no find for vector in C++?

There was no point in creating a std::vector<???>::find as the implementation would be identical to std::find(vector.begin(), vector.end(), value_to_find);.

Should I write by myself?

No. Unless you have specific limitations or requirements, you should use the STL implementation whenever possible.

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