【发布时间】:2017-11-23 22:09:54
【问题描述】:
我认为我没有正确使用迭代器,但我看不出我的代码有什么问题:
#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);
}
【问题讨论】: