Jump to content

C/C++ equivilent of Python's "in"


samgj
 Share

Recommended Posts

Based on Google search of the in operator:


int value[5] = { 1, 2, 3, 4, 5 };
bool member;
int variable;
for (int i = 0; i < value.size(); ++i) {
if (variable == array[i]) {
member = true;
return;
}
}

Alternatively you could create your own version of the in operator for use:

Note that you would need to make one for all the kinds of type reactions which is a pain so I would stick with defining it on the spot.

Header file:


class PythonLike {
public:
bool inOperatorInt(int i, int intArray[]);
}

cpp file:


bool PythonLike::inOperatorInt(int i, int intArray[]) {
for (int j = 0; j < intArray.size(); ++j) {
if (i == intarray[j]) {
return true;
}
}
}

Edited by MoLAoS
Link to comment
Share on other sites

This depends on what sort of data structure you are using. In C++ it is common to use the standard template library which has a built in find operator http://www.sgi.com/tech/stl/find.html . If you are using C then you may also be using a library to help with managing your data strcutres, which may also contain a find function. Otherwise you will have you write your own as MoLAoS suggests.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...