Jump to content
  • Advertisement
Sign in to follow this  
WOsborn

std::set with custom compare function

This topic is 4407 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

example: how can I do a find on this type of set to get an iterator that I then can delete later on? I get error C3848????
struct T_MESH_dis_comp
{
    bool operator () (const T_MESH* a, const T_MESH* b)
    {
        //Sort Furthest to Closest
        if (a->meshRenderData.disToCamera > b->meshRenderData.disToCamera) 
            return true;
        else
            return false;
    }
};
T_MESH* g;
std::set<T_MESH*,T_MESH_dis_comp> a;
a.insert( g );
std::set<T_MESH*,T_MESH_dis_comp>::iterator itr = a.find( g );  //error C3848


how can I do a find to get an iterator to then delete it from set?

Share this post


Link to post
Share on other sites
Advertisement
The code below compiles without error on a recent G++.


#include <set>

struct T_MESH {
int dis;
};

struct T_MESH_dis_comp
{
bool operator () (const T_MESH* a, const T_MESH* b)
{
//Sort Furthest to Closest
if (a->dis > b->dis)
return true;
else
return false;
}
};

int main() {
T_MESH* g;
std::set<T_MESH*,T_MESH_dis_comp> a;
a.insert( g );
std::set<T_MESH*,T_MESH_dis_comp>::iterator itr = a.find( g );
}




Share this post


Link to post
Share on other sites
You need to make the function within the functor struct const.


struct T_MESH_dis_comp
{
bool operator () (const T_MESH* a, const T_MESH* b) const // NEW
{
//Sort Furthest to Closest
if (a->meshRenderData.disToCamera > b->meshRenderData.disToCamera)
return true;
else
return false;
}
};

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:

//Sort Furthest to Closest
if (a->meshRenderData.disToCamera > b->meshRenderData.disToCamera)
return true;
else
return false;

Why not just:
//Sort Furthest to Closest
return a->meshRenderData.disToCamera > b->meshRenderData.disToCamera;

Share this post


Link to post
Share on other sites
The const made it work !!
Thanks..

Can you give a real quick summary of what that does to a function.
adding the const

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.10

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!