'There is a problem sorting the vector in the classes based on the variables in the class
I put a custom class in a vector, and I want to sort it based on the size of the variables in the class.
using namespace std;
class Player {
string name;
int score;
int id;
size_t num; // Size of bytes obtained in the free store
char* p; // Start address of free memory
public:
string getName() { return name; }
int getScore() { return score; }
size_t getNum() { return num; }
ifstream& read(ifstream& in)
{
in.read((char*)this, sizeof(Player));
p = new char[num];
in.read((char*)p, num); // pointer p is not really used (just part of the problem)
return in;
}
friend ostream& operator<<(ostream& out, const Player& Player)
{
out << "name:" << left << setw(15) << Player.name << ", "
<< "id:" << setw(10) << Player.id << ", "
<< "score:" << Player.score << ", "
<< "num:" << Player.num;
return out;
}
};
int main()
{
std::ifstream infile("file", std::ifstream::binary);
vector<Player> v;
v.reserve(10);
for (int i = 0; i < 10; i++) {
v[i].read(infile);
}
for (int i = 0; i < 10; i++) {
cout << v[i] << endl; // before sort
}
cout << endl << endl;
// sort sentence
for (int i = 0; i < 10; i++) {
cout << v[i] << endl; // after sort
}
}
First, I tried to sort by member 'num'. I tried it in a variety of ways.
friend bool compare(Player& player1, Player& player2); // in class
};
static bool compare(Player& player1, Player& player2)
{
return player1.num < player2.num;
}
...
std::sort(v.begin(), v.end(),compare);
or
std::sort(v.begin(), v.end(),[](Player& a, Player& b) {return a.getNum() < b.getNum(); });
or
bool operator<(const Player& py)
{
return num < py.num;
}
bool operator>(const Player& py)
{
return num > py.num;
}
// in class
};
std::sort(v.begin(), v.end(), [](Player& a, Player& b) {return a < b; });
result what I wanted
before
name:jhxubuhgldh , id:476109 , score:435832437, num:969
name:pmemwohbgqwwg , id:789387 , score:257570203, num:481
name:zzirtu , id:1477302, score:172025616, num:37
name:nxkrhdujq , id:4481 , score:461657936, num:258
name:benijj , id:1867600, score:39352766, num:658
name:wvukciloi , id:734835 , score:416874932, num:605
name:fisp , id:1611284, score:194548462, num:734
name:xfjvbgocgnvgfr , id:995733 , score:181002072, num:834
name:rmg , id:911109 , score:153943559, num:415
after
name:zzirtu , id:1477302, score:172025616, num:37
name:nxkrhdujq , id:4481 , score:461657936, num:258
name:rmg , id:911109 , score:153943559, num:415
name:pmemwohbgqwwg , id:789387 , score:257570203, num:481
name:wvukciloi , id:734835 , score:416874932, num:605
name:benijj , id:1867600, score:39352766, num:658
name:fisp , id:1611284, score:194548462, num:734
name:xfjvbgocgnvgfr , id:995733 , score:181002072, num:834
name:jhxubuhgldh , id:476109 , score:435832437, num:969
However, the sort was not performed. Vector's content was same before and after sort.
What did I miss? Is there a problem with Pointer or reference?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
