如何找出std :: vector中是否存在项目?
我要做的就是检查矢量中是否存在一个元素,以便我可以处理每种情况。
if ( item_present )
do_this();
else
do_that();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我要做的就是检查矢量中是否存在一个元素,以便我可以处理每种情况。
if ( item_present )
do_this();
else
do_that();
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(21)
您可以使用
std :: find
><算法> :这将返回到第一个元素。如果不存在,它会将迭代器返回到持久的一半。与您的示例:
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_if
functions。但是,如果您要在非常大的向量中搜索并影响性能,则可能需要对矢量进行分类,然后使用binary_search
, 或算法。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建议的方法:
如果您的向量是订购的,请使用binary_search方法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的算法标题中查找。我已经说明了其与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
。例如,如果是vector&lt; string&gt; v;
然后:或者使用lambda:
In C++11 you can use
any_of
. For example if it is avector<string> v;
then:Alternatively, use a lambda:
在C ++ 23中,我们终于有一个体面的解决方案:
In C++23 we finally have a decent solution:
我使用这样的东西...
...这样它实际上清晰可读。
(显然,您可以在多个位置重复使用模板)。
I use something like this...
...as that way it's actually clear and readable.
(Obviously you can reuse the template in multiple places).
从C ++ 20,使用范围(
#include&lt; ranges&gt;
)From C++20, using ranges (
#include <ranges>
)这是一个适用于任何容器的函数:
请注意,您可以使用1个模板参数逃脱,因为您可以从容器中提取
value_type
。您需要typename
因为容器:: 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.请记住,如果您要进行很多查找,那么有一些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_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:
您可以使用
查找
代码> std 名称空间,即std ::查找
。您通过std :: find
函数开始
和end
从您要搜索的向量以及您正在查找的元素中读取器因为并将结果迭代器与向量的末端进行比较,以查看它们是否匹配。您还能够像其他任何迭代器一样放弃迭代器并正常使用它。
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/algorithm/searchm/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 ::与大型矢量打交道时
。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?