如何查明某个项目是否存在于 std::vector 中?
我想做的就是检查向量中是否存在元素,这样我就可以处理每种情况。
if ( item_present )
do_this();
else
do_that();
All I want to do is to check whether an element exists in the vector or not, so I can deal with each case.
if ( item_present )
do_this();
else
do_that();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(21)
您可以使用:
std::find
来自 < code>这将返回一个指向找到的第一个元素的迭代器。 如果不存在,它将返回一个到最后一位的迭代器。 以你的例子:
You can use
std::find
from<algorithm>
:This returns an iterator to the first element found. If not present, it returns an iterator to one-past-the-last. With your example:
正如其他人所说,使用 STL
find
或find_if
函数。 但是,如果您正在搜索非常大的向量并且这会影响性能,您可能需要对向量进行排序,然后使用binary_search
,lower_bound
,或upper_bound
算法。As others have said, use the STL
find
orfind_if
functions. But if you are searching in very large vectors and this impacts performance, you may want to sort your vector and then use thebinary_search
,lower_bound
, orupper_bound
algorithms.如果你的向量没有排序,请使用 MSN 建议的方法:
如果你的向量是有序的,请使用二元搜索方法 Brian Neal 建议:
二分搜索产生 O(log n) 最坏情况性能,这比第一种方法更有效。 为了使用二分搜索,您可以使用 qsort 首先对向量进行排序以保证它是有序的。
If your vector is not ordered, use the approach MSN suggested:
If your vector is ordered, use binary_search method Brian Neal suggested:
binary search yields O(log n) worst-case performance, which is way more efficient than the first approach. In order to use binary search, you may use qsort to sort the vector first to guarantee it is ordered.
使用stl的算法头中的find。我已经用int类型说明了它的使用。 您可以使用任何您喜欢的类型,只要您可以比较相等性(如果您的自定义类需要,则重载==)。
Use find from the algorithm header of stl.I've illustrated its use with int type. You can use any type you like as long as you can compare for equality (overload == if you need to for your custom class).
在 C++11 中,您可以使用
any_of
。 例如,如果它是一个vectorv;
然后:或者,使用 lambda:
In C++11 you can use
any_of
. For example if it is avector<string> v;
then:Alternatively, use a lambda:
我使用这样的东西......
这样它实际上就清晰易读。
(显然您可以在多个地方重复使用该模板)。
I use something like this...
...as that way it's actually clear and readable.
(Obviously you can reuse the template in multiple places).
在 C++23 中,我们终于有了最明显的解决方案:
In C++23 we finally have the most obvious solution:
下面是一个适用于任何容器的函数:
请注意,您可以使用 1 个模板参数,因为您可以从容器中提取
value_type
。 您需要typename
因为Container::value_type
是 从属名称。Here's a function that will work for any Container:
Note that you can get away with 1 template parameter because you can extract the
value_type
from the Container. You need thetypename
becauseContainer::value_type
is a dependent name.从 C++20 开始,使用范围 (
#include
)From C++20, using ranges (
#include <ranges>
)请记住,如果您要进行大量查找,则 STL 容器更适合。 我不知道您的应用程序是什么,但像 std::map 这样的关联容器可能值得考虑。
std::vector 是首选容器,除非您有另一个原因,并且按值查找可能就是这样的原因。
Bear in mind that, if you're going to be doing a lot of lookups, there are STL containers that are better for that. I don't know what your application is, but associative containers like std::map may be worth considering.
std::vector is the container of choice unless you have a reason for another, and lookups by value can be such a reason.
通过 boost,您可以使用
any_of_equal
:With boost you can use
any_of_equal
:使用 STL find 函数。
请记住,还有一个 find_if 函数,如果您的搜索更为复杂,即如果您不仅仅是寻找一个元素,而是想查看是否有一个满足特定条件的元素,例如以“abc”开头的字符串。 (
find_if
会给你一个指向第一个这样的元素的迭代器)。Use the STL find function.
Keep in mind that there is also a find_if function, which you can use if your search is more complex, i.e. if you're not just looking for an element, but, for example, want see if there is an element that fulfills a certain condition, for example, a string that starts with "abc". (
find_if
would give you an iterator that points to the first such element).你可以试试这个代码:
You can try this code:
您可以使用
find
函数,该函数位于 < code>std 命名空间,即std::find
。 您将要搜索的向量中的begin
和end
迭代器以及您正在查找的元素传递给std::find
函数for 并将生成的迭代器与向量的末尾进行比较,看看它们是否匹配。您还可以取消引用该迭代器并正常使用它,就像任何其他迭代器一样。
You can use the
find
function, found in thestd
namespace, iestd::find
. You pass thestd::find
function thebegin
andend
iterator from the vector you want to search, along with the element you're looking for and compare the resulting iterator to the end of the vector to see if they match or not.You're also able to dereference that iterator and use it as normal, like any other iterator.
您也可以使用计数。
它将返回向量中存在的项目数。
You can use count too.
It will return the number of items present in a vector.
我个人最近使用模板来同时处理多种类型的容器,而不是只处理向量。 我在网上发现了一个类似的例子(不记得在哪里),所以功劳归于我从谁那里偷来的。 这种特殊的模式似乎也可以处理原始数组。
I've personally used templates of late to handle multiple types of containers at once rather than deal only with vectors. I found a similar example online (can't remember where) so credit goes to whoever I've pilfered this from. This particular pattern seems to handle raw arrays as well.
(C++17 及更高版本):
也可以使用
std::search
这对于搜索元素序列也很有用。
还可以灵活地传递一些搜索算法。 参考这里。
https://en.cppreference.com/w/cpp/algorithm/search
(C++17 and above):
can use
std::search
alsoThis is also useful for searching sequence of elements.
Also there is flexibility of passing some search algorithms. Refer here.
https://en.cppreference.com/w/cpp/algorithm/search
如果你想在向量中找到一个字符串:
If you wanna find a string in a vector:
另一种方法是使用
std::count
。示例代码:
不可否认,在处理大向量时,此方法可能比 std::find 慢。
Another way to do it is using
std::count
.Example code:
Admittedly, this method might be slower than
std::find
when dealing with large vectors.这条路怎么样?
How about this way?