vladon
vladon

Reputation: 8401

non-const find() in std::unordered_set

Why for there is non-const find() in std::unordered_set()?

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

iterator is the same as const_iterator, why there is non-const version of find()?

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

Upvotes: 2

Views: 726

Answers (1)

Slava
Slava

Reputation: 44238

iterator is the same as const_iterator, why there is non-const version of find()?

Because iterator is not mandatory the same as const_iterator, as stated in documentation:

The member types iterator and const_iterator may be aliases to the same type. Since iterator is convertible to const_iterator, const_iterator should be used in function parameter lists to avoid violations of the One Definition Rule.

emphasis is mine. Since they are not mandatory the same some generic code can depend on particular type of iterator returned by find() and it should be consistent with other containers.

Upvotes: 4

Related Questions