#include #include #include #include #include #include #include #include // ::::::::::::::::::::::::::::::::::::::::::::::::::::: // Test du support de la STL par le compilateur // string, vector, map, complex exception // ::::::::::::::::::::::::::::::::::::::::::::::::::::: using namespace std; // -------------------------------------------------------------------- // definition d'une classe de conversion de C (complexes) <> R (reel) class convCR { public: inline convCR() { dv = 0.; dv_im = 0.; } inline convCR(convCR const& x) { dv = x.dv; dv_im = x.dv_im; } inline convCR(float v) { dv = (double)v; dv_im = 0.; } inline convCR(double v) { dv = v; dv_im = 0.; } inline convCR(complex const & z) { dv = (double)z.real(); dv_im = (double)z.imag(); } inline convCR(complex const & z) { dv = z.real(); dv_im = z.imag(); } inline convCR& operator= (float v) { dv = (double)v; dv_im = 0.; return (*this); } inline convCR& operator= (double v) { dv = v; dv_im = 0.; return (*this); } inline convCR& operator= (complex const & z) { dv = (double)z.real(); dv_im = (double)z.imag(); return (*this); } inline convCR& operator= (complex const & z) { dv = z.real(); dv_im = z.imag(); return (*this); } inline operator float() const { return((float)dv); } inline operator double() const { return(dv); } inline operator complex() const { return(complex((float)dv, (float)dv_im)); } inline operator complex() const { return(complex(dv, dv_im)); } inline complex ToComplexF() const { return(complex((float)dv, (float)dv_im)); } inline complex ToComplexD() const { return(complex(dv, dv_im)); } double dv; double dv_im; /* for holding imaginary part of a complex */ }; // -------------------------------------------------------------------- int main(int narg, char *arg[]) { cout << "\n :::::::: Test complex / vector et map de la stl :::::::: \n" ; int rc = 0; try { vector vi; int k; for(k=0; k<5; k++) vi.push_back(k*5); cout << " -----> vector vi.size() = " << vi.size() << endl; for(k=0; k::iterator it; cout << " -----> map msi.size() = " << msi.size() << endl; for(it=msi.begin(); it!=msi.end(); it++) cout << " S= " << (*it).first << " I=" << (*it).second << endl; cout << " ----> complex numbers and convCR : " << endl; complex z,z1(2,2),z2(3,-1); complex zd,zd1(2.5,2.5),zd2(3.3,-1); cout << " complex: z1=" << z1 << " z2=" << z2 << " z1+z2=" << z1+z2 << endl; cout << " complex: z1=" << zd1 << " z2=" << zd2 << " zd1+zd2=" << zd1+zd2 << endl; convCR czr, czr1(z1), czr2(zd2), czr3; /* if defined(__GNUG__) && (__GNUC__ < 3) */ complex za; complex zb,zc; za = czr1.ToComplexF(); zb = czr1.ToComplexD(); zc = czr2.ToComplexD(); cout << " convCR:.ToComplexF/D() za=(zb)=czr1" << za << " zc=" << zc << " zb+zc=" << zb+zc << endl ; #if defined(__GNUG__) cout << " GNUG_Version: __GNUC__ = " << __GNUC__ << endl; #else cout << " convCR: (complex)(czr1(z1))" << (complex)(czr1) << " czr2(z2)" << (complex)(czr2) << " czr1+czr2= " << (complex)czr2+(complex)czr1 << endl; #endif complex zdd; zdd = static_cast< complex >(czr1); cout << " zdd = static_cast< complex >(czr1) = " << zdd << endl; } catch(exception exc){ // Classe de base des exception standard - // La methode what() n'est pas forcement conforme a la norme string msg = exc.what(); cerr << " Catched exception : msg= " << msg << endl; rc = 98; } catch (...) { cerr << " Catched Unknown exception ! " << endl; rc = 99; } cout << " ::::::::: Exiting from vecmapstl.cc ::::::::: \n" << endl; exit(rc); }