How to convert a sorted std::list of std::pair to a std::map

前端 未结 3 1206
南方客
南方客 2021-01-12 09:27

I have got a std::list< std::pair >, which I know is sorted according to the std::string element.

Since I

3条回答
  •  花落未央
    2021-01-12 09:56

    If you already have a sorted list, which is sorted according to the predicate Predicate, you can just do the following:

    std::list< std::pair > sorted_list;
    std::map map(sorted_list.begin(), sorted_list.end());
    

    The map constructor has linear time complexity if your list is already sorted, O(n*log n) otherwise. You can then work directly with the map as you would any other.

    If you later want the results back in your list you could just do the opposite:

    sorted_list.assign(map.begin(), map.end());

提交回复
热议问题