Hello All,
I've got the following structure...
cpp
struct product {
string id;
string description;
int quantity;
double wholesale;
double retail;
Date* date_added;
};
and I'm using the following function object to find the item in a vector of products...
cpp
class prod_eq:public std::unary_function<product, bool>
{
private:
string m_id;
public:
prod_eq( const string& n ): m_id( n ){}
bool operator()(const product& element )
{
bool ret_val = false;
if( lexicographical_compare(element.id.begin(), element.id.end(), m_id.begin(), m_id.end())
|| lexicographical_compare(element.description.begin(), element.description.end(), m_id.begin(), m_id.end())
)
ret_val = true;
return ret_val;
}
};
But calling the the functor with find_if using the vector of products, lexicographical_compare returns false. I could have sworn I had this code working but I was going to submit a tutorial on using functors and was testing my old Visual Studio 2005 code in 2008 and it's failing. Checked the values being passed in and they seem to match...
cpp
/////////////////////////
// \fn do_report( vector<product>& p )
// \brief Handling routine for report menu
//
// \b Purpose: Processes report menu choices
// \b SideEffects: none
//
// \param p - vector containing product records
// \return none
/////////////////////////
void do_report( vector<product>& p )
{
bool done = false;
int choice;
string id;
do{
//system(CLRSCR);
show_copyright();
show_report_menu();
do{
choice = get_choice("Enter your choice: ");
}while(!is_valid_choice( choice, 0, 2 ));
switch( choice ){
case 1:
case 2:{
do{
id = get_product_id();
}while(!is_valid_product_id( p, id ));
typedef vector<product>::iterator VCI;
VCI f = find_if( p.begin(), p.end(), prod_eq(id));
product& pUpdate = *f;
show_product( pUpdate );
break;
}
default:
done = true;
break;
}
}while(!done);
}
Just can't see why this would be returning false. Thanks for any help...
JW