'How to check if a string is in a 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?



Solution 1:[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:[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.

Solution 3:[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))

Solution 4:[4]

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 5:[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()) {
    ..
  }

Solution 6:[6]

if (find(mylist, &mylist[3], myinput) != &mylist[3])
    ...

Solution 7:[7]

#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
    }
}

Solution 8:[8]

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
        }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Simon Klaver
Solution 2
Solution 3
Solution 4 Hu Xixi
Solution 5 Jack
Solution 6 user1610015
Solution 7 Denis
Solution 8 Sajjad Heydari