<p>Another design decision that baffled me. Checking if an element is in a set is the entire purpose of a set! Until C++20 I had to write stuff like <code>s.find(x) != s.end()</code> or the nicer <code>s.count()</code> cast as a bool that's almost as good as <code>contains()</code> but somewhat obscures the intent (and runs in O(n) for multisets?). Even if the standard library containers were designed around iterators,  <code>count()</code> has existed from the beginning.</p>
<p>The <a href="https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0458r2.html" rel="noreferrer">P0458 proposal to add contains</a> is obvious, but there must've been some kind of argument for leaving it out originally and then leaving it out every time it was brought up. Was there some kind of design philosophy change in C++20?</p>

<standard-library><c++><history>
Why did std::set not have a contains function until C++20?
<p>Because they wanted all containers to adhere to the same interface as much as possible, and <code>.count()</code> made sense for more containers, and still worked equivalently for <code>set</code>. They didn't want to break interface uniformity just for clearer semantics in one case.</p>
<p>Later on that decision was re-evaluated.</p>
<p>Also, the lack was not really much of a lack, since you can say <code>if (myset.count(x))</code> just the same as you'd say <code>if (myset.contains(x))</code>. In modern times, more people run tools that complain about treating ints as bools, so that's become less attractive.</p>

CC BY-SA 4.0
2024-07-19T22:59:18.590Z
2024-07-19T22:59:18.590Z
Nick Matteo
CC BY-SA 4.0
2024-07-20T00:18:18.290Z
`multiset.count(x)` will take longer than `contains`
qwr
CC BY-SA 4.0
2024-07-20T03:55:21.863Z
@qwr So for `multiset` you'd use the `.find()` idiom.
Barmar
CC BY-SA 4.0
2024-07-20T06:30:48.857Z
Even if count and contains had the same cost for the container, it would be confusing to do this, because you would need to be aware that it has the same cost in that case. For most containers, you really wouldn't realize a logical contains call with an actual count call.
feldentm
# Why did std::set not have a contains function until C++20?
Posted by: username_0 on 2024-07-19
Another design decision that baffled me. Checking if an element is in a set is the entire purpose of a set! Until C++20 I had to write stuff like `s.find(x) != s.end()` or the nicer `s.count()` cast as a bool that's almost as good as `contains()` but somewhat obscures the intent (and runs in O(n) for multisets?). Even if the standard library containers were designed around iterators, `count()` has existed from the beginning.

The [P0458 proposal to add contains](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0458r2.html) is obvious, but there must've been some kind of argument for leaving it out originally and then leaving it out every time it was brought up. Was there some kind of design philosophy change in C++20?
**Tags:** standard-library c++ history
---
## 1 answers
### Answer by username_1 on 2024-07-19. Score: 7
Because they wanted all containers to adhere to the same interface as much as possible, and `.count()` made sense for more containers, and still worked equivalently for `set`. They didn't want to break interface uniformity just for clearer semantics in one case.

Later on that decision was re-evaluated.

Also, the lack was not really much of a lack, since you can say `if (myset.count(x))` just the same as you'd say `if (myset.contains(x))`. In modern times, more people run tools that complain about treating ints as bools, so that's become less attractive.
> **Comments:**
> **username_0** (2024-07-20): `multiset.count(x)` will take longer than `contains`  
> **username_2** (2024-07-20): @qwr So for `multiset` you'd use the `.find()` idiom.  
> **username_3** (2024-07-20): Even if count and contains had the same cost for the container, it would be confusing to do this, because you would need to be aware that it has the same cost in that case. For most containers, you really wouldn't realize a logical contains call with an actual count call.  
---
