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;
}
}
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 -
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
Hi Suresh
My two cents ...
If you remove areql function, the program works.
Regards
-- Saeed Amrollahi
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>?
Thanks for the comments. I had noted that it would work without the
predicate. But what I wanted was the predicate thing....
suresh
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! ]
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
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.