473,382 Members | 1,720 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,382 software developers and data experts.

STL transform algorithm


Hi,

I have the following code which uses STL transform algorithm. It
basically takes a list of Rect* object, get all the y attribute and
store it in a vector of 'int'.

My question is: is there a way to simplify the code? Is there a way for
me to get rid of the GetY class, since it essentially just calls
'getY()'. Thank you.

class GetY : public std::unary_function<Rect*, int>
{
public:
int operator()(Rect* r);
};

int GetY :: operator()( Rect* r)
{
return r->getY();
}
vector<int>& getY(list<Rect*> _rectList) {

vector<int>* _y = new vector<int>(_rectList.size()),
transform(_rectList.begin(), _rectList.end(), _y->begin(), GetY());

return *(_y);
}

Feb 14 '06 #1
5 3749

Piotr wrote:
Hi,

I have the following code which uses STL transform algorithm. It
basically takes a list of Rect* object, get all the y attribute and
store it in a vector of 'int'.

My question is: is there a way to simplify the code? Is there a way for
me to get rid of the GetY class, since it essentially just calls
'getY()'. Thank you.

class GetY : public std::unary_function<Rect*, int>
{
public:
int operator()(Rect* r);
};

int GetY :: operator()( Rect* r)
{
return r->getY();
}
vector<int>& getY(list<Rect*> _rectList) {

vector<int>* _y = new vector<int>(_rectList.size()),
transform(_rectList.begin(), _rectList.end(), _y->begin(), GetY());
You try mem_fun?

transform(begin, end, mem_fun(&Rect::GetY));
return *(_y);
}


Feb 14 '06 #2
Is there a way to replace these binary function as well? I use them in
STL sort algorithm:

bool GreaterX::operator()( Rect* bd1, Rect* bd2)
{
return (bd1->getX() < bd2->getX());
}
bool SameX::operator()( Rect* bd1, Rect* bd2)
{
return (bd1->getX() == bd2->getX());
}

Feb 14 '06 #3

Piotr wrote:
Is there a way to replace these binary function as well? I use them in
STL sort algorithm:

bool GreaterX::operator()( Rect* bd1, Rect* bd2)
{
return (bd1->getX() < bd2->getX());
}
bool SameX::operator()( Rect* bd1, Rect* bd2)
{
return (bd1->getX() == bd2->getX());
}


There is all sorts of useful goodies in <algorithm>. Have a look there
and/or get Josuttis's book on the std lib.

I don't know, I would have to look it up.

Feb 14 '06 #4
Piotr wrote:
Hi,

I have the following code which uses STL transform algorithm. It
basically takes a list of Rect* object, get all the y attribute and
store it in a vector of 'int'.

My question is: is there a way to simplify the code? Is there a way for
me to get rid of the GetY class, since it essentially just calls
'getY()'. Thank you.


Yes. Take a look at std::mem_fun(), which permits you to eliminate the
GetY functor entirely. Here's a reference:

http://www.sgi.com/tech/stl/mem_fun_t.html

Best regards,

Tom

Feb 14 '06 #5
In article <11*********************@g43g2000cwa.googlegroups. com>,
"Piotr" <ra************@gmail.com> wrote:
Is there a way to replace these binary function as well? I use them in
STL sort algorithm:

bool GreaterX::operator()( Rect* bd1, Rect* bd2)
{
return (bd1->getX() < bd2->getX());
}
bool SameX::operator()( Rect* bd1, Rect* bd2)
{
return (bd1->getX() == bd2->getX());
}


(I'm assuming getX returns an int.)

In both cases, the functions take two Rect*s, call "->getX()" on each
and performs a compare on the results returning a bool. The only
difference is what is being used to make the comparison.

It just so happens that there are two functors in the standard library
that already do the two different comparisons (std::less and
std::equal_to) so we simply need to create one class that can use either
of those functors. It's operator() will look something like this:

bool operator()( const Rect* bd1, const Rect* bd2 ) const {
return fn( bd1->getX(), bd2->getX() );
}

If 'fn' is std::less, then it will return true if bd1's x is less than
bd2's x. If 'fn' is std::equal_to, then the two xs must be equal for it
to return true.

Here is the whole class.

template <typename Op>
class compare_x_t: public binary_function<Rect*, Rect*, bool>
{
Op fn;
public:
compare_x_t() { }
compare_x_t( Op f ): fn( f ) { }
bool operator()( const Rect* bd1, const Rect* bd2 ) const {
return fn( bd1->getX(), bd2->getX() );
}
};

and a helper function for creating the right class object.

template <typename Op>
compare_x_t<Op> compare_x( Op f ) {
return compare_x_t<Op>( f );
}
Now you can:

void fn( list<Rect*>& ll, Rect* refRect ) {
ll.sort( compare_x( less<int>() ) );

list<Rect*>::iterator it = find_if( ll.begin(), ll.end(),
bind2nd( compare_x( equal_to<int>() ), refRect ) );
}

The above could be made more generic but don't do it unless you need it.

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Feb 14 '06 #6

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

Similar topics

20
by: Steffen Brinkmann | last post by:
Hi! I tried to modify the transform algorithm in a way that it doesn't take iterators, but a reference to a container class and a value, because Mostly I need to do an operation of a container...
5
by: Alex Vinokur | last post by:
Functor-parameters in the for_each() and transform() algorithms are passed by value. Might it make sense to have also algorithms for_each2() and transform2() which pass Functor-parameters by...
11
by: TheDD | last post by:
Hello, i don't manage to use the tolower() function: #include <algorithm> #include <iostream> #include <locale> #include <string> using std::cout;
0
by: Alex Vinokur | last post by:
One more improvement has been done in the "Computing very large Fibonacci numbers" algorithm. Its new version can be seen at http://groups.google.com/groups?selm=2mk62rFo1mv0U1%40uni-berlin.de ...
1
by: Brian McGuinness | last post by:
I have a question about using the STL transform algorithm in a function. What I want to do is define a group of array classes to represent APL-style arrays (arrays in which the number of...
9
by: Saki Arkoudopoulos | last post by:
Hello, I'm trying to convert a string with mixed upper- and lowercase characters to all lowercase characters. I found the following code on the Internet: transform(temp.begin(), temp.end(),...
9
by: Patrick Guio | last post by:
Dear all, I am trying to use the std::transform algorithm to to the following vector< vector<char> >::iterator ik = keys.begin(); // key list iterator vector< vector<char> >::iterator is = ik;...
4
by: Dean Card | last post by:
Okay, so here is the situation. I have need to do some on-the-fly image creation. I have everything working great except for the last part of it, applying a perspective type transform to the...
8
by: Alex Buell | last post by:
I've just written the below as an exercise (don't worry about the lack of checking), but I was wondering why I needed to write a struct with an operator() as a parameter to supply to the STL...
11
by: Gerald I. Evenden | last post by:
Working on a Kubuntu 64bit system "c++ (GCC) 4.0.3". The following simple program extracted from p.497 & 499 of N.M.Josurris' "The C++ Standard Library ... " (file t.cpp): 1 #include <string>...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.