Sign in to follow this  
joeBanana

c++ std::set

Recommended Posts

I have an objects with an id. I want to store them in a set which if perfect for me because I need fast search, one ID, that is ordered. How do I associate an ID of an object with set. I tried this; class Vertex { int ID; } set<Vertex*, classcomp> vList; set<Vertex*, classcomp>::iterator i; ... Vertex* tmp = new Vertex(30); Vertex* tmp2 = new Vertex(40); i = vList.insert(tmp); vList.insert(tmp2); HOW do I get a vertex with an ID of 40 from a set. And Also I read the reference and insert returns an iterator to the object if the object already exists. But didn't work for me. How do I get an iterator for an object with ID 40?

Share this post


Link to post
Share on other sites
Quote:
Original post by joeBanana
But didn't work for me.


Clearly, this is because you forgot to turn your computer on. Also, you're using an outdated compiler, you're assigning a pair to an iterator, you have malware, and your comparison function is faulty.

Seriously, do you expect us to guess what "didn't work" means? Compiler error? Linker error? Incorrect output? Access violation? Relevant lines for where the error happens?

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
Quote:
Original post by joeBanana
But didn't work for me.


Clearly, this is because you forgot to turn your computer on. Also, you're using an outdated compiler, you're assigning a pair to an iterator, you have malware, and your comparison function is faulty.

Seriously, do you expect us to guess what "didn't work" means? Compiler error? Linker error? Incorrect output? Access violation? Relevant lines for where the error happens?
QFE.

The following sentence illustrates how useful the three word combination "it didn't work" is:
Quote:
I tried to get to my friends house, but it didn't work.

Share this post


Link to post
Share on other sites
A set does not allow search by ID. You may only try to determine if there is an object in the set which is equivalent to another object.

If you wish to map IDs to objects, use an std::map instead.

Share this post


Link to post
Share on other sites
there are two things you can do. overload the operator= so that to items will appear equal if their ids are equal.
second use a class and overload operator() set the id you want to find and use find_if.

Share this post


Link to post
Share on other sites
Quote:
Original post by stonemetal
there are two things you can do. overload the operator= so that to items will appear equal if their ids are equal.

That's operator==, pointers have built in comparison functions, and an alternative is to pass a comparison object to the associative container like the OP is clearly doing already ("set<Vertex*, classcomp>").

OP: As others have said, most vertex classes have data like x/y/z (or a vector) which would be what you're attempting to get from any sort of table -- you'd want to use std::map to assign IDs to them.

For future reference, please post code that you actually tried (hint: copy & paste) instead of posting code that won't even compile (hint: class Vertex { ... } ----> ; <----)

#include <map>
int main() {
struct vertex { float x, y, z; };
std::map< int , vertex > verticies;
vertex a = { 1.0f, 2.0f, 3.0f };
vertex b = { 4.0f, 5.0f, 6.0f };
vertex c = { 7.0f, 8.0f, 9.0f };
verticies[0] = a;
verticies[1] = b;
verticies[2] = c;

std::map< int , vertex >::iterator i;

i = verticies.find(1);
vertex b_copy = *i;
assert( b.x == b_copy.x && b.y == b_copy.y && b.z == b_copy.z );

i = verticies.find(3); // no such ID
assert( i == verticies.end() ); // this is how you test this
}


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this