Check if string is in string (list of strings)
Solution 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.
Solution 2
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.
Solution 3
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.
Solution 4
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))
Solution 5
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()) {
..
}
Admin
Updated on July 10, 2022Comments
-
Admin almost 2 years
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 inputmyinput
is included in stringmylist
? -
Chubsdad over 11 years+1: insertion in to a set wouldn't be a O(1) or O(n) though. So it depends what is really required (fast insertion / fast search)
-
Jerry Coffin over 11 years@Chubsdad: Insertion in a set would be O(log n) in the usual case. At least in a typical case, you'd expect to search more than insert anyway. Of course, if you use
unordered_set
, you can expect the insertions to be O(1) as well. -
tjysdsg over 4 yearsAdd header file
<algorithm>