title: How to add elements to a sorted list ...? (C++)
body: <p>I just had my first contact with std::vector and now I want to change my bad habit of using plain C-style arrays. I found that std::list is the container to use when it comes to sorting. However, I am not 100% how to accomplish the following:</p>
  
  <p>I am doing some calculations whose outcome depends on two indices (i and j). In the end I am only interested in the 100 smallest results (not necessarily 100, but for sure much smaller than my total number of calculated values, m*n in the below code). </p>
  
  <pre><code>const int L = 100;
  int i_list[L];
  int j_list[L];
  double value_list[L];
  
  for (int i=0;i&lt;m;i++){
      for (int j=0;j&lt;n;j++){
          double x = doSomeCalculations(i,j);
          insertTheValueAndIndices(i,j,x,i_list,j_list,value_list);
      }
  }
  </code></pre>
  
  <p>After completion, value_list should contain the 100 smallest values (increasing order) and i_list / j_list the corresponding indices. I have a working version of the "insertValuesAndIndices()", but there I use plain arrays and the most inefficient way of inserting new values. While writing I realize that i actually have 2 distinct questions:</p>
  
  <ol>
  <li><p>The number of computed values (m*n) is by far bigger than the number I want to keep in the list, thus simply keeping all values and sorting only once in the end is not really an option. On the other hand, I need the results in the right order only in the end, so maybe there is a way of sorting the list only once. Is there any "standard" clever and efficient way do to this kind of sorting?</p></li>
  <li><p>Even if I could store all results and do the sorting afterwards, I am not sure how to use the std::list.sort() to get the arrays of indices in the right order. What came to my mind is to define some class containing the result and the two indices, put those elements in a list and then use a comparator that checks only the value to do the sorting. However, maybe there is a more straightforward way to do the same?</p></li>
  </ol>
  
  <p>Cheers &amp; Thanks in advance</p>