Check if there is a line in the line (list of lines)

I am new here and in C ++. I had some experience with python, and I found "if a in b" very simple, and I am wondering if there is an equivalent in C ++.

Background

I am trying to make a list of strings and check if the entry is in this list. The reason I want to do this is because I only want to use a function if the input really does something in this function. (In this case, change the coordinates of int x and y)

Question

string myinput; string mylist[]={"a", "b", "c"}; cin>>myinput; //if myinput is included in mylist //do other stuff here 

How to check with if myinput input is myinput in mylist string?

+8
source share
8 answers

You can 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 just fine with only three lines, but if you have a lot more, you'd better use std::set or std::unordered_set .

 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. 
+13
source

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 in advance, I suggest std::array , which provides iterators and the size() function, as well as several other advantages over built-in arrays. Note that this is only C ++ 11 (the equivalent of C ++ 03 is std::vector ), and also with C ++ 11 comes std::begin and std::end , which reduces it to the following:

 if (std::find(std::begin(mylist), std::end(mylist), myinput) != std::end(mylist)) 

In C ++ 03, it's quite simple to make your own for inline arrays, but with a standard container that provides the begin() and end() members, this should not be too necessary, although it is more universal.

+2
source

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)) 
+2
source

Since you are working with C ++, feel free to use 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()) { .. } 
+1
source
 if (find(mylist, &mylist[3], myinput) != &mylist[3]) ... 
0
source
 #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 } } 
0
source

You can use find, as suggested by others, or you can use a for loop (easier for beginners):

 for (int i = 0; i < mylist.size() ; i ++){ if (myinput == mylist[i]){ //Do Stuff } 
0
source

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 number > 0 , it means the string is in the strings.

0
source

All Articles