includes algorithm not working properly on sets of strings

  • Follow


Hi,
just look at includes algorithm on two sets of strings. Its not
working correctly.

bool areql(string x, string y)
{
  return (x==y); //whatever i write here, i get the same result. != or
< or > why???
}
int main()
{
    set<string> coll;
    set<string> search;
    coll.insert("13");
    coll.insert("96");
    coll.insert("0");

    search.insert("17");
    search.insert("937");
    //    search.insert(7);
    // check whether all elements in search are also in coll
    if (includes (coll.begin(), coll.end(),
                  search.begin(), search.end()),areql) {
        cout << "all elements of search are also in coll"
             << endl;
    }
    else {
        cout << "not all elements of search are also in coll"
             << endl;
    }
}

0
Reply suresh.amritapuri (100) 10/28/2009 8:51:35 AM

On Oct 28, 4:51=A0pm, "suresh.amritapuri" <suresh.amritap...@gmail.com>
wrote:
> Hi,
> just look at includes algorithm on two sets of strings. Its not
> working correctly.
>
> bool areql(string x, string y)
> {
> =A0 return (x=3D=3Dy); //whatever i write here, i get the same result. !=
=3D or
> < or > why???}
>
> int main()
> {
> =A0 =A0 set<string> coll;
> =A0 =A0 set<string> search;
> =A0 =A0 coll.insert("13");
> =A0 =A0 coll.insert("96");
> =A0 =A0 coll.insert("0");
>
> =A0 =A0 search.insert("17");
> =A0 =A0 search.insert("937");
> =A0 =A0 // =A0 =A0search.insert(7);
> =A0 =A0 // check whether all elements in search are also in coll
> =A0 =A0 if (includes (coll.begin(), coll.end(),
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 search.begin(), search.end()),areql) =
{

You have typo errors.

if (includes (coll.begin(), coll.end(), search.begin(), search.end(),
areql) )...

> =A0 =A0 =A0 =A0 cout << "all elements of search are also in coll"
> =A0 =A0 =A0 =A0 =A0 =A0 =A0<< endl;
> =A0 =A0 }
> =A0 =A0 else {
> =A0 =A0 =A0 =A0 cout << "not all elements of search are also in coll"
> =A0 =A0 =A0 =A0 =A0 =A0 =A0<< endl;
> =A0 =A0 }
>
>
>
> }- Hide quoted text -
>
> - Show quoted text -

0
Reply freshthomas (139) 10/28/2009 9:33:42 AM


On Oct 28, 12:33=A0pm, thomas <freshtho...@gmail.com> wrote:
> On Oct 28, 4:51=A0pm, "suresh.amritapuri" <suresh.amritap...@gmail.com>
> wrote:
>
>
>
>
>
> > Hi,
> > just look at includes algorithm on two sets of strings. Its not
> > working correctly.
>
> > bool areql(string x, string y)
> > {
> > =A0 return (x=3D=3Dy); //whatever i write here, i get the same result. =
!=3D or
> > < or > why???}
>
> > int main()
> > {
> > =A0 =A0 set<string> coll;
> > =A0 =A0 set<string> search;
> > =A0 =A0 coll.insert("13");
> > =A0 =A0 coll.insert("96");
> > =A0 =A0 coll.insert("0");
>
> > =A0 =A0 search.insert("17");
> > =A0 =A0 search.insert("937");
> > =A0 =A0 // =A0 =A0search.insert(7);
> > =A0 =A0 // check whether all elements in search are also in coll
> > =A0 =A0 if (includes (coll.begin(), coll.end(),
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 search.begin(), search.end()),areql=
) {
>
> You have typo errors.
>
> if (includes (coll.begin(), coll.end(), search.begin(), search.end(),
> areql) )...

That's right, but the problem is still on.
>
>
>
> > =A0 =A0 =A0 =A0 cout << "all elements of search are also in coll"
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0<< endl;
> > =A0 =A0 }
> > =A0 =A0 else {
> > =A0 =A0 =A0 =A0 cout << "not all elements of search are also in coll"
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0<< endl;
> > =A0 =A0 }
>
> > }- Hide quoted text -
>
> > - Show quoted text -- Hide quoted text -
>
> - Show quoted text -- Hide quoted text -
>
> - Show quoted text -

 -- Saeed Amrollahi
0
Reply amrollahi.saeed (152) 10/28/2009 11:50:59 AM

On Oct 28, 11:51=A0am, "suresh.amritapuri" <suresh.amritap...@gmail.com>
wrote:
> Hi,
> just look at includes algorithm on two sets of strings. Its not
> working correctly.
>
> bool areql(string x, string y)
> {
> =A0 return (x=3D=3Dy); //whatever i write here, i get the same result. !=
=3D or
> < or > why???}
>
> int main()
> {
> =A0 =A0 set<string> coll;
> =A0 =A0 set<string> search;
> =A0 =A0 coll.insert("13");
> =A0 =A0 coll.insert("96");
> =A0 =A0 coll.insert("0");
>
> =A0 =A0 search.insert("17");
> =A0 =A0 search.insert("937");
> =A0 =A0 // =A0 =A0search.insert(7);
> =A0 =A0 // check whether all elements in search are also in coll
> =A0 =A0 if (includes (coll.begin(), coll.end(),
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 search.begin(), search.end()),areql) =
{
> =A0 =A0 =A0 =A0 cout << "all elements of search are also in coll"
> =A0 =A0 =A0 =A0 =A0 =A0 =A0<< endl;
> =A0 =A0 }
> =A0 =A0 else {
> =A0 =A0 =A0 =A0 cout << "not all elements of search are also in coll"
> =A0 =A0 =A0 =A0 =A0 =A0 =A0<< endl;
> =A0 =A0 }
>
>
>
> }- Hide quoted text -
>
> - Show quoted text -

Hi Suresh

My two cents ...
If you remove areql function, the program works.

Regards
  -- Saeed Amrollahi
0
Reply amrollahi.saeed (152) 10/28/2009 11:53:22 AM

On Oct 28, 9:51=A0am, "suresh.amritapuri" <suresh.amritap...@gmail.com>
wrote:
> Hi,
> just look at includes algorithm on two sets of strings. Its not
> working correctly.
>
> bool areql(string x, string y)
> {
> =A0 return (x=3D=3Dy); //whatever i write here, i get the same result. !=
=3D or
> < or > why???}
>
> int main()
> {
> =A0 =A0 set<string> coll;
> =A0 =A0 set<string> search;
> =A0 =A0 coll.insert("13");
> =A0 =A0 coll.insert("96");
> =A0 =A0 coll.insert("0");
>
> =A0 =A0 search.insert("17");
> =A0 =A0 search.insert("937");
> =A0 =A0 // =A0 =A0search.insert(7);
> =A0 =A0 // check whether all elements in search are also in coll
> =A0 =A0 if (includes (coll.begin(), coll.end(),
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 search.begin(), search.end()),areql) =
{
> =A0 =A0 =A0 =A0 cout << "all elements of search are also in coll"
> =A0 =A0 =A0 =A0 =A0 =A0 =A0<< endl;
> =A0 =A0 }
> =A0 =A0 else {
> =A0 =A0 =A0 =A0 cout << "not all elements of search are also in coll"
> =A0 =A0 =A0 =A0 =A0 =A0 =A0<< endl;
> =A0 =A0 }
>
> }

Read here what includes() does and what the predicate function should
do:

    http://www.cplusplus.com/reference/algorithm/includes

Now try to understand why your program does not produce your expected
result from includes().

Bonus question: would your program still work with the same elements
in two vector<string>?
0
Reply gert-jan.de.vos (58) 10/28/2009 1:03:30 PM

On Oct 28, 6:03 am, Gert-Jan de Vos <gert-
jan.de....@onsneteindhoven.nl> wrote:
> On Oct 28, 9:51 am, "suresh.amritapuri" <suresh.amritap...@gmail.com>
> wrote:
>
>
>
> > Hi,
> > just look at includes algorithm on two sets of strings. Its not
> > working correctly.
>
> > bool areql(string x, string y)
> > {
> >   return (x==y); //whatever i write here, i get the same result. != or
> > < or > why???}
>
> > int main()
> > {
> >     set<string> coll;
> >     set<string> search;
> >     coll.insert("13");
> >     coll.insert("96");
> >     coll.insert("0");
>
> >     search.insert("17");
> >     search.insert("937");
> >     //    search.insert(7);
> >     // check whether all elements in search are also in coll
> >     if (includes (coll.begin(), coll.end(),
> >                   search.begin(), search.end()),areql) {
> >         cout << "all elements of search are also in coll"
> >              << endl;
> >     }
> >     else {
> >         cout << "not all elements of search are also in coll"
> >              << endl;
> >     }
>
> > }
>
> Read here what includes() does and what the predicate function should
> do:
>
>    http://www.cplusplus.com/reference/algorithm/includes
>
> Now try to understand why your program does not produce your expected
> result from includes().
>
> Bonus question: would your program still work with the same elements
> in two vector<string>?

Hi

Thanks for the inputs. I solved my initial problem by adding the
function object into the template parameter. But now I have the new
problem. My actual objective is to find out that two sets of strings
are equal, if one string from one set beings with another string in
the other set. Kindly look at the code below and give your valuable
comments.


#include<string>
#include<algorithm>
#include<set>
#include<list>
#include <vector>
#include <iostream>
#include <boost/algorithm/string/predicate.hpp>

using namespace std;
using namespace boost::algorithm;

struct areql{
   bool operator()(string x, string y)
   {
     return (starts_with(x,y)||starts_with(y,x));
   }

};

int main()
{
   set<string,areql> coll;
   set<string,areql> search;
     coll.insert("13");
     coll.insert("96");

     search.insert("137");
     search.insert("967");

     // check whether all elements in search are also in coll
     if(includes(coll.begin(),coll.end(),search.begin(),search.end()))
       {
         cout << "all elements of search are also in coll"
              << endl;
       }
     else {
         cout << "not all elements of search are also in coll"
              << endl;
     }
}


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply suresh 10/28/2009 5:14:02 PM

On Oct 28, 4:53=A0am, Saeed Amrollahi <amrollahi.sa...@gmail.com> wrote:
> On Oct 28, 11:51=A0am, "suresh.amritapuri" <suresh.amritap...@gmail.com>
> wrote:
>
>
>
> > Hi,
> > just look at includes algorithm on two sets of strings. Its not
> > working correctly.
>
> > bool areql(string x, string y)
> > {
> > =A0 return (x=3D=3Dy); //whatever i write here, i get the same result. =
!=3D or
> > < or > why???}
>
> > int main()
> > {
> > =A0 =A0 set<string> coll;
> > =A0 =A0 set<string> search;
> > =A0 =A0 coll.insert("13");
> > =A0 =A0 coll.insert("96");
> > =A0 =A0 coll.insert("0");
>
> > =A0 =A0 search.insert("17");
> > =A0 =A0 search.insert("937");
> > =A0 =A0 // =A0 =A0search.insert(7);
> > =A0 =A0 // check whether all elements in search are also in coll
> > =A0 =A0 if (includes (coll.begin(), coll.end(),
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 search.begin(), search.end()),areql=
) {
> > =A0 =A0 =A0 =A0 cout << "all elements of search are also in coll"
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0<< endl;
> > =A0 =A0 }
> > =A0 =A0 else {
> > =A0 =A0 =A0 =A0 cout << "not all elements of search are also in coll"
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0<< endl;
> > =A0 =A0 }
>
> > }- Hide quoted text -
>
> > - Show quoted text -
>
> Hi Suresh
>
> My two cents ...
> If you remove areql function, the program works.
>
> Regards
> =A0 -- Saeed Amrollahi

Thanks for the comments. I had noted that it would work without the
predicate. But what I wanted was the predicate thing....
suresh
0
Reply suresh.amritapuri (100) 10/28/2009 6:34:47 PM

[crosspost to clcm dropped - it's not a good idea to crosspost between 
moderated and unmoderated groups]


In message 
<5555551e-3a86-4864-8984-3fc26eed2b83@y10g2000prg.googlegroups.com>, 
suresh.amritapuri <suresh.amritapuri@gmail.com> writes

>Thanks for the inputs. I solved my initial problem by adding the 
>function object into the template parameter.

I don't think so...

>But now I have the new problem. My actual objective is to find out that 
>two sets of strings are equal, if one string from one set beings with 
>another string in the other set. Kindly look at the code below and give 
>your valuable comments.

>struct areql{
>   bool operator()(string x, string y)
>   {
>     return (starts_with(x,y)||starts_with(y,x));
>   }
>
>};

Bad name. It's not testing for equality. (Nor should it be.)

You *still* need to read the documentation of std::includes (and now 
std::set, too), and find out what they say the predicate is supposed to 
do.

Hint: 'is_less' would be a far better name than 'areql'

-- 
Richard Herring
0
Reply Richard 10/29/2009 5:43:53 PM

suresh.amritapuri wrote:
> Thanks for the inputs. I solved my initial problem by adding the
> function object into the template parameter.

  Why would that make any difference whatsoever? Because it doesn't.

  The problem is in no way related to whether your comparator is a
function or a functor. It's related to how you compare the elements. You
are not comparing them correctly.

-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Juha 10/30/2009 12:47:58 PM

8 Replies
27 Views

(page loaded in 0.175 seconds)


Reply: