2014-03-27 129 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