今天看了一下QMap类的功能,写了个小程序测试了一下,这个程序本身没有什么难度,但刚开始的时候出现了一些问题,总是出现段错误,后来仔细看了一下还是QMapIterator的问题。下面就是我所用到的程序。
#include
#include
#include
#include
#include
using namespace std;
int main(int argc,char *argv[])
{
QCoreApplication app(argc,argv);
QMap map;
map["one"] = 1;
map["two"] = 2;
map["three"] = 3;
map.insert("onne",2);
map["aaa"] = 4;
map["bb"] = 5;
QMapIterator i(map);
while (i.hasNext())
{
i.next();
cout<
}
return app.exec();
}
上面的代码中红色部分是需要重点注意的,我刚开始就是这点出了问题,耽误了好多时间。在<<C++ GUI Programming with Qt4>>的第11章中关于Iterator的介绍时提到:
Qt provides two categories of iterators for traversing the items stored in a container: Java-style iterators and STL-style iterator....The first thing to keep in mind when using Java-style iterators is that they don't point directly at items.Instead,they can be located before the first item,after the last item,or between two items.The iterator is initialized with the container to traverse.At this point,the iterator is located just before the first item.The call to hasNext() returns true if there is an item to the right of the iterator.The next() function returns the item to the right of the iterator and advances the iterator to the next valid position.
阅读(3071) | 评论(0) | 转发(0) |