Hi,
I have to create a set of structs but i don't know how because it needs to check the equality between them.
For example:

struct something{
int x,y;
string word;
};

set<something> s;

Can you help me?

Recommended Answers

All 11 Replies

This definitely sounds like a job for a class not a struct. I'm surprised at the mixture of the STL with a struct that requires specific functionality. Is this an assignment for a class? If so, I wonder why your teacher isn't asking for a class with overloaded operators.

It isn't for a teacher's homework. I try to make it for a BFS algorithm.
I think that structs are simpler than classes.

Structs are simpler. Structs are old. Structs are not as powerful.

If this isn't for homework, you should definetly use a class here. Take the extra time to figure it out, because this sort of functionality is exactly what classes ( well, all OOP really ) was designed to provide. You will need two functions that explicitly manage the data in your struct. It would be much better to design a simple class to provide this functionality. Writing a basic class for this will not be that hard, I assure you, and it will be a great exercise for expanding your programming abilities.

The STL (sets, vetctors, etc.) is a native C++ paradigm. I encourage you to embrace C++ classes as well. There is little reason to mix C style structs with C++ containers in this case.

A struct is the same as a class. The only difference is that a struct's members default to public instead of private.

>>Structs are simpler. Structs are old. Structs are not as powerful

May I ask why you think structs are not as powerful as classes in C++?

>>Structs are simpler. Structs are old. Structs are not as powerful

May I ask why you think structs are not as powerful as classes in C++?

Polymorphism and Inheritance.

Polymorphism and Inheritance.

It seems as if you are thinking structs in terms of C and not C++. In C++
you can do anything with structs that you can do with classes. The only difference
besides the keywords are that structs have public identifier by default, whereas classes
have private identifiers as default.

In this case, the struct is, IMO, totally appropriate. And NO, there is no functional difference between classes and structs except for the default access rights (private, public) which you shouldn't use anyways because it is better to be explicit about the access rights.

In this case, the data inside the struct is the interface of the type (see Plain-Old-Data types "POD"), there would be no point in hiding it in a class' or struct's private scope and providing some mutator / accessor methods. The only real difference between struct and class is in the minds of most programmers who tend to use struct as a simple "package" of data, with few methods, i.e., the data in the struct is the interface defining its purpose (to hold a bundle of associated data).

Just use the struct you have and implemented the required operators for it to work on a set<>. From the top of my head, you are required to have a valid copy-constructor, assignment operator and a less-than operator. The first two are provided by default by the compiler and in this case your struct is simple enough that the default will do just fine. So all you need is the less-than operator (and you might wanna add a few more useful comparison operators and others while you are at it).

Can you help me to create the less-than operator if the struct is:

struct something{
int x,y;
};

Sure here is a skeleton to get you started :

struct Foo{
 int x, y;
}

bool operator < (const Foo& lhs, const Foo& rhs){
 return /* some boolean expression */
}

Note that you can do what I did there( not make operator< a member function) because
all the members in the struct are public. If not then you would have to include the
boolean operator< as a member function.

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.