Compute and print the sum and mean of name and value pairs I'm just looking for more feedback on style/approach regarding this small problem. Also, if you have tips to make it more readable or C++-like, please let me know.

Also, why is the problem asking for the sum and mean of each name and all names, while the sum of a single element is itself? Am I missing anything in terms of answering the question?

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;map&gt;
#include &lt;string&gt;
#include &lt;iostream&gt;
#include &lt;sstream&gt;
#include &lt;algorithm&gt;
#include &lt;iterator&gt;

/********************************************************************
 * Read a sequence of possibly whitespace-separated (name,value)
 * pairs, where the name is a single whitespace-separated word and
 * the value is an integer or floating-point value. Compute and print
 * the sum and mean for each name and the sum and mean for all names
 * ******************************************************************/

using namespace std;

std::multimap&lt;string,float&gt; _map;

istream&amp; operator&gt;&gt;(istream&amp; stream, pair&lt;string,float&gt; &amp;in ) {
    return stream &gt;&gt; in.first &gt;&gt; in.second;
}

ostream&amp; operator&lt;&lt;(ostream&amp; stream, pair&lt;string,float&gt; &amp;out ) {
    return stream &lt;&lt; "(" &lt;&lt; out.first 
                  &lt;&lt; ", " &lt;&lt; out.second &lt;&lt; ")" &lt;&lt; endl;
}

int main( int argc, char *argv[ ] ) { 
    istream *is = &amp;cin;
    do {
        pair&lt;string,float&gt; input;
        (*is) &gt;&gt; input;
        _map.insert(input);
    } while( is-&gt;peek( ) != EOF );

    ostream *os = &amp;cout;
    multimap&lt;string,float&gt;::iterator mit = _map.begin( );
    float sum = 0.0;
    while( mit != _map.end( ) ) {
        pair&lt;string,float&gt; p_pair = (*mit);
        (*os) &lt;&lt; p_pair;
        sum+=p_pair.second;
        mit++;
    }
    float mean = static_cast&lt;float&gt;( sum/_map.size( ) );
    (*os) &lt;&lt; "Sum: " &lt;&lt; sum &lt;&lt; " Mean: " &lt;&lt; mean &lt;&lt; endl;
}
Although it might be a bit longer (I haven't bothered to count lines), I think I'd prefer something on this order:

#include &lt;map&gt;
#include &lt;string&gt;
#include &lt;numeric&gt;
#include &lt;iostream&gt;
#include &lt;utility&gt;
#include &lt;iterator&gt;
#include &lt;algorithm&gt;

typedef std::multimap&lt;std::string, float&gt; vtype;
typedef std::pair&lt;std::string, float&gt; ptype;

namespace std { 
std::istream &amp;operator&gt;&gt;(std::istream &amp;is, ptype &amp;v) { 
    return is &gt;&gt; v.first &gt;&gt; v.second;
}
}

float add_second(float a, vtype::value_type b) { 
    return a + b.second;
}

template &lt;class FwdIt&gt;
float average(FwdIt start, FwdIt end) { 
    float total = std::accumulate(start, end, 0.0f, add_second);
    return total / std::distance(start, end);
}

int main() {
    vtype values((std::istream_iterator&lt;ptype&gt;(std::cin)), 
                  std::istream_iterator&lt;ptype&gt;());

    vtype::iterator start, end;

    for (start = values.begin(); start != values.end(); start=end) {
        end = values.upper_bound(start-&gt;first);
        std::cout &lt;&lt; start-&gt;first &lt;&lt; ": " &lt;&lt; average(start, end) &lt;&lt; "\n"; 
    }
    std::cout &lt;&lt; "Overall: " &lt;&lt; average(values.begin(), values.end()) &lt;&lt; "\n";
    return 0;
}


If you preferred, you could use an std::vector instead of an std::multimap. That would require some minor changes to the code (e.g., using std::upper_bound instead of std::multimap::upper_bound, and adding a call to std::sort after reading the data, but would otherwise be pretty similar. It would, however, probably be a bit more efficient (less CPU time and memory).

While I suppose others might disagree, I prefer this for the simple reason that I find it easier to understand. We start by reading in all the data. We then find each run of identical keys, and compute the average for that run and print it out. When we reach the end of the data, we do the same for the overall average.

In fairness, if you were working with huge amounts of data, this probably wouldn't be the best way do do things. It does spend more time walking through the same data than is strictly necessary. At the same time, unless you're going to rewrite the code to overlap reading the data with computing the result, it's unlikely to make much real difference -- almost regardless of how you do the computation, that I/O will almost certainly occupy the vast majority of the overall time.