Counting occurences in a vector
<p>This program reads strings of numbers from a txt file, converts them to integers, stores them in a vector, and then tries to output them in an organized fashion like so....</p>

<p>If txt file says:</p>

<pre><code>7 5 5 7 3 117 5
</code></pre>

<p>The program outputs:</p>

<pre><code>3
5   3
7   2
117
</code></pre>

<p>so if the number occurs more than once it outputs how many times that happens. Here is the code so far.</p>

<p>Solved: Final code:</p>

<pre><code>#include "std_lib_facilities.h"

void print_numbers(const vector&lt;int&gt;&amp; num)
{
 int counter = 1;
 for(int i = 0; i &lt; num.size(); ++i)
 {
   if(i&lt;num.size()-1 &amp;&amp; num[i] == num[i+1]) ++counter;

   else if(i&lt;num.size()-1 &amp;&amp; num[i]!=num[i+1])
   {
        if(counter &gt; 1)
        {
          cout &lt;&lt; num[i] &lt;&lt; '\t' &lt;&lt; counter &lt;&lt; endl;
          counter = 1;
        }
        else cout &lt;&lt; num[i] &lt;&lt; endl;
   }

   else if(i == num.size()-1)
   {
        if(counter &gt;1) cout &lt;&lt; num[i] &lt;&lt; '\t' &lt;&lt; counter &lt;&lt; endl;
        else cout &lt;&lt; num[i] &lt;&lt; endl;
   }
  }
}



int str_to_int(string&amp; s)
{
    stringstream ss(s);
    int num;
    ss &gt;&gt; num;
    return num;
}

int main()
{
    cout &lt;&lt; "Enter file name.\n";
    string file;
    cin &gt;&gt; file;
    ifstream f(file.c_str(), ios::in);

    string num;
    vector&lt;int&gt; numbers;
    while(f&gt;&gt;num)
    {
        int number = str_to_int(num);
        numbers.push_back(number);
    }

    sort(numbers.begin(), numbers.end());
    print_numbers(numbers);

    keep_window_open();
}
</code></pre>
