• 카테고리

    질문 & 답변
  • 세부 분야

    프로그래밍 언어

  • 해결 여부

    미해결

#include <algorithm>을 해주는 이유

24.02.28 20:20 작성 조회수 99

1

16.3에서 설명해주신 코드에서#include <algorithm>을 해주지 않아도 min_element()나 sort(), reverse()가 정상적으로 작동이 되는데, 그 이유가 궁금합니다! #include <iostream> #include <vector> using namespace std; int main() { vector <int> container; for(int i = 0; i < 10; ++i) container.push_back(i); auto itr = min_element(container.begin(), container.end()); cout << *itr << endl; // 삽입 itr = find(container.begin(), container.end(), 3); container.insert(itr, 128); for (auto &e : container) cout << e << " "; cout << endl; // 정렬 sort(container.begin(), container.end()); for (auto &e : container) cout << e << " "; cout << endl; // reverse 정렬 reverse(container.begin(), container.end()); for (auto &e : container) cout << e << " "; cout << endl; return 0; }

답변 1

답변을 작성해보세요.

1

Soobak님의 프로필

Soobak

2024.02.29

안녕하세요, 질문&답변 도우미 Soobak 입니다.

 

다른 C++ 표준 라이브러리 (예 : 코드에서 포함시키신 <iostrem>, <vector>) 내에서 간접적으로 포함되었을 수 있기 때문입니다.

<algorithm> 은 C++ 표준 라이브러리의 일부로, 특히, <vector> 헤더 내에서 컨테이너가 구현될 때 <algorithm> 헤더 파일을 간접적으로 포함되어 사용되었을 수 있습니다.

그러나, 이는 표준 라이브러리의 구현 방식과 컴파일러, 환경 등에 따라 다르게 작동할 수 있기 때문에,
사용하는 모든 표준 라이브러리의 기능에 대해 필요한 헤더 파일을 명시적으로 포함시키는 것이 좋습니다.