c++ std::set

Started by
6 comments, last by MaulingMonkey 16 years, 3 months ago
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?
Advertisement
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?
one use the find function in the algorithms header.
two use map instead of set so that you can look up elements by key.
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.

"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Actually I was just hopping for an example how would I get an object from a set with find method with an ID of an object. On reference guide there was only an integer set example.
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.
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.
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}

This topic is closed to new replies.

Advertisement