includes algorithm not working properly on sets of strings

S

suresh.amritapuri

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

thomas

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) )...
 
S

Saeed Amrollahi

You have typo errors.

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

That's right, but the problem is still on.
- Show quoted text -- Hide quoted text -

- Show quoted text -

-- Saeed Amrollahi
 
S

Saeed Amrollahi

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



}- Hide quoted text -

- Show quoted text -

Hi Suresh

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

Regards
-- Saeed Amrollahi
 
G

Gert-Jan de Vos

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

suresh.amritapuri

Hi Suresh

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

Regards
  -- 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
 
S

suresh.amritapuri

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

Richard Herring

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


In message
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'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top