按私有成员对自定义对象进行排序
Sorting custom object by private members
我找到了很多关于排序函数的解释,但我无法用我的代码实现一个。
我有这样的结构:
class Out{
public:
const std::map<std::string,In>& getListIn()const;
private:
std::map<std::string,In> listIn;
class In{
public:
const float& getScore();
private :
float score;
};
};
我想按分数(从最大值到最小值)对 listIn
进行排序。我试图重载 operator>
或创建我自己的函数:
std::map<std::string,Out::In> notVisited = listIn;
bool Out::In::compareCostScore (const std::pair<std::string,Out::In>& v1,const std::pair<std::string,Out::In>& v2){
return (v1.second.getCostScore() > v2.second.getCostScore());
}
std::sort(notVisited.begin(), notVisited.end(), Out::In::compareCostScore());
但功能未知。
或者:
std::sort(notVisited.begin(), notVisited.end(),[] (const std::map<std::string,Out::In>& v1, const std::map<std::string,Out::In>& v2) {return (v1.second.getCostScore() < v2.second.getCostScore()};)
我在类型兼容性或隐私方面遇到了一些问题。也许那是因为我试图从 class...
谢谢
编辑:
我成功了 \o/ :
bool operator > (const In& v) const{
return (score > v.score);
}
std::vector<Out::In> notVisited;
for( std::map<std::string,Out::In>::iterator it = listIn.begin(); it != listIn.end(); ++it ) {
notVisited.push_back( it->second );
}
std::sort(notVisited.begin(), notVisited.end(), std::greater<Out::In>());
感谢您对地图的解释
因为你的内部 class 只有一个字段并且它有一个 getter (你可能想要使这个 getter const const float& getScore() const
)你可以简单地改变 Out::in
声明到 public
并解决问题。但是如果它是一个摘录并且在 Out::in
之后有更多的逻辑你想从 public 访问中隐藏那么你可以将你的比较器定义为这样的友元函数:
class Out{
/* ... */
public:
friend bool Out::In::compareCostScore (const std::pair<std::string,Out::In>& v1,const std::pair<std::string,Out::In>& v2);
}
我找到了很多关于排序函数的解释,但我无法用我的代码实现一个。
我有这样的结构:
class Out{
public:
const std::map<std::string,In>& getListIn()const;
private:
std::map<std::string,In> listIn;
class In{
public:
const float& getScore();
private :
float score;
};
};
我想按分数(从最大值到最小值)对 listIn
进行排序。我试图重载 operator>
或创建我自己的函数:
std::map<std::string,Out::In> notVisited = listIn;
bool Out::In::compareCostScore (const std::pair<std::string,Out::In>& v1,const std::pair<std::string,Out::In>& v2){
return (v1.second.getCostScore() > v2.second.getCostScore());
}
std::sort(notVisited.begin(), notVisited.end(), Out::In::compareCostScore());
但功能未知。 或者:
std::sort(notVisited.begin(), notVisited.end(),[] (const std::map<std::string,Out::In>& v1, const std::map<std::string,Out::In>& v2) {return (v1.second.getCostScore() < v2.second.getCostScore()};)
我在类型兼容性或隐私方面遇到了一些问题。也许那是因为我试图从 class... 谢谢
编辑: 我成功了 \o/ :
bool operator > (const In& v) const{
return (score > v.score);
}
std::vector<Out::In> notVisited;
for( std::map<std::string,Out::In>::iterator it = listIn.begin(); it != listIn.end(); ++it ) {
notVisited.push_back( it->second );
}
std::sort(notVisited.begin(), notVisited.end(), std::greater<Out::In>());
感谢您对地图的解释
因为你的内部 class 只有一个字段并且它有一个 getter (你可能想要使这个 getter const const float& getScore() const
)你可以简单地改变 Out::in
声明到 public
并解决问题。但是如果它是一个摘录并且在 Out::in
之后有更多的逻辑你想从 public 访问中隐藏那么你可以将你的比较器定义为这样的友元函数:
class Out{
/* ... */
public:
friend bool Out::In::compareCostScore (const std::pair<std::string,Out::In>& v1,const std::pair<std::string,Out::In>& v2);
}