Chinaunix首页 | 论坛 | 博客
  • 博客访问: 520423
  • 博文数量: 174
  • 博客积分: 4177
  • 博客等级: 上校
  • 技术积分: 1827
  • 用 户 组: 普通用户
  • 注册时间: 2007-10-15 14:12
文章分类

全部博文(174)

文章存档

2018年(1)

2017年(1)

2013年(3)

2012年(9)

2010年(12)

2009年(5)

2008年(106)

2007年(37)

我的朋友

分类: C/C++

2008-07-18 14:48:02

   今天看了一下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.
阅读(3040) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~