diff --git a/content/cpp/concepts/unordered-set/terms/reserve/reserve.md b/content/cpp/concepts/unordered-set/terms/reserve/reserve.md new file mode 100644 index 00000000000..35f35f26d48 --- /dev/null +++ b/content/cpp/concepts/unordered-set/terms/reserve/reserve.md @@ -0,0 +1,106 @@ +--- +Title: 'reserve()' +Description: 'Requests capacity change for `std::unordered_set` so it can accommodate at least n elements without exceeding the maximum load factor.' +Subjects: + - 'Code Foundations' + - 'Computer Science' +Tags: + - 'Containers' + - 'Sets' + - 'STL' +CatalogContent: + - 'learn-c-plus-plus' + - 'paths/computer-science' +--- + +The **`reserve()`** method requests a capacity change for an [`std::unordered_set`](https://www.codecademy.com/resources/docs/cpp/unordered-set). It sets the number of buckets to the amount needed to accommodate at least `n` elements without exceeding the container’s `max_load_factor()`. Calling `reserve(n)` may trigger a rehash; if it does, all iterators are invalidated, but references and pointers to elements remain valid. This operation does not sort or otherwise order elements. + +## Syntax + +```pseudo +unordered_set_name.reserve(n); +``` + +**Parameters:** + +- `n` (size_type): Minimum number of elements the container should be able to accommodate without exceeding `max_load_factor()`. + +**Return value:** + +This method doesn't return anything (`void`). + +## Example + +This example shows bucket capacity before and after `reserve()`, and demonstrates that elements remain accessible with the same unordered iteration semantics: + +```cpp +#include +#include +using namespace std; + +int main() { + unordered_set numbers = {10, 20, 30}; + + cout << "Buckets before: " << numbers.bucket_count() << "\n"; + + numbers.reserve(10); + + cout << "Buckets after: " << numbers.bucket_count() << "\n"; + cout << "Max load factor: " << numbers.max_load_factor() << "\n"; + cout << "Load factor: " << numbers.load_factor() << "\n"; + + for (int i = 1; i <= 7; ++i) { + numbers.insert(i * 5); + } + + cout << "Size: " << numbers.size() << "\n"; + cout << "Buckets final: " << numbers.bucket_count() << "\n"; + + return 0; +} +``` + +The output of this code is: + +```shell +Buckets before: 3 +Buckets after: 11 +Max load factor: 1 +Load factor: 0.272727 +Size: 7 +Buckets final: 11 +``` + +## Codebyte Example + +This example demonstrates `reserve()` with strings, and prints capacity-related information: + +```codebyte/cpp +#include +#include +#include +using namespace std; + +int main() { + unordered_set words = {"alpha", "beta", "gamma"}; + + cout << "Before: buckets=" << words.bucket_count() << ", size=" << words.size() << "\n"; + + words.reserve(10); + + cout << "After: buckets=" << words.bucket_count() << ", size=" << words.size() << "\n"; + + words.insert("delta"); + words.insert("epsilon"); + words.insert("zeta"); + + cout << "Load factor: " << words.load_factor() << "\n"; + cout << "Max load factor: " << words.max_load_factor() << "\n"; + + for (const auto& w : words) { + cout << w << "\n"; + } + + return 0; +} +``` diff --git a/content/javascript/concepts/dom-manipulation/terms/querySelectorAll/querySelectorAll.md b/content/javascript/concepts/dom-manipulation/terms/querySelectorAll/querySelectorAll.md deleted file mode 100644 index 9d1e2157596..00000000000 --- a/content/javascript/concepts/dom-manipulation/terms/querySelectorAll/querySelectorAll.md +++ /dev/null @@ -1,56 +0,0 @@ ---- -Title: '.querySelectorAll()' -Description: 'Returns a static (non-live) NodeList of all elements in the document that match the given CSS selectors.' -Subjects: - - 'Code Foundations' - - 'Web Development' -Tags: - - 'Methods' - - 'Node' - - 'Selectors' -CatalogContent: - - 'introduction-to-javascript' - - 'paths/front-end-engineer-career-path' ---- - -In JavaScript, the **`.querySelectorAll()`** method under the `document` object returns a static (not live) `NodeList` of all elements that match the given group of [selectors](https://www.codecademy.com/resources/docs/css/selectors). - -## Syntax - -```pseudo -document.querySelectorAll(selectors); -``` - -- `selectors`: Represents a string containing one or more CSS selectors used to match elements in the document. It follows the same rules as CSS selectors and can include: - - Type selectors (`div`, `p`, `span`) - - Class selectors (`.class-name`) - - ID selectors (`#id-name`) - - Attribute selectors (`[type="text"]`, `[disabled]`) - - Combinations (`div p`, `.container > p`, `ul > li:first-child`) - -## Examples - -### Example 1 - -In this example, a `NodeList` of all `

` elements in the document is obtained: - -```js -const matches = document.querySelectorAll('p'); -``` - -### Example 2 - -The following example returns a list of all `

` elements in the document with a class of either `note` or `alert`: - -```js -const matches = document.querySelectorAll('div.note, div.alert'); -``` - -### Example 3 - -In this example, a list of `

` elements is obtained, whose immediate parent is a `

` with the class `highlighted`, and which are inside a container with the ID `test`: - -```js -const container = document.querySelector('#test'); -const matches = container.querySelectorAll('div.highlighted > p'); -```