64,651
社区成员
发帖
与我相关
我的任务
分享
int main()
{
int x;
cout << "enter choice:";
cin >> x;
while (true)
{
cout << "Invalid choice, try again:";
cin >> x;
// not a numeric character, probably
// clear the failure and pull off the non-numeric character
if (cin.fail())
{
cin.clear();
char c;
cin >> c;
}
}
}
#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>
#include <cmath>
#include <cassert>
using namespace std;
template< typename T >
class Sqr
{
public:
void operator()( T& value )
{
assert( value >= 0 );
value = sqrt( value );
}
};
int main()
{
vector<long double> arr;
copy( istream_iterator<long double>(cin), istream_iterator<long double>(), back_inserter(arr) );
for_each( arr.begin(), arr.end(), Sqr<long double>() );
copy( arr.rbegin(), arr.rend(), ostream_iterator<long double>(cout, "\n") );
return 0;
}