I have never used STL sets before, but I have used some other STL stuff.

I am having trouble with upper_bound.

I have a class I defined, and I have a (STL set) set of pointers to different instantiations of the class. i compare using a comparison function, which takes 2 ptrs to the class type and then calls a function to compare based on an member variable of the class.

The problem is that I want to call upper_bound on my set, but I want to find the upper_bound based on that internal variable, not the key (because the key is a pointer to the class). How do I do this?

Sets are ordered based on the comparison function that you supply. So when you call upper_bound, what you get is an iterator referring to a position in the set that is based on calling the comparison function. If your comparison function compares based on a member variable, then that's what you get.

If that isn't what you want, then you need to explain what you're trying to do in more detail, along with code examples.

Sets are ordered based on the comparison function that you supply. So when you call upper_bound, what you get is an iterator referring to a position in the set that is based on calling the comparison function. If your comparison function compares based on a member variable, then that's what you get.

If that isn't what you want, then you need to explain what you're trying to do in more detail, along with code examples.

example:
cmpfunc( c* a , c* b ){
return a.p < b.p;
}
set<c*,cmpfunc> myset;

I can access the set fine for my purposes, except in the fact that
myset.upper_bound() requires a key value. the key is a ptr to class c.

I want the find the upper_bound based on the member variable (.p in the example) of class c that I am using in the comparison function.

How would I do this?

Ah. You have a set of pointers and a comparison function that compares pointers. (Incidentally, the comparison function you provided in your example won't compile, so I assume you've made it right in your actual code)

No problem. For a set<T>, the key type is just T. So if you have a set of pointers and want to call upper_bound, you have to hand it a pointer.

So... Use "new" to allocate an object of type c, with its p member initialized to the value for which you want to search. Pass that pointer as an argument to upper_bound. If it's appropriate, delete the object after upper_bound has returned.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.