std::priority_queue は要素 T を push() しておくと優先順位の高い(=大きい)順に pop() できます。 int を要素とし、その一ケタ目を優先順位とみなして 0...29 を push() し、一気に pop() してみましょう。 #include <iostream> #include <queue> // 一ケタ目だけを比較する struct less_digit { bool operator()(int x, int y) const { return (x%10) < (y%10); } }; using namespace std; int main() { priority_queue<int,std::vector<int>,less_digit> pq; for ( int i = 0; i < 30; ++i ) { pq.push(i)