resize vs reserve in std::vector and std::basic_string
Use `resize` to change how many elements exist. Use `reserve` to make room for future elements without changing the current size or contents.
CKTIS / Knowledge base
Programming syntax, operators and concepts. Find an explanation, explore an example and return to learning.
Use `resize` to change how many elements exist. Use `reserve` to make room for future elements without changing the current size or contents.
Use `auto [it, inserted]` to name the iterator and insertion flag returned by `std::map::insert` when inserting a single value without a position hint.
The `&&` declarator forms an rvalue reference type. Overload resolution still depends on expression category: the name of a variable declared `T&&` is an lvalue when passed as an argument.
These three patterns separate object identity and lifetime, the choice of which implementation to construct, and the addition of behavior behind an unchanged interface.
C++23 specifies storage duration and object lifetime rules, not physical placement. An automatic object can contain a `std::string` that dynamically allocates character storage.
Learn how to read names such as `std::cout` and `std::chrono::seconds`, make declarations available, and shorten names safely. Curriculum examples show what the namespace controls and what comes from each API’s own contract.
In C++23, this deprecated overload atomically copies a `std::shared_ptr` and lets you choose the memory order of the load.
The legacy overload publishes a std::shared_ptr with a release store and pairs with an acquire load. It is still in C++23; prefer std::atomic<std::shared_ptr<T>> in new code.
`setf` changes a stream's formatting flags. Use `std::ios::boolalpha` to print `bool` values as `true` and `false` in the classic locale.
Choose the `append` overload that matches your source: a whole string, a slice, or a buffer with a known length. Add separators explicitly and preserve embedded nulls.
Choose the overload that matches your source. For a buffer with a known length, pass a pointer and a count to preserve embedded null characters.
Build a string containing exactly `count` copies of `ch`. Use parentheses to select the fill overload rather than the initializer-list overload.
Pair `data()` with `size()` to preserve embedded zeros, and check the string’s bounds and lifetime before using the pointer.
Find the first match at or after a starting position, check for `npos`, and use an explicit pattern length when a buffer may contain embedded nulls.
Test a `find` result against `npos` before using it as an index. In the count parameter of `substr`, the same constant means to take the rest of the string.
Append one code unit to a string, directly or through an output iterator. Learn what growth can invalidate and how to preserve embedded nulls in bounded data.
`reserve(n)` makes room for future elements without changing how many exist. `resize(n)` changes the element count in both strings and vectors.
`size()` counts stored elements, not user-perceived characters. Use it to bound copies, check indices, and trim fields without unsigned wraparound.
Extract part of a string into a new owning string. Learn how the length is capped, when the start position throws, and what the C++23 rvalue overload changes.
The path slash operator returns a new path without changing either operand. The right operand’s syntax determines whether it appends to or replaces some or all of the left path.
Set the `std::ios::boolalpha` flag with `setf` to read and write `bool` values using the stream’s locale-specific names rather than numeric representations.
Learn when copying bytes preserves an object's value in C++23, and why that guarantee does not make byte copying a substitute for copying arbitrary objects.
Use `std::nullopt` to return or set an empty `std::optional` without confusing a missing result with a valid value such as zero.
Check at compile time whether the implementation’s `float` conforms to IEEE 754, separately from any storage-size or binary-format requirements.