For brevity I have used the old ed syntax: s/old-text/new-text/
int main(int argc, char *argv[])
{
if (argc != 3) error("two arguments expected");
int count = 0;
int m = atoi(argv[1]); // number of set members
int n = atoi(argv[2]); // in the range 1..n
intset s(m,n);
while (count<m) {
int t = randint(n);
if (s.member(t)==0) {
s.insert(t);
count++;
}
}
print_in_order(&s);
}
template<class T>
class Isplist : private slist_base {
public:
void insert(T* p) { slist_base::insert(p); }
void append(T* p) { slist_base::append(p); }
T* get() { return (T*) slist_base::get(); }
};
template<class T, class Comp> void Sort<T,Comp>::sort(Vector<T>& v)
{
for (int i=0; i<n-1; i++)
for (int j=n-1; i<j; j--)
if (Comp::lessthan(v[j],v[j-1])) {
T temp = v[j];
v[j] = v[j-1];
v[j-1] = temp;
}
}
template<class T> void f1(T); // fine template<class T> void f2(T*); // fine template<class T> T f3(int); // error template<int i> void f4(int[][i]); // error template<int i> void f5(int = i); // error template<class T, class C> void f6(T); // error template<class T> void f7(const T&, complex); // fine template<class T> void f8(Vector< List<T> >); // fine
class streambuf { // manage a stream buffer
protected:
char* base; // start of buffer
char* pptr; // next free char
char* gptr; // next filled char
char* eptr; // one off the end of buffer
char alloc; // buffer allocated by "new"
// ...
// Empty a buffer:
// Return EOF on error, 0 on success
virtual int overflow(int c =EOF);
// Fill a buffer:
// Return EOF on error or end of input,
// next char otherwise
virtual int underflow();
// ...
public:
streambuf();
streambuf(char* p, int l);
virtual ~streambuf();
int snextc() // get the next char
{
return (++gptr==pptr) ? underflow() : *gptr&0377;
}
int allocate(); // allocate some buffer space
// ...
};
class filebuf : public streambuf {
protected:
int fd; // file descriptor
char opened; // file opened
public:
filebuf() { opened = 0; }
filebuf(int nfd, char* p, int l)
: streambuf(p,l) { /* ... */ }
~filebuf() { close(); }
int overflow(int c =EOF);
int underflow();
filebuf* open(char *name, ios::open_mode om);
int close() { /* ... */ }
// ...
};
class X {
static Pool my_pool;
// ...
public:
// ...
void* operator new(size_t) { return my_pool.alloc(); }
void operator delete(void* p) { my_pool.free(p); }
};
Pool X::my_pool(sizeof(X));
Pool::Pool(unsigned sz)
: esize(sz)
{
head = 0;
}
People have been polite enough not to inquire how so many errors could creep in. In the case of most non-stylistic bugs, the answer is in trouble with the software that inserts fragments of the tested code examples into the text.