使用set<set>迭代器的编译错误<int>

compilation error using iterator of set<set<int> >

本文关键字:lt gt set 错误 int 编译 使用 迭代器      更新时间:2023-10-16

我认为我没有正确使用迭代器,但我看不出我的代码有什么问题:

#include <iostream>
#include <set>
#include <ctime>
#include <cstdlib>
#include <sstream>
int main(){
  stringstream s;
  set<int> a, b, c;
  set<set<int> > d;
  set<set<int> >::iterator pos;
  srand(time(0));
  d.insert(a);
  d.insert(b);
  d.insert(c);
  for (pos = d.begin(); pos != d.end(); pos++){
      for (int i=0;i<10;i++){
          (*pos).insert(genInt(1,10));
      }
      s << "nSet: " << endl << *pos;
  }
  s << endl;
}

我试图用随机整数初始化这三个集,而成为另一组集合。

汇编错误在(*pos).insert(genInt(1,10))中:

error: no matching function for call to ‘std::set<int>::insert(int) const’

genInt(1,10)返回1到10之间的随机整数。

int genInt(int min, int max){
int tam = max - min + 1;
return ( (rand() % tam) + min);
}

您无法修改集合的元素。错误是正确的,没有 const insert方法。

错误:呼叫'std :: set :: insert(int) const

no匹配函数

posset上的迭代器,只能提供const对其所指元素的引用。因此,只能通过该参考来调用const成员函数。std::set以有序的方式存储它的元素,使其可以快速找到它们。更改其中一个要素会不安。在将其插入更大的集合之前,您必须完全构造子集。