Array Search

 

Hello! So I have this array of a struct:

 struct exmpl
   {
   double orderstoploss;
   int    orderticket;
   string ordercomm;
   };
  exmpl arr[];

And i have this function that i use for searching . Is not working on struct arrays. How can i search for a  value in a array of a struct ? thanks

  template<typename T>
  int ArraSearch( T &array,T var)
  {
  int index= WRONG_VALUE;
  for(int i=0;i<ArraySize(array);i++)
    {
    if(array[i] == var)
    index=i;
    }
    return index;

     
    }
 
Daniel Cioca: Is not working on struct arrays. How can i search for a  value in a array of a struct ? 

You can't search a struct for value. A struct is not a value.

You could search all structs for a member value, e.g. if(array[i].orderticket == value).

Or create your search criteria and use it.

template<typename T, typename UnaryPredicate>
int ArraSearch(const T &array[], UnaryPredicate& pred)
  {
  for(int i=0;i<ArraySize(array);i++) if( pred.isMember(array[i]) ) return i;
  return EMPTY_VALUE;
}
class byTicket{ int t; 
 public:           
       byTicket(int ticket) : t(ticket){}
  bool isMember(const exmpl& e){ return e.orderTicket == t; }
};

byTicket byT(ticket); int i = arraSearch(arr, byT);
 
William Roeder #:

You can't search a struct for value. A struct is not a value.

You could search all structs for a member value, e.g. if(array[i].orderticket == value).

Or create your search criteria and use it.

Thank you.

template<typename T, typename UnaryPredicate>
int ArraSearch(const T &array[], UnaryPredicate& pred)
  {
  for(int i=0;i<ArraySize(array);i++) if( pred.isMember(array[i]) ) return i;
 

Apologies, I dont understand this

 
Daniel Cioca #: Apologies, I dont understand this
  bool isMember(const exmpl& e){ return e.orderTicket == t; }
};

byTicket byT(ticket); int i = arraSearch(arr, byT);
You don't understand calling a method of a class?
 
William Roeder #:
You don't understand calling a method of a class?
Ok, I see now. I thought they are not related, I thought there are 2 separate examples … 😊
Thank you!