Читать «Справочное руководство по C++» онлайн - страница 112
Бьярн Страустрап
for (int i=0; i‹max; i++) nvec[i] = vec[i];
delete vec;
vec = nvec;
max = 2*max;
}
pp =&vec[free++];
pp-›name = new char[strlen(p)+1];
strcpy(pp-›name,p);
pp-›val = 0;
return pp-›val;
}
main()
{
const MAX = 256;
char buf[MAX];
assoc vec(512);
while (cin››buf) vec[buf]++;
assoc_iterator next(vec);
pair* p;
while (p = next())
cout ‹‹ p-›name ‹‹ ": " ‹‹ p-›val ‹‹ "\n";
}
b6_9.cxx
#include ‹stream.hxx›
#include ‹string.h›
extern void exit(int);
class string {
struct srep {
char* s;
int n;
};
srep *p;
public:
string(char *);
string();
string(string&);
string& operator=(char *);
string& operator=(string&);
~string();
char& operator[](int i);
friend ostream& operator‹‹(ostream&, string&);
friend istream& operator››(istream&, string&);
friend int operator==(string&x, char *s)
{ return strcmp(x.p-›s, s) == 0; }
friend int operator==(string&x, string&y)
{ return strcmp(x.p-›s, y.p-›s) == 0; }
friend int operator!=(string&x, char *s)
{return strcmp(x.p-›s, s) != 0;}
friend int operator!=(string&x, string&y)
{return strcmp (x.p-›s, y.p-›s) != 0;}
};
string::string()
{
p = new srep;
p-›s = 0;
p-›n = 1;
}
string::string(char* s)
{
p = new srep;
p-›s = new char[strlen(s) +1];
strcpy(p-›s, s);
p-›n = 1;
}
string::string(string& x)
{
x.p-›n++;
p = x.p;
}
string::~string()
{
if (--p-›n - 0) {
delete p-›s;
delete p;
}
}
string& string::operator=(char* s)
{
if (p-›n › 1) {
p-›n--;
p = new srep;
}
else if (p-›n == 1)
delete p-›s;
p-›s = new char[strlen(s)+1];
strcpy(p-›s, s);
p-›n = 1;
return *this;
}
string& string::operator=(string& x)
{
x.p-›n++;
if (--p-›n == 0) {
delete p-›s;
delete p;
}
p = x.p;
return *this;
}
ostream& operator‹‹(ostream& s, string& x)
{
return s ‹‹ x.p-›s ‹‹ " [" ‹‹ x.p-›n ‹‹ "]\n";
}
istream& operator››(istream& s, string& x)
{
char buf[256];
s››buf;
x = buf;
cout ‹‹ "echo: " ‹‹ x ‹‹ "\n";
return s;
}
void error(char* p)
{
cout ‹‹ p ‹‹ "\n";