| Previous | Contents | Index |
This section explains how to upgrade from the pre-ANSI DIGITAL C++ complex library to the current DIGITAL C++ standard complex library.
In the ANSI library, complex objects are templatized on the type of the real and imaginary parts, the pre-ANSI DIGITAL C++ library, complex objects are not templatized. The pre-ANSI library assumes the type is double, whereas the new library provides specializations for float, double, and long double as well as allowing users to specialize on their own floating point types.
Also note that mathematical error checking is not supported in the ANSI library. Users who rely on detection of underflow, overflow, and divide by zero should continue using the pre-ANSI DIGITAL C++ complex library.
The following is a detailed list of important changes:
complex c; |
complex<double> c; |
complex c; c = polar(c); // get polar |
complex<double> c; c = polar(c,0.0); |
complex c = polar(0,0); complex c2 = c+1; |
complex<double> c = polar(0.0,0.0); // 0.0 is double complex<double> c2= c + 1.0; // 1.0 is double |
static const complex<double> complex_zero(0.0,0.0); |
template <class T>
inline complex<T> sqr(const complex<T>& a)
{
return complex<T>(real(a) * real(a) - imag(a) * imag(a),
2 * real(a) * imag(a));
}
template <class T>
inline T arg1(const complex<T>& a)
{
double val = arg(a);
if(val > -M_PI && val <= M_PI)
return val;
if(val > M_PI)
return val - (2*M_PI);
// val <= -PI
return val + (2*M_PI);
}
|
pow(c,1); |
pow(c,1.0); |
complex<double> c;
cout << "(" << c.real() << ", " << c.imag() << ")"; // add extra space
|
#include <complex>
int main() {
complex<double> c1(1,1), c2(3.14,3.14);
cout << "c2/c1: " << c2/c1 << endl;
}
|
This section explains how to upgrade from the pre-ANSI DIGITAL C++ IOStreams library to the ANSI DIGITAL C++ IOStreams library. In this section, pre-ANSI IOStreams refers to versions of the IOStreams library found in the DIGITAL C++ Class Library; ANSI IOStreams refers to versions found in the DIGITAL C++ Standard Library.
There are a number of differences between the pre-ANSI and ANSI IOstream library. One major difference between the pre-ANSI and ANSI IOstream library is that the ANSI library is templatized on the object input/output on which operations are being performed. In the pre-ANSI library, IOStreams has no templates. The ANSI library also provides specializations for char and wchar_t.
Important differences are as follows:
| From | To |
|---|---|
|
#include <iostream.h>
#include <iostream.hxx> |
#include <iostream> |
|
#include <fstream.h>
#include <fstream.hxx> |
#include <fstream> |
|
#include <strstream.h>
#include <strstream.hxx> |
#include <strstream> |
|
#include <iomanip.h>
#include <iomanip.hxx> |
#include <iomanip> |
using namespace std;
|
|
change
#include <iomanip.h> to #include <iomanip> #include <iostream> #include using namespace std; |
change
#include <strstream.h> to #include <strstream> #include <iostream> #include using namespace std; |
#include <iostream.hxx> | #include <iostream.hxx>
void func (istream &is) | void func (ostream &os)
{ | {
if (is.ipfx()) | if (os.opfx())
... | ...
is.isfx(); | os.osfx();
} | }
|
Would be coded as: | Would be coded as:
|
#include <iostream> | #include <iostream>
void func (istream &is) | void func (ostream &os)
{ | {
istream::sentry ipfx(is); | ostream::sentry opfx(os);
if (ipfx) | if (opfx)
... | ...
//is.isfx(); implicit in dtor | //os.osfx(); implicit in dtor
} | }
|
SMANIP, IMANIP, OMANIP, IOMANIP, SAPP, IAPP, OAPP, IOAPP, SMANIPREF, IMANIPREF, OMANIPREF, IOMANIPREF, SAPPREF, IAPPREF, OAPPREF, IOAPPREF |
istream &extract(istream &is)
{
...
is.rdbuf()->stossc();
}
|
class filebuf : public streambuf
{
filebuf();
filebuf(int fd);
filebuf(int fd, char * p, int len);
...
}
|
basic_filebuf();
|
#include <fstream.hxx>
int main () {
int fd = 1;
const int BUFLEN = 1024;
char buf [BUFLEN];
filebuf fb(fd,buf,BUFLEN);
filebuf fb1(fd);
return 0;
}
|
filebuf fb(fd,buf,BUFLEN); as filebuf fb(); and filebuf fb1(fd); as filebuf fb1(); |
filebuf fb(fd,buf,BUFLEN); as filebuf fb(fd); and filebuf fb1(fd); as filebuf fb1(fd); |
ifstream::ifstream(int fd);
ifstream::ifstream(int fd, char *p, int len)
ofstream::ofstream(int fd);
ofstream::ofstream(int fd, char *p, int len);
fstream::fstream(int fd);
fstream::fstream(int fd, char *p, int len);
|
filebuf::attach(int); fstream::attach(int); ifstream::attach(int); ofstream::attach(int); |
#include <fstream.hxx>
#include <stdio.h>
#include <fcntl.h>
int main () {
int fd;
fd = open("t27.in",O_RDWR | O_CREAT, 0644);
ifstream ifs;
ifs.attach(fd);
fd = creat("t28.out",0644);
ofstream of;
of.attach(fd);
return 0;
}
|
#include <fstream>
int main () {
ifstream ifs("t27.in", ios::in | ios::out);
ofstream ofs("t28.out");
return 0;
}
|
#ifdef __USE_STD_IOSTREAM
# include <iostream>
# include <iomanip>
#else
# include <iostream.hxx>
# include <iomanip.hxx>
#endif
int main () {
cout.width(10);
cout.fill('^');
cout << 'x' << '\n';
cout << '[' << setw(10) << 'x' << ']' << endl;
return 0;
}
|
^^^^^^^^^x [^^^^^^^^^x] |
x [x]^^^^^^^^^ |
#ifdef __USE_STD_IOSTREAM
#include <iostream>
#else
#include <iostream.hxx>
#endif
int main () {
char * cs = (char *) "Hello";
signed char *ss = (signed char *) "world";
unsigned char *us = (unsigned char *) "again";
cout << cs << " " << ss << " " << us << endl;
return 0;
}
|
Hello world again |
Hello 0x120001748 0x120001740 |
cout << hex << showbase << (long) ss << " " << (long) us << endl; |
#ifdef __USE_STD_IOSTREAM
#include <iostream>
#else
#include <iostream.hxx>
#endif
int main () {
signed char c = (signed char) 'c';
cout << c << endl;
return 0;
}
|
c |
99 |
cout << (long) c << endl; |
double i;
cin >> i;
cout << cin.rdstate() << ' ' << i << endl;
|
4 2.65261e-314 // failbit set |
0 123123 // goodbit set |
int i;
cin >> i;
cout << cin.rdstate() << ' ' << i << endl;
|
4 1874919423 // failbit set |
0 1874919423 // goodbit set |
unsigned int ui; cin >> ui; cout << cin.rdstate() << ' ' << ui << endl; |
0 0 |
main()
{
char buffer[10];
cin.getline (buffer,10);
cout << cin.rdstate() << ' ' << buffer << endl;
return 0;
}
|
4 123456789 |
0 123456789 |
#include <iostream>
main()
{
double d;
int i;
void *p = (void *) &d;
int *pi = &i;
cout << (void *) 0 << ' ' << p << ' ' pi << endl;
}
|
0 11fffe7a0 11fffe798 |
0x0 0x11fffdc40 0x11fffdc38 |
int main() {
filebuf fb;
...
fb.setbuf(0,0);
return 0;
}
|
| Previous | Next | Contents | Index |