In Python, doing if a in b
is really easy, and I'm wondering if there's an equivalent in C++.
Specifically, I want to make a list of strings and check if an input is in that list.
std::string myinput;
std::string mylist[] = {"a", "b", "c"};
std::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
?
Python Find String in List using count() We can also use count() function to get the number of occurrences of a string in the list. If its output is 0, then it means that string is not present in the list. l1 = ['A', 'B', 'C', 'D', 'A', 'A', 'C'] s = 'A' count = l1.
Using any() to check if string contains element from list. Using any function is the most classical way in which you can perform this task and also efficiently. This function checks for match in string with match of each element of list.
Use the all() function to check if multiple strings exist in another string, e.g. if all(substring in my_str for substring in list_of_strings): . The all() function will return True if all of the substrings exist in the string and False otherwise.
Starting with Java 8, you can use Stream API for this task. The idea is to call the anyMatch() method on the stream elements, which return true if and only if any element matches with the supplied predicate. To check for a substring, you can use the contains() method.
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.
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.
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With