A class:
class foo{
public:
int data;
};
Now I want to add a method to this class, to do some comparison, to see if its data is equal to on
I would recommend to use standard container like std::vector, but that would still imply a linear complexity with worst-case runtime of O(N)
.
class Foo{
public:
int data;
bool is_equal_to_one_of_these(const std::vector& arguments){
bool matched = false;
for(int arg : arguments){ //if you are not using C++11: for(int i = 0; i < arguments.size(); i++){
if( arg == data ){ //if you are not using C++11: if(arguments[i] == data){
matched = true;
}
}
return matched;
}
};
std::vector exampleRange{ {1,2,3,4,5} };
Foo f;
f.data = 3;
std::cout << f.is_equal_to_one_of_these(exampleRange); // prints "true"