在使用 std::sort() 和 lambda 函数按属性对 ADT 的向量进行排序时遇到问题



我正在尝试按类中的不同属性对类的向量进行多次排序。

我有一个类"命中框",它有一个类"字符"的向量作为数据成员。 字符具有字符串 name_、字符串 type_、int length_ 和 int width_ 属性。 我的目标是能够按 Character 中的每个属性对 Hitbox 类中的这个向量进行排序。 为此,我使用 std::sort(( 函数和一个 lambda 函数进行比较,如下所示,以便按字符串type_排序:

std::sort(vec.begin(), vec.end(),
[](const Character &a, const Character &b) { return a.type_ < b.type_; });

但是,这根本不是对我的向量进行排序。 根据我在其他帖子上读到的所有内容,这应该有效。 谁能告诉我我做错了什么或纠正我对 std::sort(( 函数或 lambda 函数的任何误解?

class Character {
public:
Character() : name_(""), type_(""), length_(0), width_(0) {}
Character(std::string name,std::string type,int length, int width) : name_(name), type_(type), length_(length), width_(width) {}
~Character() {}
const std::string& getName() { return name_; }
const std::string& getType() { return type_; }
const int& getLength() { return length_; }
const int& getWidth() { return width_; }
friend std::ostream& operator<<(std::ostream& os, Character e) {
os << e.name_ << "t" << e.type_ << "t" << e.length_ << "t" << e.width_;
return os;
}
int getHitboxSize() { return (length_ * width_); }
friend class Hitboxes;
std::string name_;
std::string type_;
int length_;
int width_;
};
#include <fstream>
#include <algorithm>
#include <vector>
#include "Character.h"
#ifndef Hitboxes_h
#define Hitboxes_h
class Hitboxes {
public:
Hitboxes( std::string fileName ) {
std::string name = "", type = "";
int length = 0, width = 0;
Character* e = NULL;
std::fstream iFile(fileName);
if( iFile.is_open() ) {
std::cout << "Initializing class from file...nn";
while( iFile >> name >> type >> length >> width ) {
std::cout << name << " " << type << " " << length << " " << width << std::endl;
e = new Character(name,type,length,width);
vec.push_back(*e);                       //
}
iFile.close();
std::cout << "nInitialization finished!nn";
} else {
std::cout << "Couldn't open the file: " << fileName << std::endl;
}
}
~Hitboxes() {}
std::string smallestType() {
std::sort(vec.begin(), vec.end(),
[](const Character &a, const Character &b) { return a.type_ < b.type_; });
return std::to_string(vec[0].getHitboxSize());
}
void print() {
for(Character e : vec)
std::cout << e << std::endl;
}
private:
size_t size;
std::vector<Character> vec;
};

#endif /* Hitboxes_h */
int main(int argc, const char * argv[]) {
Hitboxes hb("myFile.txt");
hb.smallestType();
hb.print();
return 0;
}

我没有收到任何错误,但目标是以不同的方式对向量进行排序。 例如,按属性排序type_将具有如下输出:

Caustic Defense 13 5
Gibraltar Defense 10 8
Lifeline Defense 8 5
Bloodhound Scout 8 5
Pathfinder Scout 10 6
Bangalore Soldier 11 4
Mirage Soldier 11 4
Wraith Soldier 8 4

但相反,我得到的输出是:

Bloodhound Scout 8 5
Pathfinder Scout 10 6
Lifeline Defense 8 5
Gibraltar Defense 10 8
Mirage Soldier 11 4
Bangalore Soldier 11 4
Wraith Soldier 8 4
Caustic Defense 13 5

欢迎对我的代码提出任何其他不相关的建设性批评。

我用你的输入排列运行了你的代码。它确实给出了正确的排序结果:

Caustic Defense 13      5
Gibraltar       Defense 10      8
Lifeline        Defense 8       5
Bloodhound      Scout   8       5
Pathfinder      Scout   10      6
Bangalore       Soldier 11      4
Mirage  Soldier 11      4
Wraith  Soldier 8       4

最新更新