Get the last element of a TMap<>

As Bojann said there is no such thing as a “last” element. The items in a map aren’t even “sorted” in the traditional sense of the word. The problem that you will run into with your implementation is that inserting a new element could cause any of the following things to happen: a) the existing element you thought was last to remain so, b) the new element becomes “last” or c) the container reorders and some other element of the map becomes the last. I’m not even 100% sure that two iterations of a map when there haven’t been changes to it will result in the same iteration, it seems like it should but I don’t know if it’s guaranteed.

If order matters for what you are trying to do with the operation, you should really choose a different container. Or work with multiple containers so that you have one that preserves order (call it A) and another that maps that FString key into A somehow (by index or something).

1 Like