formatting
This commit is contained in:
@@ -72,7 +72,7 @@ The mechanics of using std::pairs are relatively straightforward:
|
||||
- std::pairs are a templated struct with just two members, called first and second. Reminder: a struct
|
||||
is basically a wimpy class and in this course you aren’t allowed to create new structs. You should use classes
|
||||
instead.
|
||||
- To work with pairs, you must #include <utility>. Note that the header file for maps (#include <map>)
|
||||
- To work with pairs, you must #include <utility>. Note that the header file for maps (#include <map>)
|
||||
itself includes utility, so you don’t have to include utility explicitly when you use pairs with maps.
|
||||
- Here are simple examples of manipulating pairs:
|
||||
```cpp
|
||||
@@ -103,8 +103,12 @@ std::pair<const key_type, value_type>
|
||||
- We’ve used the [] operator on vectors, which is conceptually very simple because vectors are just resizable
|
||||
arrays. Arrays and vectors are efficient random access data structures.
|
||||
- But operator[] is actually a function call, so it can do things that aren’t so simple too, for example:
|
||||
|
||||
```cpp
|
||||
++counters[s];
|
||||
For maps, the [] operator searches the map for the pair containing the key (string) s.
|
||||
```
|
||||
|
||||
- For maps, the [] operator searches the map for the pair containing the key (string) s.
|
||||
– If such a pair containing the key is not there, the operator:
|
||||
1. creates a pair containing the key and a default initialized value,
|
||||
2. inserts the pair into the map in the appropriate position, and
|
||||
|
||||
Reference in New Issue
Block a user