我想做的就是检查一个元素是否存在于向量中,所以我可以处理每个案例.
if ( item_present )
do_this();
else
do_that();
Run Code Online (Sandbox Code Playgroud)
MSN*_*MSN 873
您可以使用std::find从<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()
Run Code Online (Sandbox Code Playgroud)
这将返回一个bool(true如果存在,false否则).用你的例子:
#include <algorithm>
#include <vector>
if ( std::find(vec.begin(), vec.end(), item) != vec.end() )
do_this();
else
do_that();
Run Code Online (Sandbox Code Playgroud)
Bri*_*eal 109
正如其他人所说,使用STL find或find_if函数.但是,如果你在非常大的矢量搜索,这会影响性能,您可能要排序的载体,然后使用binary_search,lower_bound或upper_bound算法.
m-s*_*arp 46
使用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;
}
Run Code Online (Sandbox Code Playgroud)
Bin*_*eng 38
如果您的矢量未订购,请使用MSN建议的方法:
if(std::find(vector.begin(), vector.end(), item)!=vector.end()){
// Found the item
}
Run Code Online (Sandbox Code Playgroud)
如果您的矢量是有序的,请使用binary_search方法Brian Neal建议:
if(binary_search(vector.begin(), vector.end(), item)){
// Found the item
}
Run Code Online (Sandbox Code Playgroud)
二进制搜索产生O(log n)最坏情况性能,这比第一种方法更有效.为了使用二进制搜索,您可以使用qsort首先对向量进行排序以保证它是有序的.
And*_*wel 21
我用这样的东西......
#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
Run Code Online (Sandbox Code Playgroud)
......就这样,它实际上清晰可读.(显然你可以在多个地方重用模板).
Pav*_*aka 13
从 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;
}
Run Code Online (Sandbox Code Playgroud)
Deq*_*ing 11
在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();
Run Code Online (Sandbox Code Playgroud)
Mar*_*rst 11
这是一个适用于任何Container的函数:
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();
}
Run Code Online (Sandbox Code Playgroud)
请注意,您可以使用1个模板参数,因为您可以value_type从Container中提取.你需要typename因为Container::value_type是一个从属名称.
Dav*_*ley 10
请记住,如果您要进行大量查找,那么有更好的STL容器.我不知道你的应用程序是什么,但像std :: map这样的关联容器可能值得考虑.
std :: vector是选择的容器,除非你有另一个的理由,并且按值查找可能是这样的原因.
在 C++23 中,我们终于有了最明显的解决方案:
if (std::ranges::contains(vec, item))
do_this();
else
do_that();
Run Code Online (Sandbox Code Playgroud)
使用boost可以使用any_of_equal:
#include <boost/algorithm/cxx11/any_of.hpp>
bool item_present = boost::algorithm::any_of_equal(vector, element);
Run Code Online (Sandbox Code Playgroud)
小智 5
你可以试试这段代码:
#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()
}
Run Code Online (Sandbox Code Playgroud)
您可以使用find在std命名空间中找到的函数,即std::find. 您将要搜索的向量中的std::find函数begin和end迭代器以及要查找的元素传递给函数,并将生成的迭代器与向量的末尾进行比较以查看它们是否匹配。
std::find(vector.begin(), vector.end(), item) != vector.end()
Run Code Online (Sandbox Code Playgroud)
您还可以取消引用该迭代器并正常使用它,就像任何其他迭代器一样。
| 归档时间: |
|
| 查看次数: |
875833 次 |
| 最近记录: |