COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### includes algorithm not working properly on sets of strings

• Email
• 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 (107) 10/28/2009 8:51:35 AM

See related articles to this posting

```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 (60) 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

#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

```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 (107) 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
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

>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

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

8 Replies
42 Views

Similar Articles

12/18/2013 7:12:17 AM
page loaded in 74847 ms. (0)

Similar Artilces:

include() not working properly
I've been running a XAMPP server for some time now, but I just tried to add an include, and it says: Warning: include() [function.include]: URL file-access is disabled in the server configuration in M:\Server\htdocs\desktop\search.php on line 12 Warning: include(http://www.google.com) [function.include]: failed to open stream: no suitable wrapper could be found in M:\Server\htdocs\desktop\search.php on line 12 Warning: include() [function.include]: Failed opening 'http://www.google.com' for inclusion (include_path='.;M:\Server\php\pear\') in M:\Server\htdocs\deskto...

Comparison of string is not working properly :-(
Hi all // my code var string1= "Pablo has 3 cats and 1 dog"; var string2= "Pablo has 3 cats"; var str1 = ""; var str2 = ""; str1 = string1.match(string1, "g"); str2 = string2.match(string1, "g"); if (str1 == str2) // STRING IS NOT the same :-( , what is wrong ??? { app.alert("String are the same "); } else { app.alert("String is different..."); } I want to compare two strings in 'if' statement, which are asssigned to variables, If I compare two variable which have assigned the same (in my opinion) st...

Bug? Hash::include not working properly
Hi all, The behaviour of both ruby 1.6 and 1.8 seems broken. Foo nicely overrides #initialize and #inspect when the module is included, but Hash does not. Feels like premature optimization to me... 1.8 at least behaves properly when overriding Hash#initialize once more. Note that the 'discarding old initialize' is on line 22, not line 12. \$ ruby1.6 -v hash_include.rb ruby 1.6.8 (2003-07-09) [i386-linux] {} InsertInit / II: #<Foo:0xb7fd90cc> II: #<Foo:0xb7fd90cc> hash_include.rb:22: warning: method redefined; discarding old initialize hash_include.rb:23:in `initialize...

Q: Should String.upcase! work on a segment of a string?
Question: Should String.upcase! work on a segment of a string? We can assign to a sub-string, so I thought that a bang-method might also work on a sub-string. s = 'AA11zz' # define a string > s[4..5].upcase! # try to change part of it => "ZZ" # yeah, ok, the new string is changed, but... > s => "AA11zz" # the original string wasn't affected > s[4..5] = s[4..5].upcase # so, do it by-hand => "ZZ" > s # and it's ok => "AA11ZZ" Are there edge cases where this would...

Strings comparison and deleting differently ordered but same set of strings
Hello, Is there any way to find out from the cell {'ab' 'cd' 'ef'; 'ac' 'de' 'bf'; 'cd' 'ab' 'ef'} that row one is just arranged in different order in row three. In my program I have to delete if such a case occurs. And I have to this very efficiently. This is just a small example but actually there may be 1000s r 10000s of rows and columns. So, using lot of for loops in the code is becoming very expensive. Is there any built in MATLAB function? Thanks.. On 6/14/2012 1:12 PM, Rinachi Garg wrote: > Hello, > ...

work with strings
Hi all, In the next program I wrote class "string_ " with next functions to manipulate strings : assign, compare, concatenate, find index of character. 1- Is there any method to write these functions without using C- standard functions, i.e to do not use #include <string.h> ? 2- After assigning s1 to s2 in the next program, the content of s2 disappears when I try to use it in other function. Why this occurs? should I use a static variable ? #include <iostream> using namespace std; class string_ { char* c; public: string_ (char* c); ~string_ (); // Methods int ge...

Init script fails when set to S99, works when set to S22
I have tshark installed on my box and I have an init script that is chmod 755 and chown root.root It is in /etc/init.d/ and rc2.d as S99tshark (symlink to /etc/ init.d/). When the box is started, tshark will not start. But, if I set it to S22tshark (higher priority) it does startup at boot. Can anyone think of a reason why this is the case? tash wrote: > I have tshark installed on my box and I have an init script that is > chmod 755 and chown root.root > > It is in /etc/init.d/ and rc2.d as S99tshark (symlink to /etc/ > init.d/). > > When the box is...

Set set serveroutput on , Dbms_output.put_line do not work when database is not open
I am using Oracle 8.1.6.1 on HP UNIX 11 (cannot change versions). I am setting up a standby database. I am writing a UNIX script for applying archived logs to standby database. I do not like managed recovery because it does not automatically delete archived logs. My question is really irrelevant to type of ecovery and probably version of Oracle. When I use PL/SQL block and try to use dbms_output,put_line etc, I get PL/SQL errors: sqlplus -S /nolog << ! connect / as sysdba; set buffer edit; set serveroutput on size 1000000; declare sql_stmt varchar2(512); start_seq integer; end_se...

Big O and algorithm to decide string A contains string B?
I need to implement a function to return True/false whether String A contains String B. For example, String A = "This is a test"; String B = "is is". So it will return TRUE if String A includes two "i" and two "s". The function should also handle if String A and B have huge values, like two big dictionary. What's the best approach to achieve this with the best performance? what's the Big O then? I am thinking to put A and B into two hashtable, like key = "i" and value = "2" and so on. Then compare these two hashtable. But ho...