2017-12-05 11 views
2

find()이 아닌 이유는 std::unordered_set()에 있습니까? find()의 const가 아닌 버전이 왜std :: unordered_set의 non-const find()

iterator find(const Key& key); 
const_iterator find(const Key& key) const; 

iteratorconst_iterator과 동일? 발견의 const가 아닌 버전이 왜

http://en.cppreference.com/w/cpp/container/unordered_set/find

+0

@ FrançoisAndrieux'find()'*** 메소드와'find()/* no const * /'지정자에 대해 얘기하고 있습니다. – vladon

+2

다른 모든 컨테이너와 일관성을 유지합니다. set 키가 불변인데도 이것은 다른 컨테이너에서 작동 할 수있는 템플릿의 개발을 가능하게하고, 컨테이너가'iterator' 멤버 클래스와 적절한'find()'에 대한 적절한 정의를 제공 할 것으로 기대하는 템플릿을 갖게합니다. –

+0

@vladon 나는 그 질문을 오해했다. 나는 당신이 non-const iterator를 얻기 위해'find '를 사용하는 방법을 묻고 있다고 생각했다. –

답변

4

반복자는, const_iterator를과 동일()?

documentation에 명시된 바와 같이 반복자, const_iterator를 같은 필수 아니므 :

멤버 타입과 반복기 const_iterator를 동일한 유형 별칭 일 수있다. iterator는 이므로 const_iterator로 변환이 가능하므로 하나의 정의 규칙 위반을 피하기 위해 const_iterator를 함수 매개 변수 목록에 사용해야합니다.

중점은 내 것입니다. 필수 코드가 아니기 때문에 동일한 일반 코드는 find()에 의해 반환되는 반복기의 특정 유형에 따라 달라질 수 있으며 다른 컨테이너와 일치해야합니다.