通过迭代器插入元素
inserting element through iterator 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, 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");
}
- 将二维数组的所有元素插入到一维数组中
- 如何将元素插入和迭代到这种映射.C++
- 无法在构造函数中执行设置元素插入
- 将数组的元素插入映射的时间复杂度是多少?
- 如何将元素插入到标准::地图的共享指针中?
- 如何将 n 个连续元素插入到元素类型不可复制的 std::vector 中?
- 将元素插入链接列表
- 创建一个函数以在给定位置将元素插入到列表中
- 我可以在一行代码中将向量中与条件匹配的所有元素插入到集合中吗?
- 为什么我不能将元素插入数组中的列表?
- 将多个偏移量的多个元素插入到一个矢量中
- 将元素插入排序数组
- 使用算法中的插入函数将元素插入空容器中,未给出预期的结果
- 使用动态分配创建数组并将元素插入其中
- 将元素插入字符串集 c++ 的向量中
- 如何将元素插入独特指针的多维矢量中
- 通过引用同一向量的元素插入到向量中
- 如何将元素插入 BST
- 如何将元素插入矢量的开头
- STL和元素插入中列表的动态分配