14

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?

cigien
  • 57,834
  • 11
  • 73
  • 112
Sumyjkl
  • 141
  • 1
  • 1
  • 4

8 Answers8

21

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.
Simon Klaver
  • 480
  • 5
  • 24
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • +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) – Chubsdad Jan 25 '13 at 04:34
  • 2
    @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. – Jerry Coffin Jan 25 '13 at 04:36
  • Add header file `` – tjysdsg Aug 14 '19 at 13:31
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))
billz
  • 44,644
  • 9
  • 83
  • 100
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.

chris
  • 60,560
  • 13
  • 143
  • 205
  • @Potatoswatter, True, I'll add that in with the std::array note. – chris Jan 25 '13 at 04:23
  • Thanks, although I was hoping for something a little simpler, I guess I'll have to try to understand how to use that. – Sumyjkl Jan 25 '13 at 04:23
  • @Sumyjkl: it's as simple as in Python, the only difference is the syntax but what is happening is really the same. Using C++ without using STL is like using python without its included API – Jack Jan 25 '13 at 04:24
  • @Sumyjkl, It's just the initial concept to get over. `find()` searches through the range you specify (more easily noted with the begin and end functions) and returns the position (an iterator) at which it's found, or the end of the range if it is not. That's why you have to compare the result with the ending position you pass in. The whole `sizeof` business divides the total size of the array by the size of one element, giving the number of elements, but as mentioned, the standard containers expose an easy `size()` member, though it's not too useful in this context. – chris Jan 25 '13 at 04:27
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.

Hu Xixi
  • 1,799
  • 2
  • 21
  • 29
1

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()) {
    ..
  }
Jack
  • 131,802
  • 30
  • 241
  • 343
1

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
        }
  • This does execute without any problems while also being **simple to beginners**, it is less logical than other solutions suggested on this post. – Andrew Jul 15 '23 at 09:18
0
if (find(mylist, &mylist[3], myinput) != &mylist[3])
    ...
user1610015
  • 6,561
  • 2
  • 15
  • 18
0
#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
    }
}
Denis
  • 5,894
  • 3
  • 17
  • 23