# C++ Set to C# List... ?

This topic is 4142 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

If anyone has seen Erin Catto's Box2D, then you know the code I'm trying to convert to C#. Here's the only part I'm having trouble with. I'm not familier with Set's in C++, so can someone tell me exactly what this code is doing, when it's being used, and possibly why... ?
// This is used by std::set
inline bool operator < (const Arbiter& a1, const Arbiter& a2)
{
if (a1.body1 < a2.body1)
return true;

if (a1.body1 == a2.body1 && a1.body2 < a2.body2)
return true;

return false;
}



##### Share on other sites
Short Answer: It's a comparison function that defines the order which Arbiters are stored in a set.

Long answer: A C++ set is a dictionary that is also sorted. When moving the code to C# you'll need to examine the C++ code to see if the sorted behaviour is begin used. If it is, you'll need a sorted list, if not a dictionary.

##### Share on other sites
Awesome. I know it's being sorted, so I'll look into sorted lists. Thanks :D

And actually... how would I see if the "sorting behaviour" is being used?

##### Share on other sites
Quote:
 Original post by NitageIf it is, you'll need a sorted list, if not a dictionary.

Or a SortedDictionary, depending on how it's being used.

Quote:
 MSDN SortedDictionary pageThe SortedDictionary generic class is a binary search tree with O(log n) retrieval, where n is the number of elements in the dictionary. In this respect, it is similar to the SortedList generic class. The two classes have similar object models, and both have O(log n) retrieval. Where the two classes differ is in memory use and speed of insertion and removal:SortedList uses less memory than SortedDictionary.SortedDictionary has faster insertion and removal operations for unsorted data: O(log n) as opposed to O(n) for SortedList.If the list is populated all at once from sorted data, SortedList is faster than SortedDictionary.

EDIT: Oops, I misread that as "If not, you'll need a sorted list, not a dictionary", sorry. However, this may still be relevant so I'll let it stand.

[Edited by - TheUnbeliever on May 19, 2007 7:10:17 AM]

1. 1
2. 2
3. 3
Rutin
22
4. 4
frob
16
5. 5

• 9
• 33
• 13
• 13
• 10
• ### Forum Statistics

• Total Topics
632582
• Total Posts
3007200

×