QMap和std::map的遍历

两种遍历方式都使用迭代器:

1QMap使用Iterator.key(),和Iterator.value()方法获取第一个或第二个元素的值。

而std::map使用Iterator->first(), Iterator->second()来获取第一个或第二个元素的值。

 

QMap  m_RatioCfg;    

QMap::Iterator  it;

for(it = m.begin();it != m.end();++it)

{

      cout<<"key:"<

    //pText->append("key="+it.key()+"value="+it.value());   //在文本中显示键值

}

 

#include
#include
#include
using namespace std;


int main()
{
    map m;
    m[1]= new string("1111111111111111");
    m[2]= new string("2222222222222222");
    m[3]= new string("3333333333333333");
    m[4]= new string("4444444444444444");
    m[0]= new string("5555555555555555");
    map::iterator it;
    for(it=m.begin();it!=m.end();)
    {
        cout<<"key: "<first <<" value: "<second<         delete it->second;
        m.erase(it++);
    }
    return   0;
}

注意删除操作

QMap的删除

QMap<intint>::iterator it;

   QMap<intint>::iterator ait;

   for (it = mapIntToInt.begin();it != mapIntToInt.end(); )

   {

      int num = it.key();

      qDebug() << "thecurrent number is " << num;

      if (num % 2 == 0)

      {

         mapIntToInt.erase(it);

         qDebug() << "erasenumber : " << num;

      }

      else

      {

         it++;

      }

   }

 

std::map的删除

  1. std::map::iterator it = mapTest.begin();  
  2. while(it != mapTest.end())  
  3. {  
  4.          if(TestVal(it->second))  
  5.          {  
  6.                 mapTest.erase(it++);  
  7.          }  
  8.          else  
  9.                  it++;  
  10. }

 

 

 

 

你可能感兴趣的:(Qt,C++)