我正在尝试打印出集合列表,但我对语法感到困惑。我希望每组都在一个新行上。这是我的代码:
set<int> set1 = { 2, 4, 5 };
set<int> set2 = { 4, 5 };
list<set<int>> list1;
list<set<int>>::iterator it = list1.begin();
list1.insert(it, set1);
list1.insert(it, set2);
cout << "List contents:" << endl;
for (it = list1.begin(); it != list1.end(); ++it)
{
cout << *it; //error is here
}
尝试打印指向迭代器的指针时出现错误。很确定是因为我在列表内部使用了一个集合,但我不知道输出此列表的正确语法。
最佳答案
是否要打印如下?
for (it = list1.begin(); it != list1.end(); ++it)
{
for (set<int>::iterator s = it->begin(); s != it->end(); s++) {
cout << *s << ' ';
}
cout << endl;
}
输出:
List contents:
2 4 5
4 5
关于C++ 打印集合列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44793867/