In Ruby I can do:
在Ruby中,我可以做到:
[1,2,3,4].include?(4) #=>True
In Haskell I can do :
在Haskell我能做到:
4 `elem` [1,2,3,4] #=> True
What should I do in C++?
我该怎么做C ++?
5 个解决方案
#1
8
There isn't a built-in function doing exactly that. There is std::find
which comes close, but since it doesn't return a bool
it is a bit more awkward to use.
没有内置函数正是这样做的。有一个std :: find接近,但由于它没有返回bool,使用起来有点尴尬。
You could always roll your own, to get syntax similar to JIa3ep's suggestion, but without using count
(which always traverses the entire sequence):
您可以随时滚动自己,获得类似于JIa3ep建议的语法,但不使用count(总是遍历整个序列):
template <typename iter_t>
bool contains(iter_t first, iter_t last, typename iter_t::value_type val){
return find(first, last, val) != last;
}
Then you can simply do this to use it:
然后你可以简单地这样做来使用它:
std::vector<int> x;
if (contains(x.begin(), x.end(), 4)) {...}
#2
20
Here an example using find:
这是一个使用find的例子:
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> Num(4);
//insert values
Num[0]=1;
Num[1]=2;
Num[2]=3;
Num[3]=4;
std::vector<int>::iterator p = find(Num.begin(), Num.end(), 4);
if (p == Num.end())
std::cout << "Could not find 4 in the vector" << std::endl;
else
std::cout << "Have found 4 in the vector" << std::endl;
return 0;
}
#3
2
If the vector is ordered, you can also use std::binary_search.
如果向量是有序的,您也可以使用std :: binary_search。
std::binary_search(vec.begin(), vec.end(), 4) // Returns true or false
#4
1
To get similar syntax as in OP's question:
要获得与OP的问题类似的语法:
std::vector<int> x;
if ( count( x.begin(), x.end(), VAL_TO_FIND ) ) {
// found
} else {
// not found
}
#5
0
You could use std::set This has a find() method.
你可以使用std :: set这有一个find()方法。
#1
8
There isn't a built-in function doing exactly that. There is std::find
which comes close, but since it doesn't return a bool
it is a bit more awkward to use.
没有内置函数正是这样做的。有一个std :: find接近,但由于它没有返回bool,使用起来有点尴尬。
You could always roll your own, to get syntax similar to JIa3ep's suggestion, but without using count
(which always traverses the entire sequence):
您可以随时滚动自己,获得类似于JIa3ep建议的语法,但不使用count(总是遍历整个序列):
template <typename iter_t>
bool contains(iter_t first, iter_t last, typename iter_t::value_type val){
return find(first, last, val) != last;
}
Then you can simply do this to use it:
然后你可以简单地这样做来使用它:
std::vector<int> x;
if (contains(x.begin(), x.end(), 4)) {...}
#2
20
Here an example using find:
这是一个使用find的例子:
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> Num(4);
//insert values
Num[0]=1;
Num[1]=2;
Num[2]=3;
Num[3]=4;
std::vector<int>::iterator p = find(Num.begin(), Num.end(), 4);
if (p == Num.end())
std::cout << "Could not find 4 in the vector" << std::endl;
else
std::cout << "Have found 4 in the vector" << std::endl;
return 0;
}
#3
2
If the vector is ordered, you can also use std::binary_search.
如果向量是有序的,您也可以使用std :: binary_search。
std::binary_search(vec.begin(), vec.end(), 4) // Returns true or false
#4
1
To get similar syntax as in OP's question:
要获得与OP的问题类似的语法:
std::vector<int> x;
if ( count( x.begin(), x.end(), VAL_TO_FIND ) ) {
// found
} else {
// not found
}
#5
0
You could use std::set This has a find() method.
你可以使用std :: set这有一个find()方法。