listings-Chap24-README.md
Listings of Chap24.docx
This is linked summary of the listings. You can also view it on one page.
- Listing 24.1: For parts of containers, use a pair of iteratos or if you use C++23, ranges.
- Book: lst-0007-book.cpp
- Godbolt: lst-0007-godb.cpp, https://godbolt.org/z/qGrYPrqYn
- Listing 24.2: A pair of iterators defines a range of elements.
- Book: lst-0008-book.cpp
- Godbolt: lst-0008-godb.cpp, https://godbolt.org/z/M1d9jEP74
- Listing 24.3: Indirectly uses begin() and end() of the container.
- Book: lst-0009-book.cpp
- Godbolt: lst-0009-godb.cpp, https://godbolt.org/z/Yv3vcxPv5
- GodboltId:babK485oT
- Book: lst-0010-book.cpp
- Godbolt: lst-0010-godb.cpp, https://godbolt.org/z/babK485oT
- GodboltId:nj14e6Wq4
- Book: lst-0011-book.cpp
- Godbolt: lst-0011-godb.cpp, https://godbolt.org/z/nj14e6Wq4
- Listing 24.4: One algorithm after another with iterators.
- Book: lst-0012-book.cpp
- Godbolt: lst-0012-godb.cpp, https://godbolt.org/z/arfee3TTP
- Listing 24.5: A pipeline of views instead of algorithms.
- Book: lst-0013-book.cpp
- Godbolt: lst-0013-godb.cpp, https://godbolt.org/z/TG1qd31Mq
- Listing 24.8: For “const” objects, “begin()” and “end()” return a “const_iterator”.
- Book: lst-0015-book.cpp
- Godbolt: lst-0015-godb.cpp, https://godbolt.org/z/dxb98rWzo
- Listing 24.9: Iterator adapters change the behavior of operations.
- Book: lst-0019-book.cpp
- Godbolt: lst-0019-godb.cpp, https://godbolt.org/z/7xYcqqh68
- Listing 24.10: A (too) simple allocator and its usage.
- Book: lst-0020-book.cpp
- Godbolt: lst-0020-godb.cpp, https://godbolt.org/z/3dYhh6n5f
- Listing 24.11: Type aliases are sometimes clearer than the concrete types.
- Book: lst-0022-book.cpp
- Godbolt: lst-0022-godb.cpp, https://godbolt.org/z/c97qra3M7
- Listing 24.12: This is the template for the example listings in this section for vector.
- Book: lst-0023-book.cpp
- Godbolt: lst-0023-godb.cpp, https://godbolt.org/z/WK86eaz1x
- Listing 24.13: The default constructor initializes an empty vector.
- Book: lst-0024-book.cpp
- Godbolt: lst-0024-godb.cpp, https://godbolt.org/z/7zMP5fo7c
- Listing 24.14: Copying a vector using the constructor or implicitly.
- Book: lst-0025-book.cpp
- Godbolt: lst-0025-godb.cpp, https://godbolt.org/z/cE6G1aErs
- Listing 24.15: For return values, the compiler can often also move.
- Book: lst-0026-book.cpp
- Godbolt: lst-0026-godb.cpp, https://godbolt.org/z/fv5WEe97M
- Listing 24.16: Using an initializer list to prefill a vector. Pay attention to the correct types in the list.
- Book: lst-0027-book.cpp
- Godbolt: lst-0027-godb.cpp, https://godbolt.org/z/6rG393Wa5
- Listing 24.17: Copy values from any other container or C-array during initialization.
- Book: lst-0028-book.cpp
- Godbolt: lst-0028-godb.cpp, https://godbolt.org/z/h8Pze74ME
- Listing 24.18: Preinitializing with a fixed number of values is (almost) only available with “vector”.
- Book: lst-0029-book.cpp
- Godbolt: lst-0029-godb.cpp, https://godbolt.org/z/addobjfsK
- Listing 24.19: You can assign to “vector” or reinitialize it later with “assign”.
- Book: lst-0030-book.cpp
- Godbolt: lst-0030-godb.cpp, https://godbolt.org/z/Yb4jEdaKj
- Listing 24.20: With “begin”, “end”, and their relatives, you get iterators.
- Book: lst-0031-book.cpp
- Godbolt: lst-0031-godb.cpp, https://godbolt.org/z/freaK1Pea
- Listing 24.21: With “vector” iterators, you can perform random access.
- Book: lst-0032-book.cpp
- Godbolt: lst-0032-godb.cpp, https://godbolt.org/z/srczx737G
- Listing 24.22: The normal case is access with [], because you already check the boundaries elsewhere.
- Book: lst-0033-book.cpp
- Godbolt: lst-0033-godb.cpp, https://godbolt.org/z/jjWx3d375
- Listing 24.23: Constant container returns constant reference.
- Book: lst-0034-book.cpp
- Godbolt: lst-0034-godb.cpp, https://godbolt.org/z/jGx51caTj
- Listing 24.24: insert shifts all elements back by one here.
- Book: lst-0035-book.cpp
- Godbolt: lst-0035-godb.cpp, https://godbolt.org/z/s9P1Y6jhv
- Listing 24.25: Use data() as an interface to C.
- Book: lst-0036-book.cpp
- Godbolt: lst-0036-godb.cpp, https://godbolt.org/z/Kx1KcP65T
- Listing 24.26: How to use span.
- Book: lst-0041-book.cpp
- Godbolt: lst-0041-godb.cpp, https://godbolt.org/z/h7jf4KM1q
- Listing 24.27: These are spans with fixed sizes.
- Book: lst-0044-book.cpp
- Godbolt: lst-0044-godb.cpp, https://godbolt.org/z/a3qqr1YPM
- Listing 24.28: You can also write through a “span”.
- Book: lst-0046-book.cpp
- Godbolt: lst-0046-godb.cpp, https://godbolt.org/z/6Ez5GdozG
- Listing 24.29: This is how the “mdspan” added in C++23 works.
- Book: lst-0047-book.cpp
- Godbolt: lst-0047-godb.cpp, https://godbolt.org/z/n8jn7v5vb
- Listing 24.30: Typically, you append at the end in vector.
- Book: lst-0048-book.cpp
- Godbolt: lst-0048-godb.cpp, https://godbolt.org/z/n6En49dsh
- GodboltId:6836bdczd
- Book: lst-0051-book.cpp
- Godbolt: lst-0051-godb.cpp, https://godbolt.org/z/6836bdczd
- Listing 24.31: erase with two parameters deletes an entire range.
- Book: lst-0052-book.cpp
- Godbolt: lst-0052-godb.cpp, https://godbolt.org/z/fe8bev9Y9
- Listing 24.32: Operations for the size and capacity of a “vector”.
- Book: lst-0054-book.cpp
- Godbolt: lst-0054-godb.cpp, https://godbolt.org/z/YzGK1WazE
- GodboltId:Y1W8WfxYn
- Book: lst-0055-book.cpp
- Godbolt: lst-0055-godb.cpp, https://godbolt.org/z/Y1W8WfxYn
- Listing 24.33: Different “arrays” as parameters require template programming.
- Book: lst-0056-book.cpp
- Godbolt: lst-0056-godb.cpp, https://godbolt.org/z/nqbKT9GEa
- Listing 24.34: Template for the example listings of this section on “array”.
- Book: lst-0057-book.cpp
- Godbolt: lst-0057-godb.cpp, https://godbolt.org/z/PrM4fGcWo
- Listing 24.36: “array” supports “get” from “tuple”.
- Book: lst-0058-book.cpp
- Godbolt: lst-0058-godb.cpp, https://godbolt.org/z/KrbTrx1aG
- Listing 24.37: An array supports structured binding.
- Book: lst-0059-book.cpp
- Godbolt: lst-0059-godb.cpp, https://godbolt.org/z/h6nrPYWfM
- Listing 24.38: Arrays can be compared lexicographically.
- Book: lst-0060-book.cpp
- Godbolt: lst-0060-godb.cpp, https://godbolt.org/z/j6EarszMT
- Listing 24.39: We remove pairs from the front and back and compare.
- Book: lst-0061-book.cpp
- Godbolt: lst-0061-godb.cpp, https://godbolt.org/z/Ee5anM9as
- Listing 24.40: What the “deque” can do!
- Book: lst-0063-book.cpp
- Godbolt: lst-0063-godb.cpp, https://godbolt.org/z/d3ExcWP4a
- Listing 24.41: “splice” is the specialty of “list” and efficiently connects two lists.
- Book: lst-0064-book.cpp
- Godbolt: lst-0064-godb.cpp, https://godbolt.org/z/so5Gebb4M
- Listing 24.42: You can use “before_begin” as an argument for “insert_after”.
- Book: lst-0065-book.cpp
- Godbolt: lst-0065-godb.cpp, https://godbolt.org/z/9b9fY4Mve
- Listing 24.43: “erase_after” can delete a range of elements.
- Book: lst-0066-book.cpp
- Godbolt: lst-0066-godb.cpp, https://godbolt.org/z/andfoh18z
- Listing 24.44: “splice_after” can very efficiently merge lists.
- Book: lst-0067-book.cpp
- Godbolt: lst-0067-godb.cpp, https://godbolt.org/z/Ka47W5Wjh
- Listing 24.45: This is the template for the example listings in this section on “set”.
- Book: lst-0068-book.cpp
- Godbolt: lst-0068-godb.cpp, https://godbolt.org/z/7b8GaE9xT
- Listing 24.46: If you define the comparison operation incorrectly, then “set” will no longer work.
- Book: lst-0070-book.cpp
- Godbolt: lst-0070-godb.cpp, https://godbolt.org/z/jfdh3ddsz
- Listing 24.47: The comparison operation can group elements.
- Book: lst-0071-book.cpp
- Godbolt: lst-0071-godb.cpp, https://godbolt.org/z/xdPc51q5s
- Listing 24.48: A custom spaceship operator for “set” compatibility.
- Book: lst-0072-book.cpp
- Godbolt: lst-0072-godb.cpp, https://godbolt.org/z/W9sK4jnzK
- Listing 24.49: There are various ways to specify a comparator.
- Book: lst-0074-book.cpp
- Godbolt: lst-0074-godb.cpp, https://godbolt.org/z/c8a9qPoPP
- Listing 24.50: There are again several ways to specify elements when constructing.
- Book: lst-0075-book.cpp
- Godbolt: lst-0075-godb.cpp, https://godbolt.org/z/oc9xxev95
- GodboltId:1dTPEd3nP
- Book: lst-0076-book.cpp
- Godbolt: lst-0076-godb.cpp, https://godbolt.org/z/1dTPEd3nP
- GodboltId:8eGvWY45o
- Book: lst-0077-book.cpp
- Godbolt: lst-0077-godb.cpp, https://godbolt.org/z/8eGvWY45o
- Listing 24.51: Instead of »assign« you can use the copy-and-swap idiom.
- Book: lst-0078-book.cpp
- Godbolt: lst-0078-godb.cpp, https://godbolt.org/z/ozMY86ns8
- Listing 24.52: To insert a single element, use “insert” or “emplace”.
- Book: lst-0079-book.cpp
- Godbolt: lst-0079-godb.cpp, https://godbolt.org/z/8nj7vfeba
- Listing 24.53: You can reuse the return value when inserting sorted ranges.
- Book: lst-0080-book.cpp
- Godbolt: lst-0080-godb.cpp, https://godbolt.org/z/qzaj79bor
- Listing 24.54: Use the same insert for sequential and associative containers.
- Book: lst-0081-book.cpp
- Godbolt: lst-0081-godb.cpp, https://godbolt.org/z/h1G8v86jv
- Listing 24.55: You can also insert multiple elements.
- Book: lst-0082-book.cpp
- Godbolt: lst-0082-godb.cpp, https://godbolt.org/z/rTvovhr5M
- Listing 24.56: These are the “set” search functions.
- Book: lst-0083-book.cpp
- Godbolt: lst-0083-godb.cpp, https://godbolt.org/z/MW74f97f7
- Listing 24.57: You can search with nonidentical keys if they are equivalent.
- Book: lst-0084-book.cpp
- Godbolt: lst-0084-godb.cpp, https://godbolt.org/z/zEsxd8e7G
- Listing 24.58: “erase” deletes one or more elements.
- Book: lst-0086-book.cpp
- Godbolt: lst-0086-godb.cpp, https://godbolt.org/z/dh8Yqx6c4
- Listing 24.59: Using [] may create an entry as a side effect.
- Book: lst-0087-book.cpp
- Godbolt: lst-0087-godb.cpp, https://godbolt.org/z/zWa58dYYq
- Listing 24.60: This is the template for the example listings in this section on “map”.
- Book: lst-0088-book.cpp
- Godbolt: lst-0088-godb.cpp, https://godbolt.org/z/K834js588
- Listing 24.61: You can also provide a custom comparison function for a “map”.
- Book: lst-0089-book.cpp
- Godbolt: lst-0089-godb.cpp, https://godbolt.org/z/W6Mfnxj1W
- Listing 24.62: The initializer list must contain “pair” elements.
- Book: lst-0091-book.cpp
- Godbolt: lst-0091-godb.cpp, https://godbolt.org/z/vW4xsMPq7
- Listing 24.63: You specify a single new element as a pair.
- Book: lst-0092-book.cpp
- Godbolt: lst-0092-godb.cpp, https://godbolt.org/z/Y1ebrxb8T
- Listing 24.64: Automatically create and immediately overwrite with “operator[]”.
- Book: lst-0093-book.cpp
- Godbolt: lst-0093-godb.cpp, https://godbolt.org/z/7h3vsWvYG
- Listing 24.65: You can change the value of a target.
- Book: lst-0094-book.cpp
- Godbolt: lst-0094-godb.cpp, https://godbolt.org/z/8948besY8
- Listing 24.66: Iterators of “map” are of type “pair”.
- Book: lst-0095-book.cpp
- Godbolt: lst-0095-godb.cpp, https://godbolt.org/z/16T5dob75
- Listing 24.67: You cannot use “operator[]” on a “const map”.
- Book: lst-0096-book.cpp
- Godbolt: lst-0096-godb.cpp, https://godbolt.org/z/Eqsjex4Ta
- Listing 24.68: This is the template for the example listings in this section on “multiset”.
- Book: lst-0097-book.cpp
- Godbolt: lst-0097-godb.cpp, https://godbolt.org/z/aMj87neG6
- Listing 24.69: Entries are sorted, and duplicates are retained.
- Book: lst-0098-book.cpp
- Godbolt: lst-0098-godb.cpp, https://godbolt.org/z/aqGTKdbMP
- Listing 24.70: The “multiset” search functions find the range of matching elements.
- Book: lst-0099-book.cpp
- Godbolt: lst-0099-godb.cpp, https://godbolt.org/z/rWGrhbYa7
- Listing 24.71: This is the template for the listings in this section on “multimap”.
- Book: lst-0100-book.cpp
- Godbolt: lst-0100-godb.cpp, https://godbolt.org/z/bxW63jPor
- Listing 24.72: All entries end up in the “multimap”.
- Book: lst-0101-book.cpp
- Godbolt: lst-0101-godb.cpp, https://godbolt.org/z/6no9dW64d
- Listing 24.73: “insert” and “emplace” in the “multimap”.
- Book: lst-0102-book.cpp
- Godbolt: lst-0102-godb.cpp, https://godbolt.org/z/dj68ej97r
- Listing 24.74: “erase” with a key can delete multiple elements.
- Book: lst-0103-book.cpp
- Godbolt: lst-0103-godb.cpp, https://godbolt.org/z/TbvKTn4Px
- Listing 24.75: Never operate unordered associative containers with a bad hash function.
- Book: lst-0104-book.cpp
- Godbolt: lst-0104-godb.cpp, https://godbolt.org/z/1MYhxb7rq
- Listing 24.76: Twice as many elements with a poor hash function means four times the runtime.
- Book: lst-0105-book.cpp
- Godbolt: lst-0105-godb.cpp, https://godbolt.org/z/cc3KohPrY
- Listing 24.77: This is the template for the example listings on “unordered_set”.
- Book: lst-0106-book.cpp
- Godbolt: lst-0106-godb.cpp, https://godbolt.org/z/38TbjGhn6
- Listing 24.78: An “unordered_set” with custom comparison and hash function.
- Book: lst-0107-book.cpp
- Godbolt: lst-0107-godb.cpp, https://godbolt.org/z/TY5x54efE
- Listing 24.79: These are the ways to initialize an “unordered_set”.
- Book: lst-0109-book.cpp
- Godbolt: lst-0109-godb.cpp, https://godbolt.org/z/1j6PMojWr
- Listing 24.80: Insertion into an “unordered_set”.
- Book: lst-0110-book.cpp
- Godbolt: lst-0110-godb.cpp, https://godbolt.org/z/js3MeoccM
- Listing 24.81: Deleting preserves the order of the remaining elements.
- Book: lst-0111-book.cpp
- Godbolt: lst-0111-godb.cpp, https://godbolt.org/z/hee331WGe
- Listing 24.82: You can access “unordered_set” by bucket.
- Book: lst-0112-book.cpp
- Godbolt: lst-0112-godb.cpp, https://godbolt.org/z/39v4aqqd1
- Listing 24.83: This is the template for the example listings on “unordered_map”.
- Book: lst-0113-book.cpp
- Godbolt: lst-0113-godb.cpp, https://godbolt.org/z/T5KKWx4z4
- Listing 24.84: A custom data type as a key in an “unordered_map”.
- Book: lst-0114-book.cpp
- Godbolt: lst-0114-godb.cpp, https://godbolt.org/z/cdeGW6a3P
- Listing 24.85: You can also use only part of your object as a key.
- Book: lst-0115-book.cpp
- Godbolt: lst-0115-godb.cpp, https://godbolt.org/z/Ed88znPn9
- GodboltId:cveT8Ej7q
- Book: lst-0117-book.cpp
- Godbolt: lst-0117-godb.cpp, https://godbolt.org/z/cveT8Ej7q
- Listing 24.86: These are the ways to initialize an “unordered_multiset”.
- Book: lst-0118-book.cpp
- Godbolt: lst-0118-godb.cpp, https://godbolt.org/z/bvehE8hse
- Listing 24.87: For the “multi” variants, “count” makes perfect sense.
- Book: lst-0119-book.cpp
- Godbolt: lst-0119-godb.cpp, https://godbolt.org/z/a8cGxr9s9
- Listing 24.88: Adapters work with interchangeable implementations.
- Book: lst-0120-book.cpp
- Godbolt: lst-0120-godb.cpp, https://godbolt.org/z/6MKMj3Pxv
- Listing 24.89: “string” is more suitable for texts than “vector
“. - Book: lst-0121-book.cpp
- Godbolt: lst-0121-godb.cpp, https://godbolt.org/z/P7zbrh71d
- Listing 24.90: A “bitset” example.
- Book: lst-0122-book.cpp
- Godbolt: lst-0122-godb.cpp, https://godbolt.org/z/5e3c88K8j
- GodboltId:KPP16bdK9
- Book: lst-0123-book.cpp
- Godbolt: lst-0123-godb.cpp, https://godbolt.org/z/KPP16bdK9
- GodboltId:r11584Wef
- Book: lst-0124-book.cpp
- Godbolt: lst-0124-godb.cpp, https://godbolt.org/z/r11584Wef
- GodboltId:chnj64dva
- Book: lst-0125-book.cpp
- Godbolt: lst-0125-godb.cpp, https://godbolt.org/z/chnj64dva
- GodboltId:PeG4K7Edc
- Book: lst-0126-book.cpp
- Godbolt: lst-0126-godb.cpp, https://godbolt.org/z/PeG4K7Edc
- GodboltId:ehP47vW9o
- Book: lst-0127-book.cpp
- Godbolt: lst-0127-godb.cpp, https://godbolt.org/z/ehP47vW9o
- GodboltId:hf8rqz5x5
- Book: lst-0128-book.cpp
- Godbolt: lst-0128-godb.cpp, https://godbolt.org/z/hf8rqz5x5