C++ – Sorting std::list using std::set

cstl

I'm adding two different elements to both std::list and std::set and I want the std::list to be sorted with the same order as of std::set. one way I tried is when the element is added to std::set, find that element then get the index of that element using std::distance(begin, found) and then insert the element to that index in std::list. is there any other way?

Best Answer

You should use the std::map, with the data you put in the set as key, and the data you put in the list as value.

This way your list elements will be ordered.