Skip to main content
GameDev.net gamedev.net
🔒 Locked

std::set with custom compare function

Started by WOsborn Jul 29, 2006 at 10:51 AM 5 replies 10k views
Original Post
WOsborn
WOsborn
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?
SiCrane
SiCrane
bool operator () (const T_MESH* a, const T_MESH* b) const
ToohrVyk
ToohrVyk
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 );}

scottdewald
scottdewald
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;    }};
WOsborn
WOsborn
The const made it work !!
Thanks..

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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.