0

Hello once again stackoverflow folks, this is really hard and it's been torturing my brain. What I must do is, a multiplayer server-sided scoreboard. Basically 10 clients are connected into my server application, during this event everytime a player kill another player his kills are increased, each player connected to the server have a class pointer

class CUser
{
(...)
    public:
    unsigned m_uEventKills;
    unsigned m_uEventDeaths;
(...)
};

m_uEvenKills is increased everytime someone kills someone and ofc, m_uEventDeaths also increase for the dying player. Imagine player a,b,c,d,e,f each one of them have random kills (a,b,c,d... is a pointer to CUser) so:

a->m_uEventKills is 72
b->m_uEventKills is 13
c->m_uEventKills is 2
d->m_uEventKills is 44
e->m_uEventKills is 21
f->m_uEventKills is 33

and random values for deaths.

I have this:

int nMax[10];

void OrganizeMax()
{
    memset( &nMax, 0, sizeof( nMax ));
    (...)

Organize max must fill nMax, from 0 to 9 (10 top users) reading from all connected users (all of them own a pointer to CUser) ->m_uEventKills, and set to nMax[0] the one with most kills, to nMax[1] the second with most kills, to nMax[2] the thirt with most kills and so on... Any briliant ideas of how to do this?

-Edit

I'll be more simple, i have 10 vars.

int a,b,c,d,e,f,g,h,i,j;
a = 3;
b = 61;
c = 29;
d = 44;
e = 12;
f = 8;
g = 27;
h = 11;
i = 0;
j = 4;

I need to insert these vars into

int nHold[10];

in decreasing order, how do I do that?

Vinicius Horta
  • 233
  • 2
  • 13
  • then shouldn't nMax be of type CUser * ? – minus Jun 28 '12 at 23:30
  • 1
    As an "FYI"...there is no need to `memset` global arrays to zero in C++. It's in the standard that the compiler will [do this for you](http://stackoverflow.com/questions/6991409/how-can-i-initialize-an-array-globally-in-c-or-c). Moreover, if you ever see things like memset, memcpy, strcat, or the like...then you're not doing things "the C++ way". – HostileFork says dont trust SE Jun 28 '12 at 23:39
  • memset is set to 0 because OrganizeMax is called multiple times, so when the array is filled, its set to 0 again OK? – Vinicius Horta Jun 29 '12 at 00:17

2 Answers2

2

First brilliant idea: avoid using raw C-style arrays, they do little error checking for you...cannot be dynamically resized...and don't really help you that much in terms of performance even when you get them right.

(std::vector is good, std::array if you feel that you must).

Second brilliant idea: employ already-written sorting algorithms, such as std::sort.

  • @ViniciusHorta Third brilliant idea is to take a time-out and read ["Learning Standard C++ as a New Language"](http://www2.research.att.com/~bs/new_learning.pdf) by Bjarne Stroustrup. It's accessible, very short, and one of the best captures of the "why" of C++ I've found yet. *(In truth, suggesting you take a break to read this is perhaps the only actual unusually good idea I have to contribute here. The rest is "obvious". :-P)* – HostileFork says dont trust SE Jun 28 '12 at 23:45
2

Instead of using a, b ...f, I'd use a std::vector of CUser (BTW, I don't like CUser as a class name):

std::vector<CUser> users;

If this is the only order you care about with respect to users, I'd define the ordering as part of the CUser class:

class CUser { 

    // ...
    bool operator<(CUser const &other) const { 
         return m_uEventKills < other.m_uEventKills;
    }
};

Then when you need the users sorted into the correct order, you can do:

std::sort(users.begin(), users.end());

You will, however, probably need to add an ID number (or something like that) to the CUser class to keep track of which user is which, regardless of the order in which they happen to be arranged at the moment.

Edit:

To just get the values of ints in a...j into nHold in ascending order, you probably want to just insert them in their existing order, then sort:

nHold[0] = a;
nHold[1] = b;
//...
nHold[9] = j;

std::sort(nHold, nHold+9);
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111