填充c ++在循环范围内映射内容

rnd*_*gen 3 c++ scope map set

我尝试在循环范围内填充c ++映射的内容.

#include <set>
#include <map>

map<int, set<int> > maps;

for (int i=0; i<10; i++) {
   set<int> seti;    // content: a set of integers
   seti.insert(i);
   seti.insert(...);
   maps.insert ( pair<int,set<int> >(i,seti) );
}
Run Code Online (Sandbox Code Playgroud)

问题是:maps.insert是否复制了这对内容?如果在每个循环范围之后对实例无效,则此类代码应该失败.

我应该如何正确生成地图内容(使用指针和新实例?)以及如何正确地清理地图?

感谢您对最佳实践的任何建议.

---更新---

map<int, set<int> >::iterator it;
int k      = (*it).first;     
set<int> v = (*it).second;       
Run Code Online (Sandbox Code Playgroud)

现在'v'也是从地图中存储的真实实例复制的一个?
如果是的话,我无法"直接"更新地图内容.

bit*_*ask 6

您不必显式创建set对象,因为当您使用[]运算符访问它时,它将在地图中创建.因此,你可以简单地写

#include <set>
#include <map>

map<int, set<int> > maps;

for (int i=0; i<10; i++) {
   maps[i].insert(i);
}
Run Code Online (Sandbox Code Playgroud)