Optimizing priority queue streaming algorithm in C++ This is a sorting algorithm that I wrote for finding the maximum \$n\$ elements at any time in a stream of IDs and integers with unknown length. I primarily do my coding in Python, so I'm not too familiar with C++ best practices, but I wanted to write this in C++ to gain some efficiency and speed.

For all of you seasoned C++ programmers; am I following best practices here or not? If not, what improvements can I make to make this more professional? Lastly, are there any optimizations that I can make to increase its speed?

#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;queue&gt;
#include &lt;vector&gt;
#include &lt;map&gt;
#include &lt;chrono&gt;

// Overload comparison operator to create a min heap
struct compare
{
  bool operator()(const int&amp; l, const int&amp; r)
  {
    return l &gt; r;
  }
};

int main()
{
  // Input filenames
  std::cout &lt;&lt; "Enter a filename for the input stream: ";
  std::string filename;
  std::cin &gt;&gt; filename;
  std::ifstream infile(filename);

  std::cout &lt;&lt; "Enter a filename for the output stream: ";
  std::string output;
  std::cin &gt;&gt; output;
  std::ofstream outfile(output);

  // Input a value for k
  std::cout &lt;&lt; "Enter a value for k: ";
  int k;
  std::cin &gt;&gt; k;
  std::cout &lt;&lt; std::endl;

  // Declare priority queue and map
  int elements = 0;
  int replacements = 0;
  int value;
  std::string id;
  std::priority_queue&lt;int, std::vector&lt;int&gt;, compare&gt; heap;
  std::map&lt;int, std::vector&lt;std::string&gt;&gt; ids;

  // Begin read/sort timer
  std::cout &lt;&lt; "Beginning read and sort ..." &lt;&lt; std::endl;
  auto readsortstart = std::chrono::high_resolution_clock::now();

  // Begin read and sort
  while (infile &gt;&gt; id &gt;&gt; value){
    elements++;
    if (heap.size() &lt; k){
      ids[value].push_back(id);
      heap.push(value);
    } else if (heap.top() &lt; value) {
      ids[value].push_back(id);
      heap.pop();
      heap.push(value);
      replacements++;
    }
  }

  // End timer
  auto readsortstop = std::chrono::high_resolution_clock::now();
  auto readduration = std::chrono::duration_cast&lt;std::chrono::milliseconds&gt;(readsortstop-readsortstart).count();
  std::cout &lt;&lt; "Read and sort completed." &lt;&lt; std::endl &lt;&lt; std::endl;

  // Ensure that k is not larger than the total number of lines
  k = std::min(k,elements);

  // Begin write timer
  std::cout &lt;&lt; "Beginning write ..." &lt;&lt; std::endl;
  auto writestart = std::chrono::high_resolution_clock::now();

  // Pop k entries off of the sorted heap to retrieve the k largest results
  for (int i = 0; i &lt; k; i++) {
    int key = heap.top();
    outfile &lt;&lt; ids[key].back() &lt;&lt; " " &lt;&lt; heap.top() &lt;&lt; std::endl;
    ids[key].pop_back();
    heap.pop();
  }

  // End write timer
  auto writestop = std::chrono::high_resolution_clock::now();
  auto writeduration = std::chrono::duration_cast&lt;std::chrono::milliseconds&gt;(writestop-writestart).count();
  std::cout &lt;&lt; "Write completed." &lt;&lt; std::endl &lt;&lt; std::endl;

  std::cout &lt;&lt; "----------------------- Statistics ------------------------" &lt;&lt; std::endl 
            &lt;&lt; "  Total number of replacements: " &lt;&lt; replacements &lt;&lt; std::endl
            &lt;&lt; "  Total number of elements:     " &lt;&lt; elements &lt;&lt; std::endl
            &lt;&lt; "  Read/sort time elapsed:       " &lt;&lt; readduration &lt;&lt; "ms" &lt;&lt; std::endl
            &lt;&lt; "  Write time elapsed:           " &lt;&lt; writeduration &lt;&lt; "ms" &lt;&lt; std::endl;
}
You could create a

struct Element {
   int value;
   std::string id;
};


and store that in the priority queue. Then you don't need the map. Your compare function would of course only look at the value component:

struct compare {
   bool operator()(const Element&amp; l, const Element&amp; r) {
       return l.value &gt; r.value;
   }
};


Note that it is possible to use a naked function as comparator, it doesn't need to be a functor:

bool compare(const Element&amp; l, const Element&amp; r) {
   return l.value &gt; r.value;
}

std::priority_queue&lt;Element, std::vector&lt;Element&gt;, decltype(compare)&gt; heap(compare);