2014-03-27 52 views
-3

我试图插入元素设置“教师”。教师是结构,学校的一部分。学校结构在另一个叫做城镇的地方。我试图把老师“绿色”放到学校里,“布朗”是校长。我用find来找到学校,但我不能让他进来。运营商<,==被定义为比较校长。通过迭代器插入元素C++

bool operator<(const School& l, const School& r){ 
    return (l.headmaster) < (r.headmaster); 
} 
bool operator==(const School& l, const School& r){ 
    return (l.headmaster) == (r.headmaster); 
} 

struct School { 
    string headmaster; 
    set <string> teachers; 
}; 

set<School>::iterator it; 
set <School> town; 
// now I alocated few schools and insert them into town, 
School *pSchool = new School(): // i will use pSchool to find school with brown as headmaster 
pSchool > headmaster = "Brown"; // 
it = rozvrh.find(*pSchool); 
cout << it->headmaster // gives Brown 
it->teachers.insert("Green"); /// error 

编辑..错误

|| === ulohaa1,调试=== | /home/ulohaa1/main.cpp||在函数'布尔变换(常量字符*,常量字符*)':| /home/ulohaa1/main.cpp|84|错误:没有匹配函数调用'std :: set> :: insert(std :: string &)const'| /home/michal/Desktop/prog/ulohaa1/main.cpp|84|note:考生是:| /usr/include/c++/4.6/bits/stl_set.h|407|note:std :: pair,_Compare,typename _Alloc :: rebind < _Key> :: other> :: const_iterator,bool> std :: set < _Key,_Compare,_Alloc> :: insert(const value_type &)[with _Key = std :: basic_string,_Compare = std :: less>,_Alloc = std :: allocator>,typename std :: _ Rb_tree < _Key,_Key,std :: _ Identity < _Key>,_Compare,typename _Alloc :: rebind < _Key> :: other> :: const_iterator = std :: _ Rb_tree_const_iterator>,std :: set < _Key,_Compare,_Alloc> :: value_type = std :: basic_string ] | /usr/include/c++/4.6/bits/stl_set.h|407|note:对于隐式'this'参数从'const std :: set>'到'std :: set>'|没有已知的转换。 /usr/include/c++/4.6/bits/stl_set.h|444|note:std :: set < _Key,_Compare,_Alloc> :: iterator std :: set < _Key,_Compare,_Alloc> :: insert(std :: set < _Key,_Compare,_Alloc> :: const_iterator,const value_type )[with _Key = std :: basic_string,_Compare = std :: less>,_Alloc = std :: allocator>,std :: set < _Key, _Compare,_Alloc> :: iterator = std :: _ Rb_tree_const_iterator>,std :: set < _Key,_Compare,_Alloc> :: const_iterator = std :: _ Rb_tree_const_iterator>,std :: set < _Key,_Compare,_Alloc> :: value_type =性病:: basic_string的] | /usr/include/c++/4.6/bits/stl_set.h|444|note:候选人需要2个参数,1个提供| /usr/include/c++/4.6/bits/stl_set.h|464|note:template void std :: set :: insert(_InputIterator,_InputIterator)[with _InputIterator = _InputIterator,_Key = std :: basic_string,_Compare = std :: less>,_Alloc = std :: allocator>] | || ===构建完成:7个错误,0个警告=== |

THX您的帮助球员

+0

C++是大小写敏感的,因此线{学校* pSchool =新学校():}不应该编译(更不要说冒号在行尾) – Andy

+0

@安迪,除非'学校'是秘密地继承'学校',但从OP的问题来看,这不太可能 – yizzlez

+0

@awesomeyi同意。 OP可以澄清, – Andy

回答

0

你有编译器错误提示你没有重载operator <为您的学校类型:

#include <set> 
#include <string> 

struct School 
{ 
    std::string headmaster; 
    std::set<std::string> teachers; 
    bool operator<(const School& s) const { return headmaster < s.headmaster; } 
}; 

如果你需要更新你所需要的设定一所学校到

1)查找集合

2)如果发现该学校,该值保存到临时学校

3)更新的临时学校

4)删除自定校,并插入临时学校设定的

这是一组容器的性质。更新项目意味着使用更新的项目进行移除和插入。所以基本上这样:

// we will search for Mr Brown's school and add a teacher 
School searchSchool; 
searchSchool.headmaster = "Brown"; 
std::set<School>::iterator it = mainSchool.find(searchSchool); 

// if found, add the teacher 
if (it != mainSchool.end()) 
{ 
    // save the school 
    School theSchool = *it; 
    theSchool.teachers.insert("A new teacher"); 
    mainSchool.erase(it); 
    mainSchool.insert(theSchool); 
} 

mainSchool是你的“全球”组。

编辑:如果使用图(我认为是上级),代码可以是如此简单:

#include <map> 
#include <set> 
#include <string> 
typedef std::set<std::string> StringSet; 
typedef std::map<std::string, StringSet> SchoolMap; 

using namespace std; 
int main() 
{ 
    SchoolMap allSchools; 
    allSchools.insert(make_pair("Brown", StringSet())); 
    allSchools.insert(make_pair("Smith", StringSet())); 

    // search for Mr Brown's school 
    SchoolMap::iterator it = allSchools.find("Brown"); 

    // if found, add the teacher 
    if (it != allSchools.end()) 
    // save the teacher 
     it->second.insert("A new teacher"); 
} 
+0

仍然是相同的错误 – user2109307

+0

你需要认识到的另一件事是,要改变一个集合中的一个值,你需要删除旧的值并用更新的值替换它。看到我更新的答案。 – PaulMcKenzie

+0

,如果我改变结构学校只有一个设置techers指针?指针会保持不变,如果我给它的元素。 – user2109307