C++ 打印集合列表

标签 c++ list iterator set

我正在尝试打印出集合列表,但我对语法感到困惑。我希望每组都在一个新行上。这是我的代码:

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/

相关文章:

c++ - C++中如何判断arg类型

c++ - 仅用 10 万个存储单元对 100 万个数字进行排序

mysql - 如何存储本质上是与 SQL 中的唯一值相关联的列表的数据

c++ - 如果底层容器发生变化,迭代器会发生什么变化?

java - 哪个性能更好: for each or iterator in LinkedList?

c++ - objective-c ++ 运算符删除

c++ - 将 cv::Mat 传递给 labview

c# - 拆分字符串并附加到没有最后一个元素的新字符串,直到字符串列表为空

python - 查找列表中的特定值

c++ - 如何定义双括号/双迭代器运算符,类似于 Vector of Vectors'?