Is it possible in C++?

How will you find if a value exists or not in an array without iterating in C++ like in python we can do it using the “in” membership operator?
If(string in arr):

1 Like

Yes it is possible and using a little bit of google will go a long way.

  1. std::count() in C++ STL - GeeksforGeeks (gives you count)
  2. (gives you occurrence as true/false)

You can use the find() function as follows to check if an element is in array or not.

 * Assume that we have declared an array arr and initialized it with some elements
 * We can use the find() function in the following way to check if an element exists
 * in the array arr:
  // checking whether 10 exists in array arr
 if(find(begin(arr),end(arr),10)!=end(arr)) {

The other answers are right but i would suggest you to go through Maps (ordered and unordered) and Sets(ordered and unordered) in c++ STL. Because the find() function in array takes o(n) time but since you have an string to search for it’ll take even more because it’ll have to compare each character of the string.

Normally find() takes -:
o(n) to find in an array or vector.
o(logn) to find in an ordered set/map or simply set/map
o(1) to find in an unordered set/map

map–> ,

set–> ,

But remember since you’re searching for a string the time taken would be o(t(n)*size_of_string) . To search for a string the best way is to create a hash table for it.

Hope this would help you.
ps:- maps in c++ are just dictionary in python

1 Like

Thanks Bro!! the unordered one is really good.