C++ 在C+;上的std::set中查找并更新自定义对象+;
我在我的项目上创建了两个自定义结构,每个结构都有一个std::setC++ 在C+;上的std::set中查找并更新自定义对象+;,c++,c++11,C++,C++11,我在我的项目上创建了两个自定义结构,每个结构都有一个std::set struct Subject { std::string name; std::set<SubjectFrame> frames; Subject(std::string subject_name); void AddFrame(SubjectFrame &frame); bool operator<(const S
struct Subject {
std::string name;
std::set<SubjectFrame> frames;
Subject(std::string subject_name);
void AddFrame(SubjectFrame &frame);
bool operator<(const Subject &rhs) const { return (name < rhs.name);}
bool operator==(const Subject &rhs) const { return (name == rhs.name);}
};
struct Dataset {
std::set<Subject> subjects;
std::map<int, std::vector<Subject> > classification_groups;
Dataset(const std::string ds_path);
void AddSubject(Subject &subject);
void GetDSFromFileSystem(const std::string dataset_path);
void GetClassificationGroups(int number_of_groups_to_create);
};
struct主题{
std::字符串名;
std::设置帧;
主题(std::字符串主题名称);
void AddFrame(SubjectFrame和frame);
布尔运算符subjects.find(subject);
如果(it!=this->subjects.end()){
对于(主题fr:本->主题){
它->添加帧(fr);
}
}否则此->主题。插入(主题);
}
调用此函数的:
void Subject::AddFrame(SubjectFrame &frame) {
set<SubjectFrame>::iterator it = this->frames.find(frame);
if (it != this->frames.end()) {
if (frame.l_eye_centroid.x != 0) {
it->r_eye_centroid = frame.r_eye_centroid;
it->l_eye_centroid = frame.l_eye_centroid;
}
if (frame.path != "") it->path = frame.path;
else return;
}
else this->frames.insert(frame);
}
void Subject::AddFrame(SubjectFrame&frame){
set::iterator it=this->frames.find(frame);
如果(it!=this->frames.end()){
if(frame.l\u eye\u centroid.x!=0){
it->r\u eye\u centroid=frame.r\u eye\u centroid;
it->l_-eye\u-centroid=frame.l_-eye\u-centroid;
}
如果(frame.path!=“it->path=frame.path;
否则返回;
}
否则此->框架。插入(框架);
}
因此,add操作背后的逻辑是:我传递一个对象,并检查std::set中是否已经存在具有该名称的对象。如果是,我用参数对象拥有的信息和已经注册的对象没有的信息更新现有对象,如果不是,我插入对象
当我试图编译我的程序时,我遇到以下错误:
错误:没有可行的重载“=”
it->r\u eye\u centroid=frame.r\u eye\u centroid
错误:没有可行的重载“=”
it->l_-eye\u-centroid=frame.l_-eye\u-centroid
错误:没有可行的重载“=”
如果(frame.path!=“it->path=frame.path
错误:成员函数“AddFrame”不可行:“this”参数的类型为
“const Subject”,但函数未标记为const
它->添加帧(fr)
有人知道是什么导致了这些问题,以及我如何解决这些问题吗?对于std::set,迭代器-s是const,这是因为它们被用作
集合
中的键,不应该对其进行修改以避免严格弱顺序中的任何不一致
解决此问题的一种方法是使要修改的字段可更改,但要确保它未用于
集的排序。对于std::set,迭代器-s是常量
,这是因为它们被用作集合
中的键,不应修改该集合以避免严格弱排序中的任何不一致
解决此问题的一种方法是将要修改的字段设置为可变的,但请确保它不用于集合
排序。您可能需要为自定义类定义一个运算符=
或一个复制构造函数,或两者都使用。std::set
不允许您更改元素,因为这可能会扰乱排序。您需要删除、修改并重新插入它。您可能需要为自定义类定义一个运算符=
或一个复制构造函数,或两者都定义。std::set
不允许您更改元素,因为这会扰乱排序。您需要删除、修改并重新插入它<代码>迭代器
std::set的类型仅在c++11之后才创建了const
@callyalater,这就是问题的标记方式。这是真的。我只是指出了这一点,如果OP想要确保向后兼容性,他可以使用const_迭代器
代替.TY作为您的答案@marcinj。但这样做并没有解决问题:/I尝试使用:const set::iterator it和const set::iterator it;错误是一样的。我做得对吗?PS:将atributes定义为可变的我解决了前3个错误,但即使我将atributes设置为可变的,最后一个错误仍然会继续。您还需要在迭代器const上调用您的函数。iterator
type forstd::set
仅在c++11之后生成const
@callyalater,这就是问题的标记方式。这是真的。我只是指出了这一点,如果OP想要确保向后兼容性,他可以使用const_迭代器
代替.TY作为您的答案@marcinj。但这样做并没有解决问题:/I尝试使用:const set::iterator it和const set::iterator it;错误是一样的。我做得对吗?PS:将atributes定义为可变的我解决了前3个错误,但即使我将atributes设置为可变的,最后一个错误仍然会继续。您还需要在迭代器const上调用您的函数。
void Subject::AddFrame(SubjectFrame &frame) {
set<SubjectFrame>::iterator it = this->frames.find(frame);
if (it != this->frames.end()) {
if (frame.l_eye_centroid.x != 0) {
it->r_eye_centroid = frame.r_eye_centroid;
it->l_eye_centroid = frame.l_eye_centroid;
}
if (frame.path != "") it->path = frame.path;
else return;
}
else this->frames.insert(frame);
}