Discuss multiset in the C Programming forum on Dev Shed. multiset C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
The ASP Free website provides in-depth information on the latest developer tools available from Microsoft. Our cadre of writers, highly experienced industry experts, reveals the best ways to use established technologies as well as new and emerging technologies. Our coverage of Microsoft's development and administration technologies is among the most respected in the IT industry today.
ASP Free and Iron Speed Designer are giving away $5,500+ in FREE licenses. Iron Speed's RAD CASE toolset can save up to 80% of your coding time. One free license per week, one perpetual license per month! Download and Activate to enter!
Intel® Graphics Performance Analyzers is a powerful tool suite for analyzing and optimizing your games, media, and graphics-intensive applications. Used by some of the best developers on the planet, Intel GPA lets you maximize your app’s performance.
Posts: 177
Time spent in forums: 2 Days 2 h 52 m 49 sec
Reputation Power: 8
multiset
Hi.
I'm hopping someone can help me with it. I heard that you can do sorting with multiset upon insert, but I can't seem to find any information on it. LIke which methods needs to be implemmented, how it's done etc. All I found so far is that, "yes you can do it.".
Posts: 144
Time spent in forums: 7 h 48 m 6 sec
Reputation Power: 8
You need to write your own comparator
The multiset class takes a number of template arguments. Only the first one is mandatory, the type of elements stored in the set. The second one is a class that is used to order the elements in the set. The ordering allows searches in the set to be done in O(log n) time. If you were not aware of it, the multiset is implemented as a red-black tree. Have a look at the STL less<T> class. It is the default comparator. From there you should be able to write your own version of it that orders elements to your liking. I'm not sure if this is the order you will traverse the tree as well with begin(), operator++ and end() though. I hope it helped a little. Remember that ordered and sorted are not necessarily the same thing.
You don't need to do anything if you've defined an operator< function for your type/class. The function insert() uses less<T> which, in turn, uses T.operator<(T) internally. So when you call insert(), sorting is done for you.
Should you wish to know how one would perform template specialization on less<T>, say so.
Posts: 177
Time spent in forums: 2 Days 2 h 52 m 49 sec
Reputation Power: 8
So basically if I have a class Foo and I do something like:
typdef multiset <Foo> FTable
FTable f;
If I do f.insert(some instance of Foo);
If I implemented operator < in Foo it will use that to sort it appropriately?
Posts: 365
Time spent in forums: 1 Week 3 Days 9 h 53 m 7 sec
Reputation Power: 146
Quote:
Originally Posted by UmneyDurak
So basically if I have a class Foo and I do something like:
typdef multiset <Foo> FTable
FTable f;
If I do f.insert(some instance of Foo);
If I implemented operator < in Foo it will use that to sort it appropriately?
Posts: 287
Time spent in forums: 2 Weeks 6 m 56 sec
Reputation Power: 12
Yeah just define a type that implements bool operator() ( Type const& lhs, Type const& rhs) const; returning true if the left object is less than the right and false for everything else.
Posts: 177
Time spent in forums: 2 Days 2 h 52 m 49 sec
Reputation Power: 8
Wait why operator (), instead of < ? Only thing I could come up wit is that when it does compare if (obj1 < obj2) overloading parentecies, will override the less operator? I'm sure it's not right.
Tried googling, but I'm not sure what to even look for. All I got is some standard information about operator overloading in C++. In some cases people don't even bother writting their own stuff and just copy articles from other pages and pass as their own.
Thanks.
Last edited by UmneyDurak : July 21st, 2005 at 01:55 AM.
Posts: 287
Time spent in forums: 2 Weeks 6 m 56 sec
Reputation Power: 12
The second default template parameter for set (and map) is std::less. std::less is defined like:
Code:
template<class T> less
{
bool operator() ( T const & lhs, T const & rhs ) const
{
return lhs < rhs;
}
};
so when you defined a set such as std::set<foo> its going to use std::less<foo> for doing its comparison. Its acturely std::less that calles up the overloaded < operator, on the class, all the set does is call up the () operator on its key comparer.
Posts: 365
Time spent in forums: 1 Week 3 Days 9 h 53 m 7 sec
Reputation Power: 146
Quote:
Originally Posted by UmneyDurak
Wait why operator (), instead of < ?
That's because the second parameter of a multiset<> is a function object. That's an object (class or struct) that defines a member function operator(). The less<> template I linked to earlier is a function object.
The purpose of the operator() function will be to return true or false wether it's first parameter should come before it's second parameter. How it decides that is entirely up to you.
Posts: 177
Time spent in forums: 2 Days 2 h 52 m 49 sec
Reputation Power: 8
I think I understand it. Thanks.
Can you recommend a good book that talks about this in more details? All the ones I looked at so far just talk about simple stuff like how to loops, etc. I'm ok with pointers and stuff, it's just stuff like this I'm not that clear about.
Last edited by UmneyDurak : July 22nd, 2005 at 11:28 PM.
Posts: 365
Time spent in forums: 1 Week 3 Days 9 h 53 m 7 sec
Reputation Power: 146
Quote:
Originally Posted by UmneyDurak
I think I understand it. Thanks.
Can you recommend a good book that talks about this in more details? All the ones I looked at so far just talk about simple stuff like how to loops, etc. I'm ok with pointers and stuff, it's just stuff like this I'm not that clear about.
At this time I'd say you're looking into reference work. The C++ Programming Language by Stroustrup is the language's reference so you'll find in it a section on C++'s function objects and why they are good for you. Also, I'd say check out ACCU's website for book recommendation for all levels.