大佬教程收集整理的这篇文章主要介绍了c – 在插入新元素时,我们如何迭代集合的所有元素?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
// set_iterator.cpp : Defines the entry point for the console application. #include "stdafx.h" #include <iostream> #include <set> using namespace std; int _tmain(int argc,_TCHAR* argv[]) { set<int> a1; set<int> a2; a1.insert(3); a1.insert(4); a1.insert(5); a2.insert(1); a2.insert(2); a2.insert(6); set<int>::iterator iter; int x = 0; for (iter = a1.begin(); iter != a1.end(); ++iter) { if (x == 0) { x = 1; a1.insert(a2.begin(),a2.end()); } cout << *iter << endl; } system("pause"); return 0; }
目标是访问集合中的每个元素一次.我认为在将元素插入a1后,迭代器无效.
输出是
3 4 5 6
1,2不打印.
我们如何编码这样的情况.
以上是大佬教程为你收集整理的c – 在插入新元素时,我们如何迭代集合的所有元素?全部内容,希望文章能够帮你解决c – 在插入新元素时,我们如何迭代集合的所有元素?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。