迭代器删除自己
如果是 java, 迭代器删除自己, 只要简单的 iter.remove;
list<int> l; l.push_back(1); l.push_back(3); l.push_back(4); std::list<int>::iterator iter; std::list<int>::iterator tmp_iter; for (iter = l.begin(); iter != l.end(); iter++) { if (*iter == 3) { tmp_iter = iter; iter++; l.erase(tmp_iter); continue; } } for (iter = l.begin(); iter != l.end(); iter++) { cout << *iter << endl; } // 1 4
时间: 2024-10-11 23:46:39