|
1 | 1 | --- |
2 | 2 | title: "map Class" |
3 | | -ms.date: "10/18/2018" |
4 | | -f1_keywords: ["map/std::map", "map/std::map::allocator_type", "map/std::map::const_iterator", "map/std::map::const_pointer", "map/std::map::const_reference", "map/std::map::const_reverse_iterator", "map/std::map::difference_type", "map/std::map::iterator", "map/std::map::key_compare", "map/std::map::key_type", "map/std::map::mapped_type", "map/std::map::pointer", "map/std::map::reference", "map/std::map::reverse_iterator", "map/std::map::size_type", "map/std::map::value_type", "map/std::map::at", "map/std::map::begin", "map/std::map::cbegin", "map/std::map::cend", "map/std::map::clear", "map/std::map::count", "map/std::map::crbegin", "map/std::map::crend", "map/std::map::emplace", "map/std::map::emplace_hint", "map/std::map::empty", "map/std::map::end", "map/std::map::equal_range", "map/std::map::erase", "map/std::map::find", "map/std::map::get_allocator", "map/std::map::insert", "map/std::map::key_comp", "map/std::map::lower_bound", "map/std::map::max_size", "map/std::map::rbegin", "map/std::map::rend", "map/std::map::size", "map/std::map::swap", "map/std::map::upper_bound", "map/std::map::value_comp"] |
5 | | -helpviewer_keywords: ["std::map [C++]", "std::map [C++], allocator_type", "std::map [C++], const_iterator", "std::map [C++], const_pointer", "std::map [C++], const_reference", "std::map [C++], const_reverse_iterator", "std::map [C++], difference_type", "std::map [C++], iterator", "std::map [C++], key_compare", "std::map [C++], key_type", "std::map [C++], mapped_type", "std::map [C++], pointer", "std::map [C++], reference", "std::map [C++], reverse_iterator", "std::map [C++], size_type", "std::map [C++], value_type", "std::map [C++], at", "std::map [C++], begin", "std::map [C++], cbegin", "std::map [C++], cend", "std::map [C++], clear", "std::map [C++], count", "std::map [C++], crbegin", "std::map [C++], crend", "std::map [C++], emplace", "std::map [C++], emplace_hint", "std::map [C++], empty", "std::map [C++], end", "std::map [C++], equal_range", "std::map [C++], erase", "std::map [C++], find", "std::map [C++], get_allocator", "std::map [C++], insert", "std::map [C++], key_comp", "std::map [C++], lower_bound", "std::map [C++], max_size", "std::map [C++], rbegin", "std::map [C++], rend", "std::map [C++], size", "std::map [C++], swap", "std::map [C++], upper_bound", "std::map [C++], value_comp"] |
| 3 | +ms.date: "9/9/2020" |
| 4 | +f1_keywords: ["map/std::map", "map/std::map::allocator_type", "map/std::map::const_iterator", "map/std::map::const_pointer", "map/std::map::const_reference", "map/std::map::const_reverse_iterator", "map/std::map::difference_type", "map/std::map::iterator", "map/std::map::key_compare", "map/std::map::key_type", "map/std::map::mapped_type", "map/std::map::pointer", "map/std::map::reference", "map/std::map::reverse_iterator", "map/std::map::size_type", "map/std::map::value_type", "map/std::map::at", "map/std::map::begin", "map/std::map::cbegin", "map/std::map::cend", "map/std::map::clear", "map/std::map::count", "map/std::map::contains", "map/std::map::crbegin", "map/std::map::crend", "map/std::map::emplace", "map/std::map::emplace_hint", "map/std::map::empty", "map/std::map::end", "map/std::map::equal_range", "map/std::map::erase", "map/std::map::find", "map/std::map::get_allocator", "map/std::map::insert", "map/std::map::key_comp", "map/std::map::lower_bound", "map/std::map::max_size", "map/std::map::rbegin", "map/std::map::rend", "map/std::map::size", "map/std::map::swap", "map/std::map::upper_bound", "map/std::map::value_comp"] |
| 5 | +helpviewer_keywords: ["std::map [C++]", "std::map [C++], allocator_type", "std::map [C++], const_iterator", "std::map [C++], const_pointer", "std::map [C++], const_reference", "std::map [C++], const_reverse_iterator", "std::map [C++], difference_type", "std::map [C++], iterator", "std::map [C++], key_compare", "std::map [C++], key_type", "std::map [C++], mapped_type", "std::map [C++], pointer", "std::map [C++], reference", "std::map [C++], reverse_iterator", "std::map [C++], size_type", "std::map [C++], value_type", "std::map [C++], at", "std::map [C++], begin", "std::map [C++], cbegin", "std::map [C++], cend", "std::map [C++], clear", "std::map [C++], count", "std::map [C++], contains", "std::map [C++], crbegin", "std::map [C++], crend", "std::map [C++], emplace", "std::map [C++], emplace_hint", "std::map [C++], empty", "std::map [C++], end", "std::map [C++], equal_range", "std::map [C++], erase", "std::map [C++], find", "std::map [C++], get_allocator", "std::map [C++], insert", "std::map [C++], key_comp", "std::map [C++], lower_bound", "std::map [C++], max_size", "std::map [C++], rbegin", "std::map [C++], rend", "std::map [C++], size", "std::map [C++], swap", "std::map [C++], upper_bound", "std::map [C++], value_comp"] |
6 | 6 | ms.assetid: 7876f4c9-ebb4-4878-af1e-09364c43af0a |
7 | 7 | --- |
8 | 8 | # map Class |
@@ -98,32 +98,33 @@ The map orders the elements it controls by calling a stored function object of t |
98 | 98 |
|
99 | 99 | |Member function|Description| |
100 | 100 | |-|-| |
101 | | -|[at](#at)|Finds an element with a specified key value.| |
102 | | -|[begin](#begin)|Returns an iterator that points to the first element in the map.| |
103 | | -|[cbegin](#cbegin)|Returns a const iterator that points to the first element in the map.| |
| 101 | +|[at](#at)|Finds an element with the specified key value.| |
| 102 | +|[begin](#begin)|Returns an iterator that points to the first element in the `map`.| |
| 103 | +|[cbegin](#cbegin)|Returns a const iterator that points to the first element in the `map`.| |
104 | 104 | |[cend](#cend)|Returns a const past-the-end iterator.| |
105 | | -|[clear](#clear)|Erases all the elements of a map.| |
| 105 | +|[clear](#clear)|Erases all the elements of a `map`.| |
| 106 | +|[contains](#contains)|Checks if there is an element with the specified key in the `map`.| |
106 | 107 | |[count](#count)|Returns the number of elements in a map whose key matches the key specified in a parameter.| |
107 | | -|[crbegin](#crbegin)|Returns a const iterator that points to the first element in a reversed map.| |
108 | | -|[crend](#crend)|Returns a const iterator that points to the location after the last element in a reversed map.| |
109 | | -|[emplace](#emplace)|Inserts an element constructed in place into the map.| |
110 | | -|[emplace_hint](#emplace_hint)|Inserts an element constructed in place into the map, with a placement hint.| |
111 | | -|[empty](#empty)|Returns **`true`** if a map is empty.| |
| 108 | +|[crbegin](#crbegin)|Returns a const iterator that points to the first element in a reversed `map`.| |
| 109 | +|[crend](#crend)|Returns a const iterator that points to the location after the last element in a reversed `map`.| |
| 110 | +|[emplace](#emplace)|Inserts an element constructed in place into the `map`.| |
| 111 | +|[emplace_hint](#emplace_hint)|Inserts an element constructed in place into the `map`, with a placement hint.| |
| 112 | +|[empty](#empty)|Returns **`true`** if a `map` is empty.| |
112 | 113 | |[end](#end)|Returns the past-the-end iterator.| |
113 | 114 | |[equal_range](#equal_range)|Returns a pair of iterators. The first iterator in the pair points to the first element in a `map` with a key that is greater than a specified key. The second iterator in the pair points to the first element in the `map` with a key that is equal to or greater than the key.| |
114 | 115 | |[erase](#erase)|Removes an element or a range of elements in a map from the specified positions.| |
115 | | -|[find](#find)|Returns an iterator that points to the location of an element in a map that has a key equal to a specified key.| |
116 | | -|[get_allocator](#get_allocator)|Returns a copy of the `allocator` object that is used to construct the map.| |
117 | | -|[insert](#insert)|Inserts an element or a range of elements into the map at a specified position.| |
118 | | -|[key_comp](#key_comp)|Returns a copy of the comparison object that used to order keys in a map.| |
119 | | -|[lower_bound](#lower_bound)|Returns an iterator to the first element in a map that has a key value that is equal to or greater than that of a specified key.| |
120 | | -|[max_size](#max_size)|Returns the maximum length of the map.| |
121 | | -|[rbegin](#rbegin)|Returns an iterator that points to the first element in a reversed map.| |
122 | | -|[rend](#rend)|Returns an iterator that points to the location after the last element in a reversed map.| |
| 116 | +|[find](#find)|Returns an iterator that points to the location of an element in a `map` that has a key equal to a specified key.| |
| 117 | +|[get_allocator](#get_allocator)|Returns a copy of the `allocator` object that is used to construct the `map`.| |
| 118 | +|[insert](#insert)|Inserts an element or a range of elements into the `map` at a specified position.| |
| 119 | +|[key_comp](#key_comp)|Returns a copy of the comparison object that used to order keys in a `map`.| |
| 120 | +|[lower_bound](#lower_bound)|Returns an iterator to the first element in a `map` that has a key value that is equal to or greater than that of a specified key.| |
| 121 | +|[max_size](#max_size)|Returns the maximum length of the `map`.| |
| 122 | +|[rbegin](#rbegin)|Returns an iterator that points to the first element in a reversed m`ap.| |
| 123 | +|[rend](#rend)|Returns an iterator that points to the location after the last element in a reversed `map`.| |
123 | 124 | |[size](#size)|Returns the number of elements in the map.| |
124 | 125 | |[swap](#swap)|Exchanges the elements of two maps.| |
125 | | -|[upper_bound](#upper_bound)|Returns an iterator to the first element in a map that has a key value that is greater than that of a specified key.| |
126 | | -|[value_comp](#value_comp)|Retrieves a copy of the comparison object that is used to order element values in a map.| |
| 126 | +|[upper_bound](#upper_bound)|Returns an iterator to the first element in a `map` that has a key value that is greater than that of a specified key.| |
| 127 | +|[value_comp](#value_comp)|Retrieves a copy of the comparison object that is used to order element values in a `map`.| |
127 | 128 |
|
128 | 129 | ### Operators |
129 | 130 |
|
@@ -526,6 +527,47 @@ The number of elements in m1 with a sort key of 2 is: 1. |
526 | 527 | The number of elements in m1 with a sort key of 3 is: 0. |
527 | 528 | ``` |
528 | 529 |
|
| 530 | +## <a name="contains"></a> contains |
| 531 | + |
| 532 | +Checks if there is an element the specified key in the `map` . |
| 533 | + |
| 534 | +```cpp |
| 535 | +[[nodiscard]] bool contains(const K& key) const; |
| 536 | +``` |
| 537 | +
|
| 538 | +### Parameters |
| 539 | +
|
| 540 | +*key*\ |
| 541 | +The element's key value to look for. |
| 542 | +
|
| 543 | +### Return Value |
| 544 | +
|
| 545 | +`true` if the element is found in the container; `false` otherwise. |
| 546 | +
|
| 547 | +### Remarks |
| 548 | +
|
| 549 | +`Contains()` is new in C++20. To use it, specify the [std:c++latest](../build/reference/std-specify-language-standard-version.md) compiler option. |
| 550 | +
|
| 551 | +### Example |
| 552 | +
|
| 553 | +```cpp |
| 554 | +// Requires /std:c++latest |
| 555 | +#include <map> |
| 556 | +#include <iostream> |
| 557 | +
|
| 558 | +int main() |
| 559 | +{ |
| 560 | + std::map<int, bool> theMap = {{0, true},{1, false}}; |
| 561 | + std::cout << std::boolalpha << theMap.contains(1) << '\n'; |
| 562 | + std::cout << std::boolalpha << theMap.contains(2) << '\n'; |
| 563 | +} |
| 564 | +``` |
| 565 | + |
| 566 | +```Output |
| 567 | +true |
| 568 | +false |
| 569 | +``` |
| 570 | + |
529 | 571 | ## <a name="crbegin"></a> crbegin |
530 | 572 |
|
531 | 573 | Returns a const iterator addressing the first element in a reversed map. |
|
0 commit comments