Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

includes algorithm not working properly on sets of strings

0 views
Skip to first unread message

suresh.amritapuri

unread,
Oct 28, 2009, 4:51:35 AM10/28/09
to
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;
}
}

thomas

unread,
Oct 28, 2009, 5:33:42 AM10/28/09
to
On Oct 28, 4:51 pm, "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) {

You have typo errors.

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;
>     }
>
>
>

> }- Hide quoted text -
>
> - Show quoted text -

Saeed Amrollahi

unread,
Oct 28, 2009, 7:50:59 AM10/28/09
to
On Oct 28, 12:33 pm, thomas <freshtho...@gmail.com> wrote:
> On Oct 28, 4:51 pm, "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) {
>
> You have typo errors.
>
> if (includes (coll.begin(), coll.end(), search.begin(), search.end(),
> areql) )...

That's right, but the problem is still on.


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

> > - Show quoted text -- Hide quoted text -
>
> - Show quoted text -- Hide quoted text -


>
> - Show quoted text -

-- Saeed Amrollahi

Saeed Amrollahi

unread,
Oct 28, 2009, 7:53:22 AM10/28/09
to
On Oct 28, 11:51 am, "suresh.amritapuri" <suresh.amritap...@gmail.com>
wrote:
> }- Hide quoted text -
>
> - Show quoted text -

Hi Suresh

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

Regards
-- Saeed Amrollahi

Gert-Jan de Vos

unread,
Oct 28, 2009, 9:03:30 AM10/28/09
to
On Oct 28, 9:51 am, "suresh.amritapuri" <suresh.amritap...@gmail.com>
wrote:

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>?

suresh.amritapuri

unread,
Oct 28, 2009, 2:34:47 PM10/28/09
to

Thanks for the comments. I had noted that it would work without the
predicate. But what I wanted was the predicate thing....
suresh

suresh.amritapuri

unread,
Oct 28, 2009, 7:14:02 PM10/28/09
to
On Oct 28, 6:03 am, Gert-Jan de Vos <gert-

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! ]

Richard Herring

unread,
Oct 29, 2009, 1:43:53 PM10/29/09
to
[crosspost to clcm dropped - it's not a good idea to crosspost between
moderated and unmoderated groups]


In message
<5555551e-3a86-4864...@y10g2000prg.googlegroups.com>,
suresh.amritapuri <suresh.a...@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

Juha Nieminen

unread,
Oct 30, 2009, 2:47:58 PM10/30/09
to
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.

0 new messages