首页 文章

通过迭代器插入元素c

提问于
浏览
-3

我试图将元素插入到“老师”中 . 教师是结构,学校的一部分 . 学校结构在另一个叫做城镇的地方 . 我想把老师“绿色”放到学校,“布朗”是校长 . 我用find找到了学校,但我无法让他进去 . 运营商<,==被定义为比较校长 .

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,Debug === | /home/ulohaa1/main.cpp||在函数'bool transform(const char *,const char *)'中: /home/ulohaa1/main.cpp|84|error:没有匹配函数来调用'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注:从'const std :: set>'到'std :: set>'的隐式'this'参数没有已知的转换/ 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 = std: :basic_string的] | / usr / include / c /4.6/bits/stl_set.h|444注:候选人需要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个警告=== |

谢谢你的帮助

1 回答

  • 0

    您的编译器错误表明您没有为您的School类型重载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是您的“全球”集 .

    编辑:如果使用 Map (我认为是优越的),代码可以像这样简单:

    #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");
    }
    

相关问题