不确定我是否做对了,或者即使我被允许使用指针进行排序。我制作了一个指向其他元素的结构。然后制作了这个结构的向量,并尝试对其进行一些交换。
起初我在交换操作中使用了取消引用运算符,但是当我这样做时,我的排序变得很奇怪,我在最终结果中得到了一堆重复的#(它们都应该是唯一的),从而解决了取消引用问题这个,但它没有排序(除非可能通过内存地址)。
所以这是我的代码,关注的领域是 sortSuperStruct 函数
#include <algorithm> //used for swap, would like to use for sort, but oh well
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
//#include <math.h>
#include <string>
#include <sstream>
#include <vector>
//------------------------------------------------------------------------------
//namespaces?
//------------------------------------------------------------------------------
using namespace std;
//------------------------------------------------------------------------------
//prototypes
//------------------------------------------------------------------------------
struct superStruct
{
//probably not needed
float *percent;
//copy of Dwarf's role pPercent;
float *pPercent;
int *roleName;
int *name;
};
//parallel array to follow along with Dwarf role
struct roles
{
int name;
float priority;
int numberToAssign;
void setRolePriorities ()
{
priority = (float)rand()/(float)RAND_MAX;
}
void setNumberToAssign(int n)
{
numberToAssign = rand() % n + 1;
}
};
//a fit for a role, a Dwarf has this
struct role
{
float percent;
//copy of roles name
int *roleName;
//copy of roles priority
float *rolePriority;
float pPercent;
void set_pPercent()
{
pPercent = percent * (*rolePriority);
};
};
struct Dwarf
{
vector <role> roleValues;
int *maxLabor;
int name;
void setRoleValues(int n, vector <roles> *roleS)
{
srand ( time(NULL) );
//rolesValues
roleValues.resize(n);
//set role %'s
for (int x = 0; x < n; x++)
{
roleValues[x].percent = (float)rand()/(float)RAND_MAX;
//cout << roleValues[x].percent << endl;
//I said I'm pointing to the address value of roleS...
roleValues[x].roleName = &roleS->at(x).name;
roleValues[x].rolePriority = &roleS->at(x).priority;
roleValues[x].set_pPercent();
}
};
};
void initializeSuperStruct(vector <superStruct> *s, vector <Dwarf> *d, int n)
{
//for each Dwarf
int counter = 0;
for (int x = 0; x < d->size(); x++)
{
//for each role
for (int y = 0; y < n; y++)
{
s->at(counter).pPercent = &d->at(x).roleValues[y].pPercent;
s->at(counter).name = &d->at(x).name;
//both are pointers.
s->at(counter).roleName = d->at(x).roleValues[y].roleName;
//cout << d->at(x).roleValues[y].percent << endl;
s->at(counter).percent = &d->at(x).roleValues[y].percent;
counter++;
}
};
};
void printSuperStruct(vector <superStruct> *s)
{
for (int x = 0; x < s->size(); x++)
{
cout << "Count: " << x << endl;
cout << "Name: " << *s->at(x).name << endl;
cout << "Percent: " << *s->at(x).percent << endl;
cout << "pPercent: " << *s->at(x).pPercent << endl;
cout << "Role Name: " << *s->at(x).roleName << endl;
}
};
void sortSuperStruct(vector <superStruct> *s)
{
//runs through list
for (int i = 0; i < s->size(); i++)
{
//checks against lower element
//cout << *s->at(i).pPercent << endl;
for (int j = 1; j < (s->size()-i); j++)
{
//using pointer dereferences messed this up, but, not using them doesn't sort right
if (*s->at(j-1).pPercent < *s->at(j).pPercent)
{
//cout << *s->at(j-1).pPercent << "vs. " << *s->at(j).pPercent << endl;
//I don't think swap is working here
//superStruct temp;
//temp = s->at(j-1);
//s->at(j-1) = s->at(j);
//s->at(j) = temp;
swap(s->at(j-1), s->at(j));
//cout << *s->at(j-1).pPercent << "vs. " << *s->at(j).pPercent << endl;
}
}
}
}
int main()
{
srand (time(NULL));
int numberOfDwarves;
int numberOfRoles;
int maxLabors;
vector <superStruct> allRoles;
vector <Dwarf> myDwarves;
vector <roles> myRoles;
cout << "number of Dwarf's: " << endl;
cin >> numberOfDwarves;
cout << "max labor per dwarf" << endl;
cin >> maxLabors;
cout << "number of Roles: " << endl;
cin >> numberOfRoles;
//this will probably have to be a vector
//Dwarf myDwarfs[numberOfDwarves];
myDwarves.resize(numberOfDwarves);
allRoles.resize(numberOfDwarves*numberOfRoles);
myRoles.resize(numberOfRoles);
//init roles first
for (int x = 0; x < numberOfRoles; x++)
{
myRoles[x].name = x;
myRoles[x].setRolePriorities();
myRoles[x].setNumberToAssign(numberOfDwarves);
}
for (int x = 0; x < numberOfDwarves; x++)
{
myDwarves[x].setRoleValues(numberOfRoles, &myRoles);
myDwarves[x].name = x;
//messy having this here.
myDwarves[x].maxLabor = &maxLabors;
}
initializeSuperStruct(&allRoles, &myDwarves, numberOfRoles);
cout << "Before sort, weighted matrix" << endl;
system("pause");
printSuperStruct(&allRoles);
sortSuperStruct(&allRoles);
system("pause");
cout << "After sort, weighted matrix" << endl;
printSuperStruct(&allRoles);
}
输出:
Role Name: 5
Count: 96
Name: 6
Percent: 0.175787
pPercent: 0.0178163
Role Name: 5
Count: 97
Name: 7
Percent: 0.175787
pPercent: 0.0178163
Role Name: 5
Count: 98
Name: 8
Percent: 0.175787
pPercent: 0.0178163
Role Name: 5
Count: 99
Name: 9
Percent: 0.175787
pPercent: 0.0178163
Role Name: 5