这个循环能不能遍历迭代器里面的每个元素?该如何解决
这个循环能不能遍历迭代器里面的每个元素?
我怎么觉得v在插入元素之后,first是新插入的元素的迭代器,
++first只是重新指向了第一个元素?????
------解决方案--------------------
死循环。
v.insert(first, 42)是在迭代器所指对象的前面插入,并返回指向插入后的对象的迭代器,因此每次循环结束first指向的对象都没有改变。
------解决方案--------------------
恩,死循环!
iterator insert(iterator it, const T& x = T());
Each of the member functions inserts, before the element pointed to by it in the controlled sequence, a sequence specified by the remaining operands. The first member function inserts a single element with value x and returns an iterator that points to the newly inserted element
------解决方案--------------------
肯定死循环啦
- C/C++ code
vector<int> v(10); // 假设v里面包含一些原始元素 vector<int>::iterator first = v.begin(); while(first != v.end()) { // do some processing first = v.insert(first, 42); ++first; }
我怎么觉得v在插入元素之后,first是新插入的元素的迭代器,
++first只是重新指向了第一个元素?????
------解决方案--------------------
死循环。
v.insert(first, 42)是在迭代器所指对象的前面插入,并返回指向插入后的对象的迭代器,因此每次循环结束first指向的对象都没有改变。
------解决方案--------------------
恩,死循环!
iterator insert(iterator it, const T& x = T());
Each of the member functions inserts, before the element pointed to by it in the controlled sequence, a sequence specified by the remaining operands. The first member function inserts a single element with value x and returns an iterator that points to the newly inserted element
------解决方案--------------------
肯定死循环啦