| Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date | |
| All threads index page 6 | |
hi, all
when invoking QList::erase, it should return an iterator to the next item in the list (which may be end()) as document said, but when I run the following code, I don't get the correct result:
QList<QString> strList;
strList << "aa" << "bb" << "cc" << "dd";
QList<QString>::iterator iter = strList.begin();
QList<QString>::iterator iter_end = strList.end();
while (iter != iter_end)
{
printf("value %s\n", (*iter).toLocal8Bit().data());
if ( (*iter) == "dd" )
iter = strList.erase(iter);
else
iter++;
}
it crashes, when I look at the iterator value, I find the iter value doesn't change when I run over iter = strList.erase(iter); even more, if you change (*iter) == "dd" to (*iter) == "cc", you will find "value dd" ouput twice. but if I use the std::list, everything is fine.
Reimand
reimand zhu schrieb:
> hi, all
> when invoking QList::erase, it should return an iterator to the next
> item in the list (which may be end()) as document said, but when I run
> the following code, I don't get the correct result:
> QList<QString> strList;
> strList << "aa" << "bb" << "cc" << "dd";
> QList<QString>::iterator iter = strList.begin();
> QList<QString>::iterator iter_end = strList.end();
> while (iter != iter_end)
> {
> printf("value %s\n", (*iter).toLocal8Bit().data());
> if ( (*iter) == "dd" )
> iter = strListerase(iter);
> else
> iter++;
> }
> it crashes, when I look at the iterator value, I find the iter value
> doesn't change when I run over iter = strList.erase(iter); even more, if
> you change (*iter) == "dd" to (*iter) == "cc", you will find "value dd"
> ouput twice. but if I use the std::list, everything is fine.
>From the documentation of QList::iterator:
"Multiple iterators can be used on the same list. However, be aware that
any non-const function call performed on the QList will render all
existing iterators undefined. If you need to keep iterators over a long
period of time, we recommend that you use QLinkedList rather than QList."
Best Regards / Mit freundlichen Grü?en
Rainer Wiesenfarth
--
[ signature omitted ]
Attachment:
smime.p7s
Description: S/MIME Cryptographic Signature
reimand zhu wrote:
> hi, all
> when invoking QList::erase, it should return an iterator to the next
> item in the list (which may be end()) as document said, but when I run
> the following code, I don't get the correct result:
> QList<QString> strList;
> strList << "aa" << "bb" << "cc" << "dd";
> QList<QString>::iterator iter = strList.begin();
> QList<QString>::iterator iter_end = strList.end();
> while (iter != iter_end)
> {
> printf("value %s\n", (*iter).toLocal8Bit().data());
> if ( (*iter) == "dd" )
> iter = strListerase(iter);
> else
> iter++;
> }
> it crashes, when I look at the iterator value, I find the iter value
> doesn't change when I run over iter = strList.erase(iter); even more, if
> you change (*iter) == "dd" to (*iter) == "cc", you will find "value dd"
> ouput twice. but if I use the std::list, everything is fine.
>
> Reimand
When you erase an item, iterators pointing to later entries in the list
become invalid. If you want to erase more than one item from a list, you
should start at the end of the list and work forward.
>
>
>
>
>
>
>
>
>
> ------------------------------------------------------------------------
> 想加入吗?1.9亿用户正在使用网易邮箱 www.126.com <http://www.126.com/>
--
[ signature omitted ]