Need help for function converting

I have this function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
bool f(string a, string b) {
   for(int i=0; i<a.size(); i++)
      a[i]=tolower(a[i]);

   for(int i=0; i<b.size(); i++)
      b[i]=tolower(b[i]);
   
   if (b.size()>=a.size()
   for(int i=0; i<(b.size()-a.size()); i++)

      if(b.substr(i, a.size())==a)
         return true;
   
   else
   for(int i=0; i<(a.size()-b.size()); i++)

      if(a.substr(i, b.size())==b)
         return true;
   
   return false;
}

And I want a function, let's call it g, that statisfies the following equation for any string a and b:
(!g(a, b))&&(!g(b, a))==f(a, b)
Any help, at least pseudocode?
I assume you meant ( !g(a, b) && !g(b,a) ) == f(a, b)

Couldn't you just define g(a, b) as "!f(a,b)"?
That would't be it! Ok, assume the f() function is a kind of an equality operator (==), and g() is the less-than operator! That's what I need. Many times, when only one operator is needed to define them all, the == operator is defined as (!a<b && !b<a), so that's what I want.
Hi,

Something like that ?

1
2
3
4
5
6
7
8
9
10
11
12
bool g(string a, string b) {
   for(int i=0; i<a.size(); i++)
      a[i]=tolower(a[i]);

   for(int i=0; i<b.size(); i++)
      b[i]=tolower(b[i]);

   if ( (b.find(a) > 0 ) || (a.find(b) > 0) )
       return false;
  else 
       return true;
}


Or just ?

1
2
3
4
5
6
7
8
9
10
11
12
bool g(string a, string b) {
   for(int i=0; i<a.size(); i++)
      a[i]=tolower(a[i]);

   for(int i=0; i<b.size(); i++)
      b[i]=tolower(b[i]);

   if ( a.find(b) > 0 )
       return false;
  else 
       return true;
}

Last edited on
> when only one operator is needed to define them all ...

In general two operators would need to be defined. The standard library does not require a (less than) comparison operator to impose a strong ordering.

Perhaps what you are looking for are the operators in std::rel_ops (<utility>)
http://en.cppreference.com/w/cpp/utility/rel_ops/operator_cmp
Topic archived. No new replies allowed.