我是靠谱客的博主 聪慧煎饼,这篇文章主要介绍java 迭代器增加元素_通过迭代器插入元素c,现在分享给大家,希望可以做个参考。

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

#include

#include

struct School

{

std::string headmaster;

std::set<:string> teachers;

bool operator

};

如果您需要更新您需要的学校中的学校

1)找到集合中的学校

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

3)更新临时学校

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

这就是集合容器的本质 . 更新项目意味着删除并插入更新的项目 . 基本上这个:

// we will search for Mr Brown's school and add a teacher

School searchSchool;

searchSchool.headmaster = "Brown";

std::set::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

#include

#include

typedef std::set<:string> StringSet;

typedef std::map<: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");

}

最后

以上就是聪慧煎饼最近收集整理的关于java 迭代器增加元素_通过迭代器插入元素c的全部内容,更多相关java内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(126)

评论列表共有 0 条评论

立即
投稿
返回
顶部