我要做的就是检查向量中是否存在元素,这样我就可以处理每种情况。
if ( item_present )
do_this();
else
do_that();
您可以使用 <algorithm>
中的 std::find
:
#include <algorithm>
#include <vector>
vector<int> vec;
//can have other data types instead of int but must same datatype as item
std::find(vec.begin(), vec.end(), item) != vec.end()
这会将迭代器返回到找到的第一个元素。如果不存在,它会返回一个迭代器到最后一个。用你的例子:
#include <algorithm>
#include <vector>
if ( std::find(vec.begin(), vec.end(), item) != vec.end() )
do_this();
else
do_that();
正如其他人所说,使用 STL find
或 find_if
函数。但是,如果您在非常大的向量中进行搜索并且这会影响性能,您可能需要对向量进行排序,然后使用 binary_search
、lower_bound
或 upper_bound
算法。
如果您的向量未排序,请使用 MSN 建议的方法:
if(std::find(vector.begin(), vector.end(), item)!=vector.end()){
// Found the item
}
如果您的向量是有序的,请使用 Brian Neal 建议的 binary_search 方法:
if(binary_search(vector.begin(), vector.end(), item)){
// Found the item
}
二分搜索产生 O(log n) 最坏情况的性能,这比第一种方法更有效。为了使用二分查找,您可以先使用 qsort 对向量进行排序,以保证它是有序的。
使用 stl 的算法头中的 find。我已经说明了它与 int 类型的用法。您可以使用任何您喜欢的类型,只要您可以比较是否相等(如果您需要为您的自定义类重载 ==)。
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
typedef vector<int> IntContainer;
typedef IntContainer::iterator IntIterator;
IntContainer vw;
//...
// find 5
IntIterator i = find(vw.begin(), vw.end(), 5);
if (i != vw.end()) {
// found it
} else {
// doesn't exist
}
return 0;
}
在 C++11 中,您可以使用 any_of
。例如,如果它是 vector<string> v;
,则:
if (any_of(v.begin(), v.end(), bind(equal_to<string>(), _1, item)))
do_this();
else
do_that();
或者,使用 lambda:
if (any_of(v.begin(), v.end(), [&](const std::string& elem) { return elem == item; }))
do_this();
else
do_that();
std::find()
,为什么还要使用 std::any_of()
?
std::any_of
时还要使用 std::find()
?
我用这样的东西......
#include <algorithm>
template <typename T>
const bool Contains( std::vector<T>& Vec, const T& Element )
{
if (std::find(Vec.begin(), Vec.end(), Element) != Vec.end())
return true;
return false;
}
if (Contains(vector,item))
blah
else
blah
...那样它实际上是清晰易读的。 (显然你可以在多个地方重用模板)。
value_type
作为元素类型,则可以使用 1 个模板参数。我已经添加了这样的答案。
if true return true else return false
。该方法可以是一个:return std::find(Vec.begin(), Vec.end(), Element) != Vec.end();
这是一个适用于任何容器的函数:
template <class Container>
const bool contains(const Container& container, const typename Container::value_type& element)
{
return std::find(container.begin(), container.end(), element) != container.end();
}
请注意,您可以使用 1 个模板参数,因为您可以从容器中提取 value_type
。您需要 typename
,因为 Container::value_type
是 dependent name。
ranges
,所以它可以被称为 Range
而不是 Container
,并且 Bob 是你的叔叔。
请记住,如果您要进行大量查找,那么 STL 容器会更适合您。我不知道您的应用程序是什么,但像 std::map 这样的关联容器可能值得考虑。
std::vector 是首选容器,除非您有另一个原因,并且按值查找可能是这样的原因。
借助 boost,您可以使用 any_of_equal
:
#include <boost/algorithm/cxx11/any_of.hpp>
bool item_present = boost::algorithm::any_of_equal(vector, element);
你可以试试这段代码:
#include <algorithm>
#include <vector>
// You can use class, struct or primitive data type for Item
struct Item {
//Some fields
};
typedef std::vector<Item> ItemVector;
typedef ItemVector::iterator ItemIterator;
//...
ItemVector vtItem;
//... (init data for vtItem)
Item itemToFind;
//...
ItemIterator itemItr;
itemItr = std::find(vtItem.begin(), vtItem.end(), itemToFind);
if (itemItr != vtItem.end()) {
// Item found
// doThis()
}
else {
// Item not found
// doThat()
}
您可以使用 std
命名空间中的 find
函数,即 std::find
。您将要搜索的向量中的 begin
和 end
迭代器以及要查找的元素传递给 std::find
函数,并将生成的迭代器与向量的末尾进行比较以查看它们是否匹配或不是。
std::find(vector.begin(), vector.end(), item) != vector.end()
您还可以取消引用该迭代器并像任何其他迭代器一样正常使用它。
您也可以使用计数。它将返回向量中存在的项目数。
int t=count(vec.begin(),vec.end(),item);
find
比 count
快,因为它不会在第一场比赛后继续计数。
template <typename T> bool IsInVector(const T & what, const std::vector<T> & vec)
{
return std::find(vec.begin(),vec.end(),what)!=vec.end();
}
nullptr
?根本没有必要。此外,您复制 T what
,这可能很昂贵并且是不必要的工作。这两个参数都应该是 const
引用而不是它们当前的引用。最后,我不知道为什么人们会写 if (condition) return true; else return false;
,而他们本来可以写 return condition;
。
.
而不是 ->
。
std::find()
调整为可用于容器,请以一般方式进行,而不仅仅是向量。或许可以称它为 find()
或 stdx::find()
之类的。
我个人最近使用模板来一次处理多种类型的容器,而不是只处理向量。我在网上找到了一个类似的例子(不记得在哪里)所以归功于我从谁那里偷来的。这种特殊的模式似乎也可以处理原始数组。
template <typename Container, typename T = typename std::decay<decltype(*std::begin(std::declval<Container>()))>::type>
bool contains(Container && c, T v)
{
return std::find(std::begin(c), std::end(c), v) != std::end(c);
}
template <typename Container> struct value_type { ... etc. ... }
如果你想在向量中找到一个字符串:
struct isEqual
{
isEqual(const std::string& s): m_s(s)
{}
bool operator()(OIDV* l)
{
return l->oid == m_s;
}
std::string m_s;
};
struct OIDV
{
string oid;
//else
};
VecOidv::iterator itFind=find_if(vecOidv.begin(),vecOidv.end(),isEqual(szTmp));
std::find
在这种情况下很好,不需要谓词对象。
(C++17 及以上):
也可以使用 std::search
这对于搜索元素序列也很有用。
#include <algorithm>
#include <iostream>
#include <vector>
template <typename Container>
bool search_vector(const Container& vec, const Container& searchvec)
{
return std::search(vec.begin(), vec.end(), searchvec.begin(), searchvec.end()) != vec.end();
}
int main()
{
std::vector<int> v = {2,4,6,8};
//THIS WORKS. SEARCHING ONLY ONE ELEMENT.
std::vector<int> searchVector1 = {2};
if(search_vector(v,searchVector1))
std::cout<<"searchVector1 found"<<std::endl;
else
std::cout<<"searchVector1 not found"<<std::endl;
//THIS WORKS, AS THE ELEMENTS ARE SEQUENTIAL.
std::vector<int> searchVector2 = {6,8};
if(search_vector(v,searchVector2))
std::cout<<"searchVector2 found"<<std::endl;
else
std::cout<<"searchVector2 not found"<<std::endl;
//THIS WILL NOT WORK, AS THE ELEMENTS ARE NOT SEQUENTIAL.
std::vector<int> searchVector3 = {8,6};
if(search_vector(v,searchVector3))
std::cout<<"searchVector3 found"<<std::endl;
else
std::cout<<"searchVector3 not found"<<std::endl;
}
还有传递一些搜索算法的灵活性。参考这里。
https://en.cppreference.com/w/cpp/algorithm/search
从 C++20 开始,使用范围 (#include <ranges>
)
//SAMPLE DATA
std::vector<int> vecOfElements = { 2,4,6,8 };
//DO SOMETHING IF 8 IN VECTOR
if (std::ranges::find(vecOfElements, 8) != vecOfElements.end())
{
std::cout << "DO SOMETHING" << std::endl;
}
#include <algorithm>
否则您可能会遇到非常奇怪的错误,例如“在命名空间 std 中找不到匹配的函数”.find()
仍然不是std::vector
的成员函数,这是否没有让任何人感到困扰,正如您所期望的那样?我想知道这是否是模板化的结果。std::vector<>::find()
不会带来任何好处,也不需要它,因此,不,它不应该是成员。另请参阅en.wikipedia.org/wiki/Coupling_%28computer_programming%29mvec.find(key) != mvec.cend()
优于std::find(mvec.cbegin(), mvec.cend(), key) != mvec.cend()
。