Question 1
A performance engineer is optimizing a logging system where log entries, represented by a custom LogEntry struct, are collected into a std::vector. The requirements state that after collection, the logs must be sorted chronologically by timestamp. If two entries have the exact same timestamp, their original insertion order must be preserved for forensic analysis. Which STL algorithm and comparator setup is the most appropriate and correct choice to meet these requirements?
Answer and explanation
Correct answer: B
The key requirement is to preserve the original insertion order for entries with identical timestamps. The std::stable_sort algorithm is specifically designed for this purpose. It guarantees that the relative order of equivalent elements remains unchanged after sorting. std::sort does not provide this guarantee and may reorder elements with equal keys, which would violate the forensic analysis requirement. The other options are incorrect because std::partition only groups elements based on a predicate without fully sorting them, and std::sort on its own is insufficient.