473,386 Members | 1,668 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

STL adaptors help needed...

Hi there...

I have something like this:

#include <string>
#include <list>
#include <algorithm>

using namespace std;
typdef list<string> ls;

bool IsGood (const string& st);

struct AreGood
{
bool operator () (const string& s1, const string& s2) {
return IsGood(s1) == IsGood(s2);
}
};

void remove_redundant_lines (ls& the_list)
{
ls::iterator lbeg, lend;
lbeg = the_list.begin();
lend = the_list.end();

the_list.erase (unique(lbeg, lend, AreGood()), lend);
}

Now remove_redundant_lines works as I expect it (removes subsequent strings
that satisfy IsGood from the_list). Here is my problem: I would like to do
it without AreGood functor. AreGood is used only in remove_redundant_lines
so introducing this struct just for that purpose seems to much. Could it be
done only with the use of IsGood? In this line:

unique(lbeg, lend, AreGood())

I wonder if some standard adaptor (or combination of those) could be used
instead of AreGood()? There is extension to STL adaptors available in SGI
STL named binary_compose which could (possibly) do what I want, but I don't
want to use SGI. So how would I do it with standard STL adaptors?

TIA
Oct 16 '05 #1
8 1646
> struct AreGood
{
bool operator () (const string& s1, const string& s2) {
return IsGood(s1) == IsGood(s2);
}
};


This functor looks fine. In fact, you should make it adaptable
by deriving it from the binary_function. This way, it is more
likely to be reused in the future.

Oct 16 '05 #2
SpOiLeR wrote:
Hi there...

I have something like this:

#include <string>
#include <list>
#include <algorithm>

using namespace std;
typdef list<string> ls;

bool IsGood (const string& st);

struct AreGood
{
bool operator () (const string& s1, const string& s2) {
return IsGood(s1) == IsGood(s2);
}
};

void remove_redundant_lines (ls& the_list)
{
ls::iterator lbeg, lend;
lbeg = the_list.begin();
lend = the_list.end();

the_list.erase (unique(lbeg, lend, AreGood()), lend);
}

Now remove_redundant_lines works as I expect it (removes subsequent strings
that satisfy IsGood from the_list). Here is my problem: I would like to do
it without AreGood functor. AreGood is used only in remove_redundant_lines
so introducing this struct just for that purpose seems to much. Could it be
done only with the use of IsGood? In this line:

unique(lbeg, lend, AreGood())

I wonder if some standard adaptor (or combination of those) could be used
instead of AreGood()? There is extension to STL adaptors available in SGI
STL named binary_compose which could (possibly) do what I want, but I don't
want to use SGI. So how would I do it with standard STL adaptors?

TIA


Why do you need a functor?

void remove_redundant_lines(ls& the_list)
{
the_list.erase( std::unique(the_list.begin(), the_list.end()),
the_list.end());
}

Greg

Oct 17 '05 #3
On 16 Oct 2005 15:26:41 -0700, An**********@gmail.com wrote:
struct AreGood
{
bool operator () (const string& s1, const string& s2) {
return IsGood(s1) == IsGood(s2);
}
};


This functor looks fine. In fact, you should make it adaptable
by deriving it from the binary_function. This way, it is more
likely to be reused in the future.


This would make sense if I could make it to use any unary predicate. How do
I do that?

template <class T, class un_pred>
struct AreEqualByPredicate
{
bool operator () (const T& s1, const T& s2) {
return un_pred(s1) == un_pred(s2);
}
}

might work, but not for IsGood in:

unique(lbeg, lend, AreEqualByPredicate<strting, IsGood>())

because IsGood should be functor, and I can't change that.
Oct 17 '05 #4
On 16 Oct 2005 22:58:12 -0700, Greg wrote:

Why do you need a functor?

void remove_redundant_lines(ls& the_list)
{
the_list.erase( std::unique(the_list.begin(), the_list.end()),
the_list.end());
}

Greg


I need it because I don't want unique to use default operator== for (in
this case) strings, but to check relation of equality using unary predicate
IsGood.
Oct 17 '05 #5
SpOiLeR wrote:
On 16 Oct 2005 22:58:12 -0700, Greg wrote:

Why do you need a functor?

void remove_redundant_lines(ls& the_list)
{
the_list.erase( std::unique(the_list.begin(), the_list.end()),
the_list.end());
}

Greg


I need it because I don't want unique to use default operator== for (in
this case) strings, but to check relation of equality using unary predicate
IsGood.


Nevertheless the simplest, cleanest solution is to rely on the equality
comparison to determine uniqueness. And if the std::string == operator
(which tests for identical strings) is not the true test for
equivalence, then the std::list does not really contain std::string's
but a "string-like" class that has its own semantics.

So I would formalize the distinction by writing a wrapper class to turn
the std::string into a different type (but with the same behavior as a
std::string). And then define the operator== (and whatever other
operators are needed) for this type:

class TextLine
{
private:
std::string mString;

public:
TextLine(const char *s) : mString(s) {}
...
bool operator==(const TextLine& rhs) const
{
return IsGood(this) == IsGood(rhs);
}
...

Of course this approach would seem overkill for the benefit of one
routine. But having equality and other comparison operators defined for
the type will simplify any code that iterates, tests, compares and
erases these types items. In other words, instead of writing "one-off"
functors throughout the program, implement a single, overloaded
operator instead that can be used in many different functions.

Greg

Oct 17 '05 #6
On 16 Oct 2005 23:32:07 -0700, Greg wrote:
SpOiLeR wrote:
On 16 Oct 2005 22:58:12 -0700, Greg wrote:

Why do you need a functor?

void remove_redundant_lines(ls& the_list)
{
the_list.erase( std::unique(the_list.begin(), the_list.end()),
the_list.end());
}

Greg


I need it because I don't want unique to use default operator== for (in
this case) strings, but to check relation of equality using unary predicate
IsGood.


Nevertheless the simplest, cleanest solution is to rely on the equality
comparison to determine uniqueness. And if the std::string == operator
(which tests for identical strings) is not the true test for
equivalence, then the std::list does not really contain std::string's
but a "string-like" class that has its own semantics.

So I would formalize the distinction by writing a wrapper class to turn
the std::string into a different type (but with the same behavior as a
std::string). And then define the operator== (and whatever other
operators are needed) for this type:

class TextLine
{
private:
std::string mString;

public:
TextLine(const char *s) : mString(s) {}
...
bool operator==(const TextLine& rhs) const
{
return IsGood(this) == IsGood(rhs);
}
...

Of course this approach would seem overkill for the benefit of one
routine. But having equality and other comparison operators defined for
the type will simplify any code that iterates, tests, compares and
erases these types items. In other words, instead of writing "one-off"
functors throughout the program, implement a single, overloaded
operator instead that can be used in many different functions.

Greg


To much work if I want to keep all good stuff that std::strting offers (I
would need to repeat interface of std::string, any global overloaded
operators and so on... unless I inherit from std::string which I don't know
if it's possible to do). But even then: single operator== wouldn't do. For
example, let it be that once IsGood checks if given string contains digits.
On the other time I want to have predicate that returns true only if given
string has certain number of given charaters... Or given string is exactly
length long... An so on. This is easilly achieved by writing unary/binary
predicates for all this situations, and then to have general binary
predicate that can take two strings and functor/predicate and check those
two strings for equallity by that predicate... Something like that is
impossible to achieve with operator== (or I could write a new class for
each that case which is really to much)
Oct 17 '05 #7
An**********@gmail.com wrote:
struct AreGood
{
bool operator () (const string& s1, const string& s2) {
return IsGood(s1) == IsGood(s2);
}
};

This functor looks fine. In fact, you should make it adaptable
by deriving it from the binary_function. This way, it is more
likely to be reused in the future.


Actually, it in addition to deriving from binary_function, operator()
should be a const member function.

i.e.

#include <functional>
#include <string>
using std::string;
using std::binary_function;

struct AreGood :
public binary_function<string,string,bool>
{
bool operator()(const string&s1, const string&s2) const
{
return IsGood(s1) == IsGood(s2);
}
};

As a side note, your implementation of AreGood will indicate s1 and s2
are good if they are both bad.

Oct 17 '05 #8
On Mon, 17 Oct 2005 17:09:46 GMT, red floyd wrote:
An**********@gmail.com wrote:
struct AreGood
{
bool operator () (const string& s1, const string& s2) {
return IsGood(s1) == IsGood(s2);
}
};
...


Actually, it in addition to deriving from binary_function, operator()
should be a const member function.


Point taken.
...
struct AreGood :
public binary_function<string,string,bool>
{
bool operator()(const string&s1, const string&s2) const
{
return IsGood(s1) == IsGood(s2);
}
};

As a side note, your implementation of AreGood will indicate s1 and s2
are good if they are both bad.


AreGood is name I made just for representation sake. I guess, better would
be AreEqualByPredicate... But name itself is not my biggest issue...
Oct 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: System | last post by:
Hello All, Redhat 9.0 Mysql 3.23.56 ==> Running I want to upgarde to 4.0.13 but this is the error it says: # rpm -Uvh MySQL-server-4.0.13-0.i386.rpm warning: MySQL-server-4.0.13-0.i386.rpm: V3...
8
by: Stephen | last post by:
I am trying to add some code to below to include a datatable and fill the datatable. The reason for doing this is so as I can check to see whether there are any rows returned by the stored...
4
by: Mark A. Gibbs | last post by:
good day, i'm not sure if i'm using the standard function adaptors correctly. i want to use a member function in a call to transform(), but not a member function of the class being transformed....
2
by: Drew McCormack | last post by:
I have a self written Tensor class which I need to write a number of elementwise operations for (eg sin, cos, abs, conj). I am trying to implement most of these in terms of standard library...
5
by: Naveen Mukkelli | last post by:
Hi, I've got two network adaptors in my computer(Windows server 2003). One for public network such as internet, and the other for a private network. I mean, I want to connect other 2 PCs (XP...
5
by: Steve | last post by:
Hi, I am sitting down to design our next set of internal apps. I would like to approach this in a way that would allow me to break logical parts of the application that handle specific tasks...
0
by: ultradiv | last post by:
I have a VB.NET application partly built that produces an xml output (just a file at present) I have a .NET webserver and SQLserver 2000 I need to be able to send the xml to the webserver/database...
28
by: Ian Davies | last post by:
Hello I would appreciate some help from someone who has knowledge of working with css, php javascript and how they interact. Ive been working on a task for the last few days and have started to...
37
by: C_guy | last post by:
Does anyone know of a (hopefully free) tool that can traverse a project and determine which "#include"s are not needed or needed in every .C file? This would be helpful in removing header...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.