Справочное руководство по C++ — страница 40 из 41

}


main()

{

 const MAX = 256;

 char buf[MAX];

 assoc vec(512);

 while (cin››buf) vec[buf]++;

 vec.print_all();

}

b6_8.cxx

#include ‹stream.hxx›

#include ‹string.h›


struct pair {

 char* name;

 int val;

};


class assoc {

 friend class assoc_iterator;

 pair* vec;

 int max;

 int free;

public:

 assoc(int);

 int& operator[](char*);

};

class assoc_iterator {

 assoc* cs;

 int i;

public:

 assoc_iterator(assoc& s) { cs =&s; i = 0; }

 pair* operator()()

 { return (i‹cs-›free) ? &cs-›vec[i++] : 0; }

};


assoc::assoc(int s)

{

 max = (s‹16) ? s : 16;

 free = 0;

 vec = new pair[max];

}


int& assoc::operator[](char* p)

{

 register pair* pp;


 for (pp = &vec[free-1]; vec‹=pp; pp--)

  if (strcmp(p,pp-›name)==0) return pp-›val;


 if (free == max) {

  pair* nvec = new pair[max*2];

  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";

 exit(1);

}

char& string::operator[](int i)

{

 if (i‹0 || strlen(p-›s)‹i) error("index out of range");

 return p-›s[i];

}


main()

{

 string x[100];

 int n;


 cout ‹‹ "here we go\n";