【问题标题】:inserting element through iterator c++通过迭代器c ++插入元素
【发布时间】:2014-03-27 01:16:28
【问题描述】:

我正在尝试将元素插入到集合“教师”中。教师是设置的,这是学校结构的一部分。学校结构在另一个叫做城镇的集合内。我正试图将“格林”老师送入“布朗”担任校长的学校。我用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,调试 ===| /home/ulohaa1/main.cpp||在函数'bool transform(const char*, const char*)'中:| /home/ulohaa1/main.cpp|84|错误:没有匹配函数调用‘std::set >::insert(std::string&) const’| /home/michal/Desktop/prog/ulohaa1/main.cpp|84|注意:候选人是:| /usr/include/c++/4.6/bits/stl_set.h|407|注意: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>, _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|注意: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|注意:模板 void std::set::insert(_InputIterator, _InputIterator) [with _InputIterator = _InputIterator, _Key = std::basic_string, _Compare = std: :less >, _Alloc = std::allocator >]| ||=== 构建完成:7 个错误,0 个警告 ===|

谢谢你们的帮助

【问题讨论】:

  • C++ 区分大小写,因此 {School *pSchool = new school():} 行不应该编译(更不用说行尾的冒号了。
  • @Andy,除非school 是从School 秘密继承的,但从OP 的问题来看,这不太可能
  • @awesomeyi 同意。也许OP可以澄清。
  • 你怎么会有一套?仅此一项就无法编译,因为 School 定义的顺序不少于。更好的是,发布一个真实的、可编译的、但很小的程序。匆忙发布代码并让其他人尝试找出所有的拼写错误并没有帮助。
  • /// error 到底是什么意思??

标签: c++ insert set structure


【解决方案1】:

您的编译器错误表明您没有为您的 School 类型重载运算符

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

【讨论】:

  • 您需要意识到的另一件事是,要更改集合中的值,您需要删除旧值并用更新后的值替换它。请参阅我的更新答案。
  • 如果我将 struct school 更改为只有一个指向 set techers 的指针?如果我将元素添加到指针中,指针将保持不变。
  • 不要在集合中使用指针。也许您应该使用地图,而不是一组。关键是校长,价值观可以是老师的集合。然后可以在不擦除整个键的情况下更改地图的项目。
  • 嗯。我害怕在地图查找功能上浪费时间。(我刚刚意识到它具有相似的查找速度..)好的地图听起来不错
  • 查看我关于使用地图的更新答案。与集合一样,地图具有对数查找时间。所以速度没有放缓。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-13
  • 2019-09-23
  • 2011-09-07
相关资源
最近更新 更多