Efficient way to get middle (median) of an std::set?

后端 未结 5 1767
故里飘歌
故里飘歌 2021-01-07 23:52

std::set is a sorted tree. It provides begin and end methods so I can get minimum and maximum and lower_bound and u

5条回答
  •  盖世英雄少女心
    2021-01-08 00:03

    It's going to be O(size) to get the middle of a binary search tree. You can get it with std::advance() as follows:

    std::set::iterator it = s.begin();
    std::advance(it, s.size() / 2);
    

提交回复
热议问题