I created this little project in C++ for a basketball 'gameish' thing. One thing I wanted to implement was a funciton that allowed one to see who had the highest or lowest stats out of all of the players, in this case, specifically, points per game (ppg).
I broke up the stat components of the players and the names of the players into two different vectors, vector <vector <string> > names
and vector <vector <double>> stats
. I made it so that names[0] correlated to stats[0], so LeBron and his stats would be represented by say 1. I figured out how to sort the vectors by descending or ascending order, but when I sort a vector, say stats[0], the order is now different from the original order, and so it doesn't align with names[0]. So when I print stats[0][i] belongs to names[0][i] it no longer is true, as the two are no longer 'connected'. I've tried to troubleshoot this myself first, but came up empty, and then tried searching online for an answer which I haven't found, perhaps I didn't look hard enough. What do I do to make it so that the now mixed-up order of the stats vector can be written out correctly?
This code I created, seen below, works sometimes, for ppg it works until around 20. I tried to create it so that you could use any vector .
void sorting_test(bool ascend, int number, vector <double> statistic) {
vector <vector <double>> ppg_desc {
{},//ppg
{},//slot
{},//locked
};
//
for (int i = 0; i < statistic.size(); i++) {
ppg_desc[0].push_back(statistic[i]);
ppg_desc[1].push_back(i);
}
sort(ppg_desc[0].begin(), ppg_desc[0].end());
if (ascend == false)
reverse(ppg_desc[0].begin(), ppg_desc[0].end());
//
for (int i = 0; i < statistic.size(); i++) {
//
bool lok = false;
//
for (int f = 0; f < statistic.size(); f++) {
//
for (int d = 0; d < ppg_desc[2].size(); d++) {
//
if (i == ppg_desc[1][d]) {
//
lok = true;
//
for (int s = f + 1; s < statistic.size(); s++) {
//
if (statistic[s] == ppg_desc[0][i]) {
lok = false;
f = s;
if (f >= statistic.size()) f = statistic.size() - 1;
}
}
}
}
//
//if (stats[6][f] == ppg_desc[0][i] && lok == false) ppg_desc[1][i] = f, ppg_desc[2].push_back(ppg_desc[1][i]), cout<<i<<"-";
if (statistic[f] == ppg_desc[0][i] && lok == false) ppg_desc[1][i] = f, ppg_desc[2].push_back(i);
}
}
//
for (int i = 0; i < number; i++) {
cout << i << ".) " << ppg_desc[0][i] << " - " << names[0][ppg_desc[1][i]] << endl;
}
//
}