插入std :: map时出现奇怪的结果

时间:2016-03-13 03:21:26

标签: c++ stl stdmap

我试图在QtCreator中调试我的代码,当我将insert()std::map一起使用时,我注意到我得到了奇怪的数字。插入键/值对于前5-6次插入工作正常,然后突然插入第6个值时,它会将所有键和值更改为类似于(1,1)的所有键或者重复模式,示例(1,1)然后(1,7)然后再(1,1)再次等等。 我要做的是从.obj文件中导入的数据中列出我的网格边缘(现在只是一个立方体)。

我的容器类型为std::map< std::pair< unsigned int, unsigned int >, short >

std::map< std::pair< unsigned int, unsigned int >, short >::iterator it;
unsigned int v1, v2, v3;
unsigned int min, max;

// Loop through the faces of the mesh
for( unsigned int i = 0; i < m_nFaces; ++i )
{
     // Here I get the index to each vertex of the face
      v1 = m_face[ i ].getVertIndex( 0 ) + 1;
      v2 = m_face[ i ].getVertIndex( 1 ) + 1;
      v3 = m_face[ i ].getVertIndex( 2 ) + 1;

        // Fill in adjacency list for mesh with edges directed from lower numbered vertex to the higher numbered one
        // Make sure no pair is repeated by setting the left term of the pair to be always the min and the right term to the max
        min = v1 < v2 ? v1 : v2;
        max = v1 > v2 ? v1 : v2;
        // Check if tuple is already in the container
        it = m_edgeList.find( std::make_pair( min, max) );
        // Else, if returned iterator to map::end
        if( it == m_edgeList.end() )
        {
            // Add the new pair
            m_edgeList.insert( std::make_pair( std::pair< unsigned int, unsigned int > ( min, max ), 0 ) );
            m_boundaryEdgeList.insert( std::make_pair( std::pair< unsigned int, unsigned int > ( min, max ), 0 ) );
            m_nEdges++;
        }
        else
        {
            m_boundaryEdgeList.erase( std::make_pair( min, max ) );
        }

        min = v2 < v3 ? v2 : v3;
        max = v2 > v3 ? v2 : v3;
        it = m_edgeList.find( std::make_pair( min, max) );
        if( it == m_edgeList.end() )
        {
            m_edgeList.insert( std::make_pair( std::pair< unsigned int, unsigned int > ( min, max ), 0 ) );
            m_boundaryEdgeList.insert( std::make_pair( std::pair< unsigned int, unsigned int > ( min, max ), 0 ) );
            m_nEdges++;
        }
        else
        {
            m_boundaryEdgeList.erase( std::make_pair( min, max ) );
        }

        min = v1 < v3 ? v1 : v3;
        max = v1 > v3 ? v1 : v3;
        it = m_edgeList.find( std::make_pair( min, max) );
        if( it == m_edgeList.end() )
        {
            m_edgeList.insert( std::make_pair( std::pair< unsigned int, unsigned int > ( min, max ), 0 ) );
            m_boundaryEdgeList.insert( std::make_pair( std::pair< unsigned int, unsigned int > ( min, max ), 0 ) );
            m_nEdges++;
        }
        else
        {
            m_boundaryEdgeList.erase( std::make_pair( min, max ) );
        }

}

现在,奇怪的是m_boundaryEdgeList是正确的,而m_edgeList在调试时给了我这些时髦的数字。有人std::map有过这种奇怪的事吗?

我正在从.obj文件中读取数据,这些是我要插入地图的值

f 1/1/1 2/2/2 3/3/3
f 3/3/3 2/2/2 4/4/4
f 3/3/5 4/4/6 5/5/7
f 5/5/7 4/4/6 6/6/8
f 5/5/9 6/6/10 7/7/11
f 7/7/11 6/6/10 8/8/12
f 2/2/13 8/9/14 4/4/15
f 4/4/15 8/9/14 6/10/16
f 7/11/17 1/1/18 5/12/19
f 5/12/19 1/1/18 3/3/20

这是一个SSCCE http://pastebin.com/akHzzGNH

0 个答案:

没有答案