Skip to content

Commit 97832fb

Browse files
Tyler WhitneyTyler Whitney
authored andcommitted
draft contains() work
1 parent 797fcee commit 97832fb

File tree

9 files changed

+145
-57
lines changed

9 files changed

+145
-57
lines changed

docs/standard-library/map-class.md

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -534,10 +534,15 @@ Checks if there's an element the specified key in the `map`.
534534

535535
```cpp
536536
[[nodiscard]] bool contains(const K& key) const;
537+
538+
template<class K> [[nodiscard]] bool contains(const K& key) const;
537539
```
538540
539541
### Parameters
540542
543+
*K*\
544+
The type of the key.
545+
541546
*key*\
542547
The element's key value to look for.
543548
@@ -549,6 +554,8 @@ The element's key value to look for.
549554
550555
`Contains()` is new in C++20. To use it, specify the [std:c++latest](../build/reference/std-specify-language-standard-version.md) compiler option.
551556
557+
`template<class Key> [[nodiscard]] bool contains(const Key& key) const;`only participates in overload resolution if `key_compare` is transparent. See [Heterogeneous lookup in associative containers](https://docs.microsoft.com/cpp/standard-library/stl-containers#heterogeneous-lookup-in-associative-containers-c14) for more information.
558+
552559
### Example
553560
554561
```cpp
@@ -558,15 +565,24 @@ The element's key value to look for.
558565
559566
int main()
560567
{
561-
std::map<int, bool> theMap = {{0, true},{1, false}};
562-
std::cout << std::boolalpha << theMap.contains(1) << '\n';
563-
std::cout << std::boolalpha << theMap.contains(2) << '\n';
568+
std::map<int, bool> m = {{0, true},{1, false}};
569+
570+
std::cout << std::boolalpha; // so booleans show as 'true' or 'false'
571+
std::cout << m.contains(1) << '\n';
572+
std::cout << m.contains(2) << '\n';
573+
574+
// call template function
575+
std::map<std::string, int, std::less<>> m2 = {{"ten", 10}, {"twenty", 20}, {"thirty", 30}};
576+
std::cout << m2.contains("ten");
577+
578+
return 0;
564579
}
565580
```
566581

567582
```Output
568583
true
569584
false
585+
true
570586
```
571587

572588
## <a name="crbegin"></a> crbegin
@@ -1770,9 +1786,9 @@ All constructors store a function object of type Traits that is used to establis
17701786
17711787
The first three constructors specify an empty initial map, the second specifying the type of comparison function (*Comp*) to be used in establishing the order of the elements and the third explicitly specifying the allocator type (*Al*) to be used. The key word **`explicit`** suppresses certain kinds of automatic type conversion.
17721788
1773-
The 4th constructor specifies a copy of the map *Right*.
1789+
The fourth constructor specifies a copy of the map *Right*.
17741790
1775-
The 5th constructor specifies a copy of the map by moving *Right*.
1791+
The fifth constructor specifies a copy of the map by moving *Right*.
17761792
17771793
The 6th, 7th, and 8th constructors use an initializer_list from which to copy the members.
17781794

docs/standard-library/multimap-class.md

Lines changed: 48 additions & 33 deletions
Large diffs are not rendered by default.

docs/standard-library/multiset-class.md

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ The C++ Standard Library `multiset` class is:
4848
4949
The iterator provided by the `multiset` class is a bidirectional iterator, but the class member functions [insert](#insert) and [multiset](#multiset) have versions that take as template parameters a weaker input iterator, whose functionality requirements are more minimal than those guaranteed by the class of bidirectional iterators. The different iterator concepts form a family related by refinements in their functionality. Each iterator concept has its own set of requirements and the algorithms that work with them must limit their assumptions to the requirements provided by that type of iterator. It may be assumed that an input iterator may be dereferenced to refer to some object and that it may be incremented to the next iterator in the sequence. This is a minimal set of functionality, but it is enough to be able to talk meaningfully about a range of iterators [ `First`, `Last`) in the context of the class's member functions.
5050
51-
The choice of container type should be based in general on the type of searching and inserting required by the application. Associative containers are optimized for the operations of lookup, insertion and removal. The member functions that explicitly support these operations are efficient, performing them in a time that is on average proportional to the logarithm of the number of elements in the container. Inserting elements invalidates no iterators, and removing elements invalidates only those iterators that had specifically pointed at the removed elements.
51+
The choice of container type should be based in general on the type of searching and inserting required by the application. Associative containers are optimized for the operations of lookup, insertion and removal. The member functions that explicitly support these operations are efficient, performing them in a time that is on average proportional to the logarithm of the number of elements in the container. Inserting elements invalidates no iterators, and removing elements invalidates only those iterators that had pointed at the removed elements.
5252
5353
The `multiset` should be the associative container of choice when the conditions associating the values with their keys are satisfies by the application. The elements of a `multiset` may be multiple and serve as their own sort keys, so keys are not unique. A model for this type of structure is an ordered list of, say, words in which the words may occur more than once. Had multiple occurrences of the words not been allowed, then a set would have been the appropriate container structure. If unique definitions were attached as values to the list of unique key words, then a map would be an appropriate structure to contain this data. If instead the definitions were not unique, then a multimap would be the container of choice.
5454
55-
The `multiset` orders the sequence it controls by calling a stored function object of type *Compare*. This stored object is a comparison function that may be accessed by calling the member function [key_comp](#key_comp). In general, the elements need be merely less than comparable to establish this order: so that, given any two elements, it may be determined either that they are equivalent (in the sense that neither is less than the other) or that one is less than the other. This results in an ordering between the nonequivalent elements. On a more technical note, the comparison function is a binary predicate that induces a strict weak ordering in the standard mathematical sense. A binary predicate *f*( *x*, *y*) is a function object that has two argument objects *x* and *y* and a return value of **`true`** or **`false`**. An ordering imposed on a set is a strict weak ordering if the binary predicate is irreflexive, antisymmetric, and transitive and if equivalence is transitive, where two objects x and y are defined to be equivalent when both *f*( *x,y*) and *f*( *y,x*) are false. If the stronger condition of equality between keys replaces that of equivalence, then the ordering becomes total (in the sense that all the elements are ordered with respect to each other) and the keys matched will be indiscernible from each other.
55+
The `multiset` orders the sequence it controls by calling a stored function object of type *Compare*. This stored object is a comparison function that may be accessed by calling the member function [key_comp](#key_comp). In general, the elements need be merely less than comparable to establish this order: so that, given any two elements, it may be determined either that they are equivalent (in the sense that neither is less than the other) or that one is less than the other. This results in an ordering between the nonequivalent elements. On a more technical note, the comparison function is a binary predicate that induces a strict weak ordering in the standard mathematical sense. A binary predicate *f*(*x*, *y*) is a function object that has two argument objects *x* and *y* and a return value of **`true`** or **`false`**. An ordering imposed on a set is a strict weak ordering if the binary predicate is irreflexive, antisymmetric, and transitive and if equivalence is transitive, where two objects x and y are defined to be equivalent when both *f*(*x,y*) and *f*(*y,x*) are false. If the stronger condition of equality between keys replaces that of equivalence, then the ordering becomes total (in the sense that all the elements are ordered with respect to each other) and the keys matched will be indiscernible from each other.
5656
57-
In C++14 you can enable heterogeneous lookup by specifying the `std::less<>` or `std::greater<>` predicate that has no type parameters. For more information, see [Heterogeneous Lookup in Associative Containers](../standard-library/stl-containers.md#sequence_containers)
57+
In C++14, you can enable heterogeneous lookup by specifying the `std::less<>` or `std::greater<>` predicate that has no type parameters. For more information, see [Heterogeneous Lookup in Associative Containers](../standard-library/stl-containers.md#sequence_containers)
5858
5959
### Constructors
6060
@@ -381,10 +381,15 @@ Checks if there is an element the specified key in the `multiset` .
381381

382382
```cpp
383383
[[nodiscard]] bool contains(const K& key) const;
384+
385+
template<class K> [[nodiscard]] bool contains(const K& key) const;
384386
```
385387
386388
### Parameters
387389
390+
*K*\
391+
The type of the key.
392+
388393
*key*\
389394
The element's key value to look for.
390395
@@ -396,6 +401,8 @@ The element's key value to look for.
396401
397402
`Contains()` is new in C++20. To use it, specify the [std:c++latest](../build/reference/std-specify-language-standard-version.md) compiler option.
398403
404+
`template<class Key> [[nodiscard]] bool contains(const Key& key) const;`only participates in overload resolution if `key_compare` is transparent. See [Heterogeneous lookup in associative containers](https://docs.microsoft.com/cpp/standard-library/stl-containers#heterogeneous-lookup-in-associative-containers-c14) for more information.
405+
399406
### Example
400407
401408
```cpp
@@ -407,8 +414,9 @@ int main()
407414
{
408415
std::multiset<int> theMultiSet = {1, 2};
409416
410-
std::cout << std::boolalpha << theMultiSet.contains(2) << '\n';
411-
std::cout << std::boolalpha << theMultiSet.contains(3) << '\n';
417+
std::cout << std::boolalpha; // so booleans show as 'true' or 'false'
418+
std::cout << theMultiSet.contains(2) << '\n';
419+
std::cout << theMultiSet.contains(3) << '\n';
412420
}
413421
```
414422

@@ -1557,9 +1565,9 @@ All constructors store a function object of type Compare that is used to establi
15571565

15581566
The first three constructors specify an empty initial multiset, the second specifying the type of comparison function (*Comp*) to be used in establishing the order of the elements and the third explicitly specifying the allocator type (*Al*) to be used. The keyword **`explicit`** suppresses certain kinds of automatic type conversion.
15591567

1560-
The 4th constructor specifies a copy of the multiset *Right*.
1568+
The fourth constructor specifies a copy of the multiset *Right*.
15611569

1562-
The 5th constructor specifies a copy of the multiset by moving *Right*.
1570+
The fifth constructor specifies a copy of the multiset by moving *Right*.
15631571

15641572
The 6th, 7th, and 8th constructors specify an initializer_list from which to copy the elements.
15651573

docs/standard-library/set-class.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
---
22
title: "set Class"
3+
description: "API reference for the C++ Standard Library container class `set`, which is used to store and retrieve data from a collection."
34
ms.date: "9/9/2020"
45
f1_keywords: ["set/std::set", "set/std::set::allocator_type", "set/std::set::const_iterator", "set/std::set::const_pointer", "set/std::set::const_reference", "set/std::set::const_reverse_iterator", "set/std::set::difference_type", "set/std::set::iterator", "set/std::set::key_compare", "set/std::set::key_type", "set/std::set::pointer", "set/std::set::reference", "set/std::set::reverse_iterator", "set/std::set::size_type", "set/std::set::value_compare", "set/std::set::value_type", "set/std::set::begin", "set/std::set::cbegin", "set/std::set::cend", "set/std::set::clear", "set/std::set::contains", "set/std::set::count", "set/std::set::crbegin", "set/std::set::crend", "set/std::set::emplace", "set/std::set::emplace_hint", "set/std::set::empty", "set/std::set::end", "set/std::set::equal_range", "set/std::set::erase", "set/std::set::find", "set/std::set::get_allocator", "set/std::set::insert", "set/std::set::key_comp", "set/std::set::lower_bound", "set/std::set::max_size", "set/std::set::rbegin", "set/std::set::rend", "set/std::set::size", "set/std::set::swap", "set/std::set::upper_bound", "set/std::set::value_comp"]
56
helpviewer_keywords: ["std::set [C++]", "std::set [C++], allocator_type", "std::set [C++], const_iterator", "std::set [C++], const_pointer", "std::set [C++], const_reference", "std::set [C++], const_reverse_iterator", "std::set [C++], difference_type", "std::set [C++], iterator", "std::set [C++], key_compare", "std::set [C++], key_type", "std::set [C++], pointer", "std::set [C++], reference", "std::set [C++], reverse_iterator", "std::set [C++], size_type", "std::set [C++], value_compare", "std::set [C++], value_type", "std::set [C++], begin", "std::set [C++], cbegin", "std::set [C++], cend", "std::set [C++], clear", "std::set [C++], contains", "std::set [C++], count", "std::set [C++], crbegin", "std::set [C++], crend", "std::set [C++], emplace", "std::set [C++], emplace_hint", "std::set [C++], empty", "std::set [C++], end", "std::set [C++], equal_range", "std::set [C++], erase", "std::set [C++], find", "std::set [C++], get_allocator", "std::set [C++], insert", "std::set [C++], key_comp", "std::set [C++], lower_bound", "std::set [C++], max_size", "std::set [C++], rbegin", "std::set [C++], rend", "std::set [C++], size", "std::set [C++], swap", "std::set [C++], upper_bound", "std::set [C++], value_comp"]
67
ms.assetid: 8991f9aa-5509-4440-adc1-371512d32018
78
---
89
# set Class
910

10-
The C++ Standard Library container class set is used for the storage and retrieval of data from a collection in which the values of the elements contained are unique and serve as the key values according to which the data is automatically ordered. The value of an element in a set may not be changed directly. Instead, you must delete old values and insert elements with new values.
11+
The C++ Standard Library container class `set` is used to store and retrieve data from a collection. The values of the elements in the `set` are unique and serve as the key values according to which the data is automatically ordered. The value of an element in a `set` may not be changed directly. Instead, you must delete old values and insert elements with new values.
1112

1213
## Syntax
1314

@@ -380,10 +381,15 @@ Checks if there's an element the specified key in the `set`.
380381

381382
```cpp
382383
[[nodiscard]] bool contains(const K& key) const;
384+
385+
template<class K> [[nodiscard]] bool contains(const K& key) const;
383386
```
384387
385388
### Parameters
386389
390+
*K*\
391+
The type of the key.
392+
387393
*key*\
388394
The element's key value to look for.
389395
@@ -395,6 +401,8 @@ The element's key value to look for.
395401
396402
`Contains()` is new in C++20. To use it, specify the [std:c++latest](../build/reference/std-specify-language-standard-version.md) compiler option.
397403
404+
`template<class Key> [[nodiscard]] bool contains(const Key& key) const;`only participates in overload resolution if `key_compare` is transparent. See [Heterogeneous lookup in associative containers](https://docs.microsoft.com/cpp/standard-library/stl-containers#heterogeneous-lookup-in-associative-containers-c14) for more information.
405+
398406
### Example
399407
400408
```cpp
@@ -406,8 +414,9 @@ int main()
406414
{
407415
std::set<int> theSet = {1, 2};
408416
409-
std::cout << std::boolalpha << theSet.contains(2) << '\n';
410-
std::cout << std::boolalpha << theSet.contains(3) << '\n';
417+
std::cout << std::boolalpha; // so booleans show as 'true' or 'false'
418+
std::cout << theSet.contains(2) << '\n';
419+
std::cout << theSet.contains(3) << '\n';
411420
}
412421
```
413422

docs/standard-library/unordered-map-class.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -770,10 +770,15 @@ Introduced in C++20.
770770

771771
```cpp
772772
[[nodiscard]] bool contains(const key_type& key) const;
773+
774+
template<class K> [[nodiscard]] bool contains(const K& key) const;
773775
```
774776
775777
### Parameters
776778
779+
*K*\
780+
The type of the key.
781+
777782
*key*\
778783
The key value of the element to look for.
779784
@@ -785,6 +790,8 @@ The key value of the element to look for.
785790
786791
`Contains()` is new in C++20. To use it, specify the [std:c++latest](../build/reference/std-specify-language-standard-version.md) compiler option.
787792
793+
`template<class Key> [[nodiscard]] bool contains(const Key& key) const;`only participates in overload resolution if `key_compare` is transparent. See [Heterogeneous lookup in associative containers](https://docs.microsoft.com/cpp/standard-library/stl-containers#heterogeneous-lookup-in-associative-containers-c14) for more information.
794+
788795
### Example
789796
790797
```cpp
@@ -796,8 +803,9 @@ int main()
796803
{
797804
std::unordered_map<int, bool> theUnorderedMap = {{0, false}, {1,true}};
798805
799-
std::cout << std::boolalpha << theUnorderedMap.contains(1) << '\n';
800-
std::cout << std::boolalpha << theUnorderedMap.contains(2) << '\n';
806+
std::cout << std::boolalpha; // so booleans show as 'true' or 'false'
807+
std::cout << theUnorderedMap.contains(1) << '\n';
808+
std::cout << theUnorderedMap.contains(2) << '\n';
801809
}
802810
```
803811

docs/standard-library/unordered-map.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
title: "&lt;unordered_map&gt;"
3+
description: "API overview for the C++ Standard Library container class `map`."
34
ms.date: "11/04/2016"
45
f1_keywords: ["<unordered_map>"]
56
helpviewer_keywords: ["unordered_map header"]

0 commit comments

Comments
 (0)