,,,
operator[]. () operator[] . .. , . . :
struct pair {
char* name;
int val;
};
class assoc {
pair* vec;
int max;
int free;
public:
assoc(int);
int& operator[](char*);
void print_all();
};
assoc pair max. free. :
assoc::assoc(int s)
{
max = (s<16) ? s : 16;
free = 0;
vec = new pair[max];
}
. assoc :
#include
int assoc::operator[](char* p)
/*
"pair":
p,
"pair"
"pair", 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; iname = new char[strlen(p)+1];
strcpy(pp->name,p);
pp->val = 0; // : 0
return pp->val;
}
assoc , . , , :
vouid assoc::print_all()
{
for (int i = 0; i>buf) vec[buf]++;
vec.print_all();
}
Copyright (c) 2024 Stud-Baza.ru , , , .