title: Build a histogram with a predefined number of bins in C++
body: <p>I want to build a histogram with a predefined number of bins defined by keys (in this case 12*5=60) in C++ and don't know how to do it. Here is a minimum example: </p>
  
  <pre><code>using namespace std;
  using namespace cv;
  
  // Function to calculate the keys defining specific ranges of r1 and theta_1
  void getKeys(vector&lt;float&gt; r1_vector, vector&lt;float&gt; theta_1_vector, vector&lt;vector&lt;float&gt;&gt; &amp;keys) {
      int r1_bin = 0;
      int r2_bin = 0;
      int theta1_bin = 0;
  
      vector&lt;float&gt; key;
  
      // r1 is divided equally into 5 bins ranging from -0.3 to 1.3
  
      for (size_t i = 0; i &lt; r1_vector.size(); i++) {
          if (-0.3 &lt;= r1_vector[i] &amp;&amp; r1_vector[i] &lt; 0.02) {
              r1_bin = 1;
          }
          else if (0.02 &lt;= r1_vector[i] &amp;&amp; r1_vector[i] &lt; 0.34) {
              r1_bin = 2;
          }
          else if (0.34 &lt;= r1_vector[i] &amp;&amp; r1_vector[i] &lt; 0.66) {
              r1_bin = 3;
          }
          else if (0.66 &lt;= r1_vector[i] &amp;&amp; r1_vector[i] &lt; 0.98) {
              r1_bin = 4;
          }
          else if (0.98 &lt;= r1_vector[i] &amp;&amp; r1_vector[i] &lt;= 1.30) {
              r1_bin = 5;
          }
  
      // theta_1 is divided equally into 12 bins ranging from 0 to 2*PI
  
          if (0 &lt;= theta_1_vector[i] &amp;&amp; theta_1_vector[i] &lt; CV_PI / 6) {
              theta1_bin = 1;
          }
          else if (CV_PI / 6 &lt;= theta_1_vector[i] &amp;&amp; theta_1_vector[i] &lt; CV_PI / 3) {
              theta1_bin = 2;
          }
          else if (CV_PI / 6 &lt;= theta_1_vector[i] &amp;&amp; theta_1_vector[i] &lt; CV_PI / 2) {
              theta1_bin = 3;
          }
          else if (CV_PI / 2 &lt;= theta_1_vector[i] &amp;&amp; theta_1_vector[i] &lt; 2 * CV_PI / 3) {
              theta1_bin = 4;
          }
          else if (2 * CV_PI / 3 &lt;= theta_1_vector[i] &amp;&amp; theta_1_vector[i] &lt; 5 * CV_PI / 6) {
              theta1_bin = 5;
          }
          else if (5 * CV_PI / 6 &lt;= theta_1_vector[i] &amp;&amp; theta_1_vector[i] &lt; CV_PI) {
              theta1_bin = 6;
          }
          else if (CV_PI &lt;= theta_1_vector[i] &amp;&amp; theta_1_vector[i] &lt; 7 * CV_PI / 6) {
              theta1_bin = 7;
          }
          else if (7 * CV_PI / 6 &lt;= theta_1_vector[i] &amp;&amp; theta_1_vector[i] &lt; 4 * CV_PI / 3) {
              theta1_bin = 8;
          }
          else if (4 * CV_PI / 3 &lt;= theta_1_vector[i] &amp;&amp; theta_1_vector[i] &lt; 3 * CV_PI / 2) {
              theta1_bin = 9;
          }
          else if (3 * CV_PI / 2 &lt;= theta_1_vector[i] &amp;&amp; theta_1_vector[i] &lt; 5 * CV_PI / 3) {
              theta1_bin = 10;
          }
          else if (5 * CV_PI / 3 &lt;= theta_1_vector[i] &amp;&amp; theta_1_vector[i] &lt; 11 * CV_PI / 6) {
              theta1_bin = 11;
          }
          else if (11 * CV_PI / 6 &lt;= theta_1_vector[i] &amp;&amp; theta_1_vector[i] &lt;= 2 * CV_PI) {
              theta1_bin = 12;
          }
  
          key.push_back(r1_bin);
          key.push_back(theta1_bin);
          keys.push_back(key);
          key.clear();
      }   
  }
  
  
  
  int main(int argc, char** argv)
  {
      // Create some values - both vectors have the same size
      vector&lt;float&gt; r1_vec;
      r1_vec.push_back(-0.2);
      r1_vec.push_back(1.2);
      r1_vec.push_back(0.2);
      r1_vec.push_back(0.3);
      r1_vec.push_back(0.35);
      r1_vec.push_back(0.2);
      r1_vec.push_back(0.8);
      r1_vec.push_back(0.8);
  
      vector&lt;float&gt; theta_vec;
      theta_vec.push_back(1.4);
      theta_vec.push_back(2.4);
      theta_vec.push_back(3.7);
      theta_vec.push_back(2.4);
      theta_vec.push_back(1.5);
      theta_vec.push_back(1.6);
      theta_vec.push_back(2.4);
      theta_vec.push_back(5.8);
  
      vector&lt;vector&lt;float&gt;&gt; keys;
      getKeys(r1_vec, theta_vec, keys);
  
      // Print values
      /*for (size_t i = 0; i &lt; keys.size(); i++) {    
              cout &lt;&lt; "The keys for line one are: " &lt;&lt; keys[i][0] &lt;&lt; ", " &lt;&lt; keys[i][1]  &lt;&lt; endl;
      }*/
  
  }
  </code></pre>
  
  <p>Now I don't know how to handle the 12 bins of theta_1 because I cannot simply count from 1 to 60 for my bins. I also have in mind that there could be a better way than building a histogram (e.g. unordered_map, map, bucket_sort or something like this). But therefore I would need specific/unique types for my keys.
  So in the end I want to count the occurence of every pair of keys (e.g. [2,12] has 10 occurences).<br>
  They keys might not just be pairs but even triples or quadruples and are stored in a <code>vector&lt;vector&lt;float&gt;&gt;</code>.</p>