Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,057 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,577 people online right now. Registration is fast and FREE... Join Now!




lexicographical_compare problem with functor

 
Reply to this topicStart new topic

lexicographical_compare problem with functor, Using function object with compare failing to match

jwwicks
4 Aug, 2008 - 04:35 AM
Post #1

D.I.C Head
Group Icon

Joined: 31 Jul, 2008
Posts: 59



Thanked: 6 times
Dream Kudos: 200
My Contributions
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
User is offlineProfile CardPM
+Quote Post

Bench
RE: Lexicographical_compare Problem With Functor
4 Aug, 2008 - 10:15 AM
Post #2

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 617



Thanked: 14 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
I think you misunderstand the meaning of 'lexicographical'. The algorithm should return true when the second string is lexicographically greater than the first - meaning that if the two strings were placed in 'dictionary order', the second string would come after the first.

eg, if the first string were "Hallowed" and the second string were "Hollowed", lexicographical_compare would return true. since 'Ho' is lexicographically greater than 'Ha'


I can't see why you even need to use a compare algorithm for this, it should be enough just to use the == operator to compare the two strings
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 )
{
return element.id == m_id.id || element.description == m_id.description;
}
};


This post has been edited by Bench: 4 Aug, 2008 - 10:19 AM
User is offlineProfile CardPM
+Quote Post

jwwicks
RE: Lexicographical_compare Problem With Functor
4 Aug, 2008 - 11:02 AM
Post #3

D.I.C Head
Group Icon

Joined: 31 Jul, 2008
Posts: 59



Thanked: 6 times
Dream Kudos: 200
My Contributions
Hello Bench,

QUOTE(Bench @ 4 Aug, 2008 - 11:15 AM) *

I think you misunderstand the meaning of 'lexicographical'. The algorithm should return true when the second string is lexicographically greater than the first - meaning that if the two strings were placed in 'dictionary order', the second string would come after the first.

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 )
{
return element.id == m_id.id || element.description == m_id.description;
}
};



Changed as specified...

Thanks for the bump on the head for the lexi algorithm though. Must have been asleep smile.gif

Actually on second thought I needed a case insensitive compare so I wrote it this way...

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 = true;

typedef string::const_iterator SI;
SI p1 = element.id.begin();
SI p2 = m_id.begin();

while( (p1!=element.id.end() && p2!=m_id.end()) && ret_val )
{
if( toupper(*p1)!= toupper(*p2) ) ret_val = false;
++p1;
++p2;
}


if(!ret_val){
SI p1 = element.description.begin();
SI p2 = m_id.begin();
ret_val = true;

while( (p1!=element.description.end() && p2!=m_id.end()) && ret_val )
{
if( toupper(*p1)!= toupper(*p2) ) ret_val = false;
++p1;
++p2;
}
}
return ret_val;
}
};

JW

This post has been edited by jwwicks: 5 Aug, 2008 - 05:53 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 06:05PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month