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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...