Home > Back-end >  Indirect sort tree class object array
Indirect sort tree class object array

Time:10-07

#include
#include
#include
#include
using namespace std;

The class codeMonkey;
The class produceDog;

The class produceDog
{
Public:
Void add_todelist (codeMonkey& , int);
Void reduce_todelist (codeMonkey& , int);
};

The class codeMonkey
{
Public:
Void the set (string, int);
Int sizeof_todelist ();
String getname ();
Friend void produceDog: : add_todelist (codeMonkey& , int);
Friend void produceDog: : reduce_todelist (codeMonkey& , int);
Private:
string name;
Int todelist;
};
Void codeMonkey: : set (string n, int to)
{
Name=n;
Todelist=to;
}

Void produceDog: : add_todelist (codeMonkey& Cm, int tode)
{
Cm. Todelist +=tode;
}
Void produceDog: : reduce_todelist (codeMonkey& Cm, int tode)
{
Cm. Todelist -=tode;
}

Int codeMonkey: : sizeof_todelist ()
{
Return todelist;
}
String codeMonkey: : getname ()
{
return name;
}

Int comp (const void * a, const void * b)
{
Return (((codeMonkey *) a) - & gt; Getname () & gt; B)=((codeMonkey *) - & gt; Getname ())? 1:1;
}


Int the find (string name, codeMonkey * code [], int a, b int)//dichotomy find
{
If (a<=b)
{
Int k=(a + b)/2;
If (name==code [k] - & gt; Getname ()) return (a + b)/2;
Else if (nameThe else k=find (name, code, k + 1, b);
The return of k;
}
The else return 10000;
}


Int main ()
{
Int n, m, select;
string name;
Int todelist;
ProduceDog produceDog;
Cin> n;
CodeMonkey * codeMonkey=new codeMonkey [n].
CodeMonkey * codem [10000];//pointer array, indirect sort pointer array
for(int i=0; i{
Cin> Name> Todelist;
Codemonkey [I]. Set (name, todelist);
Codem [I]=& amp; Codemonkey [I];
}

Tree codem, n, sizeof (codeMonkey *), comp);

for(int i=0; icout
int x;
Cin> m;
for(int i=0; i{
Cin> Name> The select;
Int y=find (name, codem, 0, n);
Cin> x;
The switch (select)
{
Case: 0 producedog. Add_todelist (codemonkey [y], x); break;
Case 1: producedog. Reduce_todelist (codemonkey [y], x); break;
}
}
for(int i=0; i{
cout}
return 0;

}


Fast row add dichotomy lookup, but fast row array of objects not success! Give advice or comments!

CodePudding user response:

A simpler pointer array, such as a pointer to an int array, such comp function is more concise, and then look at the problem in comp link or tree
  • Related