c++ - Standard library sort and user defined types -
If I want to sort the UDT vector one of two types of variables, then it is possible for standard Sort the library To do this or I need to write my own sort function.
For example if you have
struct MyType {int a; Int b; }; Of vector & lt; MyType & gt; Mu // Doo Stuff which moves the data back to Mew.begin (moo.begin (), moo.end ()) //, but at least the highest, but not
Is it possible to sort using stdlib sort? Thank you.
If your type applies "bool operator & lt; (...) const "
and a copy constructor (compiler-generate or custom).
struct MyType {int a; Int b; The Bull Operator & lt; (Cost MyType & Other) const {... // A meaningful implementation for your type} // copy constructor (unless it is not a POD type). MyType (Constant MyType & Other): A (Other. A), B (Other. B) {} // Some other forms of production besides copy maker. MyType (): a (0), b (0) {}};
Alternatively, you are the operator implementing sort ()
to the functioning function (or fun doctor) "
.
bool type_is_less (Constant MyType & T1, Constant MyType & T2) {...} ... std :: sort (c.begin (), c.end (), Type_is_less);
This is useful in the following situations:
- You apply operator
"
The reason you do not want to, - You need to sort the built-in or pointer type containers. You can not overload the operator.
- Before you different ordering: Sometimes you want a structure with the names of the first / last names depending on the first name, Lastly, two different tasks (or printers) by other names make such choices down.
Comments
Post a Comment