通过迭代器插入元素

inserting element through iterator c++

本文关键字:元素 插入 迭代器      更新时间:2023-10-16

我试图插入元素到集"教师"。教师是学校结构的一部分。学校的建筑在另一个叫做城镇的地方。我正设法把"格林"老师调到"布朗"担任校长的学校。我想办法找到那所学校,但没办法让他进去。定义运算符<, ==来比较校长。

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|错误:调用' std::set>::insert(std::string&) const ' |没有匹配的函数/home/michael/desktop/prog/ulohaa1/main.cpp|84|注:候选项为:|/usr/include/c + +/4.6/位/stl_set.h | 407 |注意:std::pair, _Compare, typename _Alloc::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Alloc = std:: rb_tree <_Key, _Key, std::_Identity<_Key>, _Compare, typename _Alloc::rebind<_Key>::other>::const_iterator = std::_Rb_tree_const_iterator>, std::set<_Key, _Alloc>::value_type = std::basic_string] |/usr/include/c++/4.6/bits/stl_set.h|407|注:没有已知的隐式this形参从' const std::set> '到' std::set> ' |的转换/usr/include/c + +/4.6/位/stl_set.h | 444 |注意:std:: set<_Key、_Compare _Alloc>:: iterator std:: set<_Key, _Compare, _Alloc>::插入(std:: set<_Key _Compare, _Alloc>:: const_iterator, const value_type&) [_Key = std:: basic_string _Compare = std::>, _Alloc = std::分配器>,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个警告===|

谢谢大家的帮助

编译器错误提示您没有重载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; }
};

如果您需要更新集合中的一个学校,您需要

在集合中找到学校

2)如果找到,将值保存到临时学校

3)更新临时学校

4)将学校从集合中删除,并将临时学校插入集合

这是set容器的性质。更新项意味着删除并插入已更新的项。所以基本上就是:

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