程序代码:
#include <iostream>
#include <vector>
using namespace std ;
int main( void )
{
vector<int> vec;
for( int val; cin>>val; )
vec.push_back( val );
int sum = 0;
for( size_t i=0; i!=vec.size(); ++i )
sum += vec[i];
double avg = vec.empty() ? 0.0 : sum*1.0/vec.size();
cout << "count = " << vec.size() << '\n';
cout << "sum = " << sum << '\n';
cout << "average = " << avg << endl;
return 0;
}
程序代码:
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <numeric>
using namespace std;
int main( void )
{
vector<int> vec;
copy( istream_iterator<int>(cin), istream_iterator<int>(), back_inserter(vec) );
int sum = accumulate( vec.begin(), vec.end(), int() );
double avg = vec.empty() ? 0.0 : sum*1.0/vec.size();
cout << "count = " << vec.size() << '\n';
cout << "sum = " << sum << '\n';
cout << "average = " << avg << endl;
return 0;
}