W
.. und zum Abschluß die Lösung für alle Freunde der STL
#include <iostream>
#include <iterator>
#include <algorithm>
#include <sstream>
#include <stdexcept>
struct Getter // Input-Funktor
{
void operator()( int& x )
{
using namespace std;
cout << ">";
cin >> x;
}
};
int do_main()
{
using std::cout;
using std::endl;
const int ANZ = 10;
int feld1[ANZ];
try
{
std::cin.exceptions( std::ios_base::failbit | std::ios_base::badbit );
std::for_each( feld1, feld1 + ANZ, Getter() ); // Eingabe (s.o.)
}
catch( const std::exception& err )
{
std::stringstream buf;
buf << "Fehler bei der Eingabe: " << err.what();
throw std::runtime_error( buf.str() );
}
std::copy( feld1, feld1 + ANZ, // Eingabe ausgeben
std::ostream_iterator< int >( cout << "Ihre Eingabe: \n", " ") );
cout << endl;
int feld2[ANZ];
std::copy( feld1, feld1 + ANZ, // in umgekehrter Reihenfolge kopieren
std::reverse_iterator< int* >( feld2 + ANZ ) );
std::copy( feld2, feld2 + ANZ, // neues Feld 'feld2' ausgeben
std::ostream_iterator< int >( cout << "Reverse: \n", " ") );
cout << endl;
return 0;
}
int main()
{
try{ return do_main(); }
catch( const std::exception& err )
{
std::cerr << "FEHLER: " << err.what() << std::endl;
return -1;
}
}
Gruß
Werner