ABOUT ME

IT 기업에 근무하는 S/W 개발자이자입니다. Server 개발 및 DevOps, S/W Test Automation을 주로 다루고 있습니다.

  • erase() 사용시 주의점.
    Algorithm & Date structure/STL 2008. 11. 25. 11:20
    728x90

    erase()는 반복자를 삭제후 그 다음위치를 리턴합니다.

    그러니 erase()를 사용후 백터의 크기를 조사해서 예외처리를 해주도록 하는게 안전하겠죠?

    std::vector<int> arrInt;
    std::vector<int>::iterlator itor = arrInt.begin();

    while ( itor != arrInt.end() )
    {
        cin << (*itor);
        itor = arrInt.erase(itor);
    }

    혹은

    std::list<int> arrInt;
    std::list<int>::iterlator itor = arrInt.begin();

    while ( itor != arrInt.end() )
    {
        cin << (*itor);
        itor = arrInt.erase(itor);
    }

    'Algorithm & Date structure > STL' 카테고리의 다른 글

    댓글

Designed by black7375.