[英]Check if element is in the list (contains)
I've got a list of elements, say, integers and I want to check if my variable (another integer) is one of the elements from the list.我有一个元素列表,比如整数,我想检查我的变量(另一个整数)是否是列表中的元素之一。 In python I'd do:
在 python 我会这样做:
my_list = [1,2,3,4] # elements
my_var = 3 # my variable
my_var in my_list # returns boolean
How to do that in C++?如何在 C++ 中做到这一点? I thought of using
std::list
, but I can find no find
method in it.我想过使用
std::list
,但我找不到其中的find
方法。 I can see such method in std::set
structure.我可以在
std::set
结构中看到这样的方法。
More deeply, the problem is that my program is given some unique ids (a list, a set, whatever) and I iterate over a long list of input data (ids) and check if they are included in the list (boolean value returned for each iteration step).更深入地说,问题是我的程序被赋予了一些唯一的 id(一个列表,一个集合,等等)并且我遍历一长串输入数据(id)并检查它们是否包含在列表中(返回的布尔值每个迭代步骤)。 And I'm not sure how should I do that in C++.
而且我不确定在 C++ 中我应该怎么做。
You can use std::find
您可以使用
std::find
bool found = (std::find(my_list.begin(), my_list.end(), my_var) != my_list.end());
You need to include <algorithm>
.您需要包含
<algorithm>
。 It should work on standard containers, vectors lists, etc...它应该适用于标准容器、向量列表等...
std::list
does not provide a search method. std::list
不提供搜索方法。 You can iterate over the list and check if the element exists or use std::find
.您可以遍历列表并检查元素是否存在或使用
std::find
。 But I think for your situation std::set
is more preferable.但我认为对于你的情况
std::set
更可取。 The former will take O(n)
time but later will take O(lg(n))
time to search.前者将花费
O(n)
时间,但后者将花费O(lg(n))
时间进行搜索。
You can simply use:您可以简单地使用:
int my_var = 3;
std::set<int> mySet {1, 2, 3, 4};
if(mySet.find(myVar) != mySet.end()){
//do whatever
}
你必须#include <algorithm>
,然后你可以使用std::find
They really should add a wrapper.他们真的应该添加一个包装器。 Like this:
像这样:
namespace std
{
template<class _container,
class _Ty> inline
bool contains(_container _C, const _Ty& _Val)
{return std::find(_C.begin(), _C.end(), _Val) != _C.end(); }
};
...
if( std::contains(my_container, what_to_find) )
{
}
A one-liner solution, similar to python, would be (std::set<int> {1, 2, 3, 4}).count(my_var) > 0
.类似于 python 的单行解决方案是
(std::set<int> {1, 2, 3, 4}).count(my_var) > 0
。
Minimal working example最小工作示例
int my_var = 3;
bool myVarIn = (std::set<int> {1, 2, 3, 4}).count(my_var) > 0;
std::cout << std::boolalpha << myVarIn << std::endl;
prints true
or false
dependent of the value of my_var.根据 my_var 的值打印
true
或false
。
Use std::find
, something like:使用
std::find
,例如:
if (std::find(std::begin(my_list), std::end(my_list), my_var) != std::end(my_list))
// my_list has my_var
Declare additional helper function like this:像这样声明额外的辅助函数:
template <class T, class I >
bool vectorContains(const vector<T>& v, I& t)
{
bool found = (std::find(v.begin(), v.end(), t) != v.end());
return found;
}
And use it like this:并像这样使用它:
void Project::AddPlatform(const char* platform)
{
if (!vectorContains(platforms, platform))
platforms.push_back(platform);
}
Snapshot of example can be found here:示例快照可以在这里找到:
https://github.com/tapika/cppscriptcore/blob/b7f3d62747494a52a440482e841ffb016a3fc56e/SolutionProjectModel/Project.cpp#L13 https://github.com/tapika/cppscriptcore/blob/b7f3d62747494a52a440482e841ffb016a3fc56e/SolutionProjectModel/Project.cpp#L13
Since C++20 you can use l.contains(my_var)从 C++20 开始,您可以使用 l.contains(my_var)
https://www.modernescpp.com/index.php/more-convenience-functions-for-containers-with-c-20 https://www.modernescpp.com/index.php/more-convenience-functions-for-containers-with-c-20
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.