C/C++ : for ++

By | 2025년 11월 14일
Table of Contents

C/C++ : for ++

  • it++는 operator++(int)를 호출하여 이전 값의 복사본을 반환
  • ++it는 operator++()를 호출하여 자기 자신의 참조를 반환

컴파일러가 성능을 보정해 줄수도 있지만 ++it 가 더 효율적.

std::vector<int>::iterator it;
for (it = vec.begin(); it != vec.end(); it++)   // 임시 객체 생성 가능
for (it = vec.begin(); it != vec.end(); ++it)   // 더 효율적

답글 남기기