[Qt-interest] QMap container issue

Mandeep Sandhu mandeepsandhu.chd at gmail.com
Sat Jul 23 20:14:53 CEST 2011


On Sat, Jul 23, 2011 at 6:35 PM, Sajjad <dosto.walla at gmail.com> wrote:

> Hello forum,
>
> I am trying to implement a graph data-structure using QMap<QString,int> and
> QVector<Node*> .
>
> QMap<> contains the node name and index of the QVector so that i can
> accesss the Node* directly.
>
> And i have the following function to retrieve the index for the vector to
> retrieve the node
>
>
> //////////////////////////////////////////////////////////////////////////
>
>
>   int SceneHierarchy::getNodeIndex(const QString &nodeName) const
>
> {
>
>     int pos = -1;
>
>      //find the node in the map with the key - node name
>
>     QMap<QString,int>::const_iterator iter = m_nodeMap.constFind(nodeName);
>
>
>      if(iter == m_nodeMap.constEnd())
>
>     {
>
>         //node name is not found in the map
>
>         pos = -1;
>
>     }
>
>     else
>
>     {
>
>         //!node found inside the map and so we
>
>         //!return the index of the node in the vector list
>
>         pos = iter.value();
>
>     }
>
>      return pos;
>
> }
>
>
>
>
> ////////////////////////////////////////////////////////////////////////////
>
>
> The program crashes at the following line
>
>     QMap<QString,int>::const_iterator iter = m_nodeMap.constFind(nodeName);
>
>
>
> I do not get what could be the possible problem with the above code. If the name is not found it will simply returns constEnd(), right?
>
>
> I must be missing something. Anyone in the forum help to point it out?
>
>
Are your node names unique? If so why not simply do:

int SceneHierarchy::getNodeIndex(const QString &nodeName) const

{

 return m_nodeMap.value(nodeName, -1);
}


Why do you need an iterator to find an item in the map?


HTH,

-mandeep




>
> Thanks
>
> Sajjad
>
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at qt.nokia.com
> http://lists.qt.nokia.com/mailman/listinfo/qt-interest
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110723/b78e178e/attachment.html 


More information about the Qt-interest-old mailing list