+ 5

How to check an element to be in an array or not in C++?

listStr = ["hi", "hello", "bye"] elt = "hi" if elt in listStr : print("yes") else: print("no")

2nd Jun 2017, 3:11 AM
School Dog
2 Answers
+ 1
//https://code.sololearn.com/cQwlCmY1xAPN/#cpp #include <iostream> using namespace std; int main() { string listStr[] = {"hi", "hello", "bye"}; string elt = "hi"; int x = 0; for(int i = 0; i < sizeof(listStr) / sizeof(listStr[0]); i++) { if (listStr[i] == elt) { cout << "yes" << endl; } else { x++; } if (x == sizeof(listStr) / sizeof(listStr[0])) { cout << "no" << endl; } } return 0; }
2nd Jun 2017, 5:28 AM
Joseph P French
Joseph P French - avatar
+ 1
tks
2nd Jun 2017, 8:30 AM
School Dog