问题
I'm new here and to C++. I've had some experience in python, and found "if a in b" really easy, and I'm wondering if there's an equivalent in C++.
Background
I've been trying to make a list of strings and check if an input is in that list. The reason I want to do this is because I want to only use a function if the input will actually do something in that function. (Change int x and y coordinates in this case)
Question
string myinput;
string mylist[]={"a", "b", "c"};
cin>>myinput;
//if myinput is included in mylist
//do other stuff here
How do I check using an if
whether the input myinput
is included in string mylist
?
回答1:
You could use std::find
:
std::string myinput;
std::vector<std::string> mylist{"a", "b", "c"};
std::cin >> myinput;
if (std::find(std::begin(mylist), std::end(mylist), myinput) != std::end(mylist))
// myinput is included in mylist.
This works fine with only three strings, but if you're going to have many more, you'd probably be better off with an std::set
or std::unordered_set
instead.
std::set<std::string> myset;
// put "a", "b", and "c" into the set here
std::cin >> myinput;
if (myset.find(myinput) != myset.end())
// myinput is included in myset.
回答2:
Use std::find:
std::size_t listsize = sizeof mylist / sizeof mylist[0];
if (std::find(mylist, mylist + listsize, myinput) != mylist + listsize) {
//found
}
If you know the size of the list beforehand, I suggest std::array
which exposes iterators and a size()
function, as well as a few other benefits over built-in arrays. Note that this is C++11 only (the C++03 near equivalent is std::vector
), and also with C++11 comes std::begin
and std::end
, which reduce it to this:
if (std::find(std::begin(mylist), std::end(mylist), myinput) != std::end(mylist))
It's fairly easy to make your own for built-in arrays in C++03 as well, but with a standard container that exposes begin()
and end()
members, this shouldn't be too necessary, though it is more versatile.
回答3:
Use std::find, std::find_if algorithms
string myinput;
string mylist[]={"a", "b", "c"};
std::string *begin = mylist;
std::string *end = mylist + 3;
if (std::find(begin, end, "b") != end)
{
std::cout << "find" << std::endl;
}
Or use C++11 std::array
with std::begin()
, std::end()
std::array<std::string, 3> mylist = { "a", "b", "c" };
if (std::find(std::begin(mylist), std::end(mylist), "b") != std::end(mylist))
{
cout << "find" << endl;
}
Or Lambda:
if (std::find_if(std::begin(mylist), std::end(mylist),
[](const std::string& s){ return s == "b";}) != std::end(mylist))
回答4:
Since you are working with C++ don't hesitate in using the STL library:
string mylist[]={"a", "b", "c"};
vector<string> myvector(mylist, mylist + sizeof(mylist)/sizeof(mylist[0]));
if (find(myvector.begin(), myvector.end(), mystring) != myvector.end()) {
..
}
回答5:
if (find(mylist, &mylist[3], myinput) != &mylist[3])
...
回答6:
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string myinput;
string mylist[]={"a", "b", "c"};
if (cin >> myinput &&
std::find(std::begin(mylist), std::end(mylist), myinput) == std::end(mylist))
{
// do other stuff
}
}
回答7:
You can use find as others suggested, or you can use a for loop(easierfor beginners):
for (int i = 0; i < mylist.size() ; i ++){
if (myinput == mylist[i]){
//Do Stuff
}
回答8:
You can also use std::count
#include <iostream>
#include <algorithm> // std::count
#include <vector>
//using namespace std;
int main() {
std::vector<std::string> ans = {"a", "b", "c", "a"};
std::cout << count(ans.begin(), ans.end(), "a") << std::endl;
return 0;
}
if the number > 0
, it means the string is in strings.
来源:https://stackoverflow.com/questions/14515274/check-if-string-is-in-string-list-of-strings