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);
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.
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).
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.
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'.
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.
发布评论
评论(6)
有
std::find()
算法,它在迭代器范围内执行线性搜索,例如,如果您的向量已排序,则可以使用
std::binary_search()
code> 测试向量中是否存在某个值,使用std::equal_range()
获取向量中具有该值的元素范围的开始和结束迭代器。There is the
std::find()
algorithm, which performs a linear search over an iterator range, e.g.,If your vector is sorted, you can use
std::binary_search()
to test whether a value is present in the vector, andstd::equal_range()
to get begin and end iterators to the range of elements in the vector that have that value.没有
vector::find
的原因是因为没有相对于std::find
的算法优势(std::find
是>O(N)
并且一般来说,对于向量你不能做得更好)。但是你拥有
map::find
的原因是因为它可以更高效(map::find
是O(log N)
所以你总是想在地图上使用它而不是 std::find)。The reason there is no
vector::find
is because there is no algorithmic advantage overstd::find
(std::find
isO(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
isO(log N)
so you would always want to use that overstd::find
for maps).谁告诉你的? C++中有一个
向量
的“查找”算法。讽刺的是巧合的是,它被称为std::find
。或者也许是std::binary_search
。或者其他什么,取决于向量中存储的数据的属性。仅当算法的有效实现以某种方式与容器的内部细节相关联时,容器才会获得自己特定版本的通用算法(作为容器方法实现)。
std::list<>::sort
就是一个例子。在所有其他情况下,算法都是由独立函数实现的。
Who told you that? There's is "find" algorithm for
vector
in C++.IronicallyCoincidentally, it is calledstd::find
. Or maybestd::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.
在容器类中具有“查找”功能违反了“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'.
使用 std::find(vec.begin(), vec.end(), value) 。
并且不要忘记包含
Use
std::find(vec.begin(), vec.end(), value)
.And don't forget to include
<algorithm>
该标准提供了 std::find,用于对相似元素(或类似元素)的任意序列进行顺序搜索。
这可以应用于所有支持迭代器的容器,但对于内部排序的容器(如
std::map
),可以优化搜索。在这种情况下,容器提供它自己的find
成员函数。创建
std::vector::find
是没有意义的,因为实现与std::find(vector.begin(), vector. end(), value_to_find);
。不可以。除非您有特定的限制或要求,否则您应该尽可能使用 STL 实现。
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 ownfind
member function.There was no point in creating a
std::vector<???>::find
as the implementation would be identical tostd::find(vector.begin(), vector.end(), value_to_find);
.No. Unless you have specific limitations or requirements, you should use the STL implementation whenever possible.