迭代器跳过循环

时间:2015-07-07 16:25:37

标签: c++ stl iterator

我正在使用列表容器但是当我使用for循环时我遇到了问题。我不明白为什么要跳过,我打印一些“标签”,直到哪个部分正在工作,唯一一个正在打印的是“First if”,然后跳过所有其他部分。为什么会出现这个问题?

标题

class CRoute
{
    private:
        vector<CWaypoint> m_pWaypoint;
        vector<CPOI*> m_pPoi;
        CPoiDatabase* m_pPoiDatabase;
        list<CWaypoint*>m_pRoute;
        CWpDatabase* m_pWpDatabase;

    public:
        void connectToPoiDatabase(CPoiDatabase* pPoiDB);
        void connectToWpDatabase(CWpDatabase* pWpDB);
        void addPoiAndWp(string namePoi, string afterWp);

};

.cpp的

void CRoute::addPoiAndWp(string namePoi, string afterWp)
{
    CPOI* poi = m_pPoiDatabase->getPointerToPoi(namePoi);

    list<CWaypoint*>::iterator pos1;

    if( (m_pWpDatabase != 0) && (poi != 0))
    {
        cout << "First if " << endl; // this is printed

        for(pos1 = m_pRoute.begin(); pos1 != m_pRoute.end(); pos1++) //here is skipping all
        {
            cout << "It's in the for loop" << endl;

            if( (*pos1)->getName() == afterWp)
            {
                cout << "Waypoint found! " << endl;
                list<CWaypoint*>::iterator pos2 = pos1;
                m_pRoute.insert(++pos2,poi);
            }

            cout << "Before leave the loop" << endl;
        }
    }
    else
    {
        cout << "WP not found / DB not connected " << endl;
    }
        cout << "Waypoint not found " << endl; // This is also printed

}

1 个答案:

答案 0 :(得分:1)

问题是,对于空列表,--create vertices array (with duplicates, 3 vertices for each face) for f = 1 to (meshOp.getNumFaces eMesh) do ( face = getFace eMesh f for i = 1 to 3 do ( --for each of 3 vertices in face (triangle) vertexPos = (getvert eMesh face[i]) * (inverse eMesh.objecttransform) --vertex position in object space (including rotation) .begin()将返回相同的值。我建议你在进入循环之前插入第一个值或空值。试试如下。

.end()