![]() |
|
STL Vector + sort |
Mariusz
Member #1,634
November 2001
|
Hello, I am using vectors for my Sprite class. I was wondering how to implement sorting so that I could order my sprites using ZOrder attribute that I have given it?
Thanks, |
Billybob
Member #3,136
January 2003
|
Overload the < operator on that class. Then you can use STL's regular sort algorithm.
|
Mark Oates
Member #1,146
March 2001
![]() |
Mariuz: bool operator<(const CSprite &a, const CSprite &b) { return a.m_ZOrder < b.m_ZOrder; } and sort with this: vector<CSprite> sprites; sort(sprites.begin(), sprites.end()); it might be squirly because m_ZOrder is protected. ----- this is my code that does not compile:
for some reason it doesn't like that I'm using a function (get_laugh()) to compare the two objects, and I get the errors: main.cpp: In function `bool operator<(const time_stamp_object&, const time_stamp_object&)': main.cpp:108: error: passing `const time_stamp_object' as `this' argument of `int time_stamp_object::get_length()' discards qualifiers main.cpp:108: error: passing `const time_stamp_object' as `this' argument of `int time_stamp_object::get_length()' discards qualifiers what am I doing wrong? -- |
Billybob
Member #3,136
January 2003
|
Mark: get_length needs to be declared const, otherwise the compiler is unsure if get_length will modify the object, therefore possibly violating the const in operator<'s parameters.
|
Mariusz
Member #1,634
November 2001
|
Thanks for your quick response. Well it has been ages since I have worked with operators. It apears that I have to put this outside my CSprite class, which then like you said my m_ZOrder would not be accessible, unless I make it public. Is there another way to make this operator part of my class? Right now If I make < part of CSprite I get: Thanks, |
Billybob
Member #3,136
January 2003
|
Because it should only be one parameter, and you'd do I'm not sure if it'll still complain about protected, though. If it does you'll need to create a function that returns m_ZOrder, or just move m_ZOrder to public. EDIT: bool operator<(const CSprite &b) { return this->m_ZOrder < b.m_ZOrder; }
|
Mark Oates
Member #1,146
March 2001
![]() |
William: I tried adding const in front of the function but didn't work ... ?? -- |
Mariusz
Member #1,634
November 2001
|
Thats right now I remember. Thanks a lot |
Bob
Free Market Evangelist
September 2000
![]() |
You don't need to overload anything if you don't want to. You can just write something like: struct sprite_sorter { bool operator()(const CSprite &a, const CSprite &b) { return a.m_ZOrder < b.m_ZOrder; } }; /* And then */ std::sort(sprites.begin(), sprites.end(), sprite_sorter);
-- |
Mariusz
Member #1,634
November 2001
|
Thats cool, but I have just implemented William's suggestion and it seems that everything is working just fine. Anyhow, I really appreciate your input, it helps me to refresh my rusty C/C++ stuff. All the best, ----------------------------------------------------------------------------------- I have BG with zOrder of 1000 (background with size of full screen). My Render function looks like this:
I would expect fro one sprite to be hidden behind my Background according to its ZOrder. Any suggestions? |
Mark Oates
Member #1,146
March 2001
![]() |
(oops wrong thread) -- |
Billybob
Member #3,136
January 2003
|
Mark: Hmm, learn something new everyday. You need to put it after the () of the function.
|
Mark Oates
Member #1,146
March 2001
![]() |
ah, thanks. I'd give a cookie if I could. -- |
ImLeftFooted
Member #3,935
October 2003
![]() |
Quote:
struct sprite_sorter { /* And then */ Why bother with the struct? #include <algorithm> static bool comp(const CSprite &a, const CSprite &b) { return a.m_ZOrder < b.m_ZOrder; } std::sort(sprites.begin(), sprites.end(), comp); [edit] Quote:
Right now If I make < part of CSprite I get: Make the operator < take only one argument and compare whether *this < the parameter. |
HoHo
Member #4,534
April 2004
![]() |
I would probably go with overloaded operator< if maximum speed is necessary. I'm not sure if functors can get inlined. // should be inside class definition bool operator<(const CSprite &a) const { return m_ZOrder < a.m_ZOrder; } //Somewhere in code std::sort(sprites.begin(), sprites.end()); If I was Mark I'd use this kind of code:
If all this code is in headers those functions should get inlined. __________ |
ImLeftFooted
Member #3,935
October 2003
![]() |
#include <algorithm> inline bool comp(const CSprite &a, const CSprite &b) { return a.m_ZOrder < b.m_ZOrder; } std::sort(sprites.begin(), sprites.end(), comp); Any speed boost you're getting from using operator < is imagined. |
Tobias Dammers
Member #2,604
August 2002
![]() |
What HoHo said. The overloaded operator should be a member func. That's why the compiler complains about the "this" argument. --- |
ImLeftFooted
Member #3,935
October 2003
![]() |
Quote: What HoHo said. The overloaded operator should be a member func. That's why the compiler complains about the "this" argument. No, thats wrong. A standard conforming compiler accepts both ways, one takes 2 parameters and the other only takes 1. |
Mariusz
Member #1,634
November 2001
|
Hi, I have a question in regards to STL list sorting. This is a snap of my code that I use:
Problem I am having is that Alien sprite is hidden behind Background and unless I add sprites in order Background, player, alien I have alien sprite hidden. It appears that the sort function is not working for me. Any suggestions would be appreciated. Thanks, |
lennaert van der linden
Member #6,053
July 2005
![]() |
This is a guess, but I think you are sorting pointers, instead of objects (it's a vector of pointers to CSprite, instead of an vector of CSprite). Try adding some logging to the operator< member function to see if it is actually called. You could create a custom compare function that compares pointers to CSprite and use that as an argument to the sort algorithm. See Bob's message above. |
Mariusz
Member #1,634
November 2001
|
Well, I am not exactly sure where to place Bobs code. Is the structure a part of Sprite Class, or a shared function? Where do I call sort from, so far I get an error telling me that sort is not a member of std namespace. error C2039: 'sort' : is not a member of 'std' Thanks, |
lennaert van der linden
Member #6,053
July 2005
![]() |
You probably forgot to #include <algorithm>. I have made a small example:
|
Mariusz
Member #1,634
November 2001
|
Thank you, I will check this as soon I have a chance. M. |
|