Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if string is in string (list of strings)

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?

like image 210
Sumyjkl Avatar asked Jan 25 '13 04:01

Sumyjkl


People also ask

How do you check if a string is present in a list of strings?

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.

How do you check if elements in a list are in a string?

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.

How do you check if multiple strings are in a 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.

How do you check if a string matches any string in a list Java?

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.


3 Answers

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.
like image 139
Jerry Coffin Avatar answered Sep 25 '22 23:09

Jerry Coffin


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.

like image 37
chris Avatar answered Sep 22 '22 23:09

chris


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))
like image 25
billz Avatar answered Sep 26 '22 23:09

billz