From cbd7b8b4813f4d36c0da9632f70d1c6d573c38bb Mon Sep 17 00:00:00 2001 From: Fureeish Date: Sat, 29 Mar 2025 01:12:45 +0100 Subject: [PATCH 1/9] Ignore JetBrains' IDEs project settings dir --- .gitignore | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 7cf84ae1f..cdae3b7e9 100644 --- a/.gitignore +++ b/.gitignore @@ -21,4 +21,7 @@ yarn-error.log* # Custom .vscode/ -*.drawio.* \ No newline at end of file +*.drawio.* + +# JetBrains' IDEs +.idea \ No newline at end of file From 9a13260d5d0af93cb7533a4eccc485b16227c292 Mon Sep 17 00:00:00 2001 From: Fureeish Date: Sat, 29 Mar 2025 01:13:10 +0100 Subject: [PATCH 2/9] Reorder examples and add short explanation at the very top --- content/docs/std/algo/ranges/find.mdx | 333 ++++++++++++++++++++++---- 1 file changed, 289 insertions(+), 44 deletions(-) diff --git a/content/docs/std/algo/ranges/find.mdx b/content/docs/std/algo/ranges/find.mdx index cd5a96e8f..132305f8c 100644 --- a/content/docs/std/algo/ranges/find.mdx +++ b/content/docs/std/algo/ranges/find.mdx @@ -1,6 +1,6 @@ --- title: ranges::find algorithm -sidebar_label: ranges::find( ) +sidebar_label: ranges::find() description: ranges::find algorithm C++ documentation hide_title: true tags: [ranges, algorithm, find, search] @@ -19,6 +19,17 @@ import Signature_SinceCpp20_Simplified from './_codes/find/since-cpp20-simplifie # std::ranges::find() algorithm +### Explanation + +Used to _locate_ an element inside a range, providing direct access to it through the returned iterator. + +If there are multiple values that this algorithm could locate, it returns an iterator to the one that it first encounters. +This is determined by the passed range / iterators behavior. + +If there is no suitable value, i.e., if the finding process fails, the algorithm returns an iterator equal to the end of the supplied range. + +### Signature + , @@ -102,70 +113,304 @@ inline constexpr find_fn find; ### Examples - -
+#### Finding and printing the found value -```cpp title='Main.cpp' showLineNumbers +```cpp showLineNumbers #include #include -#include +#include int main() { - namespace ranges = std::ranges; + const std::vector values{2, 4, 6, 1, 8, 10}; + const auto iteratorTo1 = std::ranges::find(values, 1); + std::cout << *iteratorTo1; +} +``` - const int n1 = 3; - const int n2 = 5; - const auto v = {4, 1, 3, 2}; +```plaintext title="Output" +1 +``` - if (ranges::find(v, n1) != v.end()) - std::cout << "v contains: " << n1 << '\n'; - else - std::cout << "v does not contain: " << n1 << '\n'; +#### Finding and removing the found value - if (ranges::find(v.begin(), v.end(), n2) != v.end()) - std::cout << "v contains: " << n2 << '\n'; - else - std::cout << "v does not contain: " << n2 << '\n'; +```cpp showLineNumbers +#include +#include +#include +#include - auto is_even = [](int x) { return x % 2 == 0; }; +int main() +{ + std::vector values{2, 4, 6, 1, 8, 10}; + const auto iteratorTo1 = std::ranges::find(values, 1); + values.erase(iteratorTo1); - if (auto result = ranges::find_if(v.begin(), v.end(), is_even); result != v.end()) - std::cout << "First even element in v: " << *result << '\n'; - else - std::cout << "No even elements in v\n"; + std::string text = "hello, there!"; + const auto iteratorToComma = std::ranges::find(text, ','); + text.erase(iteratorToComma); - if (auto result = ranges::find_if_not(v, is_even); result != v.end()) - std::cout << "First odd element in v: " << *result << '\n'; - else - std::cout << "No odd elements in v\n"; + for (const int element : values) std::cout << element << ' '; + std::cout << '\n'; + std::cout << text; +} +``` + +```plaintext title="Output" +2 4 6 8 10 +hello there! +``` + +#### Finding and changing the found value + +```cpp showLineNumbers +#include +#include +#include + +int main() +{ + auto println = [](std::vector const& vec) + { + for (const int element : vec) std::cout << element << ' '; + std::cout << '\n'; + }; + + std::vector values{2, 4, 5, 8, 10}; + println(values); + auto iteratorTo5 = std::ranges::find(values, 5); + *iteratorTo5 = 6; + println(values); +} +``` + +```plaintext title="Output" +2 4 5 8 10 +2 4 6 8 10 +``` + +#### Using a projection to find a value with a given characteristic #1 + +```cpp showLineNumbers +#include +#include +#include +#include + +int main() +{ + const std::vector words = { + "some", "of", "these", "words", "are", + "of", "the", "same", "lengths" + }; + + const auto bySize = [](const std::string& s) { + return s.size(); + }; + + const auto ofSize5 = std::ranges::find(words, 5, bySize); + std::cout << *ofSize5; +} +``` + +```plaintext title="Output" +these +``` + +#### Using a projection to find a value with a given characteristic #2 + +```cpp showLineNumbers +#include +#include +#include + +int main() +{ + const std::vector numbers = { + 5, 6, 7, 8 + }; + + const auto byModulus5 = [](const int n) { + return n % 5; + }; + + const auto withRemainder2 = std::ranges::find(numbers, 2, byModulus5); + std::cout << *withRemainder2; +} +``` + +```plaintext title="Output" +7 +``` + +#### Failing to find a value safely + +```cpp showLineNumbers +#include +#include +#include + +int main() +{ + const std::vector onlyEvens = { + 2, 4, 6, 8, 10 + }; - auto divides_13 = [](int x) { return x % 13 == 0; }; + auto positionOf1 = std::ranges::find(onlyEvens, 1); - if (auto result = ranges::find_if(v, divides_13); result != v.end()) - std::cout << "First element divisible by 13 in v: " << *result << '\n'; + if (positionOf1 != onlyEvens.end()) + { + std::cout << "odd element found: " << *positionOf1; + } else - std::cout << "No elements in v are divisible by 13\n"; + { + std::cout << "no odd elements found"; + } +} +``` + +```plaintext title="Output" +no even elements found +``` + +:::info +Because finding _may_ fail, we should almost always compare the returned iterator with the _end_ of the supplied range before we use it. + ::: + +#### Failing to find a value unsafely - if (auto result = ranges::find_if_not(v.begin(), v.end(), divides_13); - result != v.end()) - std::cout << "First element indivisible by 13 in v: " << *result << '\n'; +```cpp +#include +#include +#include + +int main() +{ + const std::vector onlyEvens = { + 2, 4, 6, 8, 10 + }; + + auto positionOf1 = std::ranges::find(onlyEvens, 1); + + std::cout << "odd element found: " << *positionOf1; +} +``` + +:::danger +Because `onlyEvens` does **not** contain the searched-for value `1`, it returns the _end_ of the supplied range, i.e., `onlyEvens.end()`. Dereferencing it invokes undefined behavior. +::: + +#### Finding the last (instead of the first) suitable value thanks to reverse iterators + +```cpp showLineNumbers +#include +#include +#include +#include + +int main() +{ + auto println = [](std::vector const& vec) + { + for (const int element : vec) std::cout << element << ' '; + std::cout << '\n'; + }; + + std::vector numbers = { + 1, 2, 3, 2, 1 + }; + + const auto first1 = std::ranges::find(numbers, 1); + const auto last1 = std::ranges::find(numbers.rbegin(), numbers.rend(), 1); + + println(numbers); + *first1 = 9; + *last1 = 7; + println(numbers); + + // can also use reverse_view from + auto numbersReversed = numbers | std::views::reverse; + const auto last2 = std::ranges::find(numbersReversed, 2); + *last2 = 0; + println(numbers); +} +``` + +```plaintext title="Output" +1 2 3 2 1 +9 2 3 2 7 +9 2 3 0 7 +``` + +#### Failing to find a value safely when dealing with an iterator-pair subrange + +```cpp showLineNumbers +#include +#include +#include + +int main() +{ + const std::vector numbers = { + 1, 2, 3, 4, 5, 0, 6 + }; + + const auto zeroInFirstThree = std::ranges::find( + numbers.begin(), numbers.begin() + 3, 0 + ); + + if (zeroInFirstThree != numbers.begin() + 3) + { + std::cout << "there is a 0 in the first three elements"; + } else - std::cout << "All elements in v are divisible by 13\n"; + { + std::cout << "there are no 0s in the first three elements"; + } } ``` -
-
+```plaintext title="Output" +there are no 0s in the first three elements +``` + +:::info +When dealing with an iterator-pair subrange, the _end_ of that subrange is the second iterator passed to the algorithm. Thus, in order to correctly test for the validity of the returned iterator, it needs to be compared with the second argument of the algorithm. +::: + + +#### Failing to find a value when dealing with an iterator-pair subrange unsafely + +```cpp showLineNumbers +#include +#include +#include + +int main() +{ + const std::vector numbers = { + 1, 2, 3, 4, 5, 0, 6 + }; + + const auto zeroInFirstThree = std::ranges::find( + numbers.begin(), numbers.begin() + 3, 0 + ); + + if (zeroInFirstThree != numbers.end()) + { + std::cout << "there is a 0 in the first three elements"; + } + else + { + std::cout << "there are no 0s in the first three elements"; + } +} +``` ```plaintext title="Output" -v contains: 3 -v does not contain: 5 -First even element in v: 4 -First odd element in v: 1 -No elements in v are divisible by 13 -First element indivisible by 13 in v: 4 +there is a 0 in the first three elements ``` -
-
+:::danger +Here, instead of comparing the resulting iterator with the correct _end_, it was compared to the `numbers.end()`, which it had no chance of reaching. Thus, it is not equal to it, mistakenly suggesting that there indeed does exist a `0` value in the first three elements of `numbers`. +::: From e38349353223f39699919eeaef8fdee8e4ba7b34 Mon Sep 17 00:00:00 2001 From: Fureeish Date: Sun, 30 Mar 2025 19:11:52 +0200 Subject: [PATCH 3/9] Introduce abbreviated examples with an optional full-code view --- ...dealing-with-an-iterator-pair-subrange.mdx | 25 ++ ...example-Failing-to-find-a-value-safely.mdx | 23 ++ ...ample-Failing-to-find-a-value-unsafely.mdx | 16 + ...ith-an-iterator-pair-subrange-unsafely.mdx | 25 ++ ...e-Finding-and-changing-the-found-value.mdx | 20 ++ ...e-Finding-and-printing-the-found-value.mdx | 12 + ...e-Finding-and-removing-the-found-value.mdx | 31 ++ ...able-value-thanks-to-reverse-iterators.mdx | 33 ++ ...d-a-value-with-a-given-characteristic1.mdx | 21 ++ ...d-a-value-with-a-given-characteristic2.mdx | 19 + content/docs/std/algo/ranges/find.mdx | 328 ++++++++---------- 11 files changed, 372 insertions(+), 181 deletions(-) create mode 100644 content/docs/std/algo/ranges/_codes/find/example-Failing-to-find-a-value-safely-when-dealing-with-an-iterator-pair-subrange.mdx create mode 100644 content/docs/std/algo/ranges/_codes/find/example-Failing-to-find-a-value-safely.mdx create mode 100644 content/docs/std/algo/ranges/_codes/find/example-Failing-to-find-a-value-unsafely.mdx create mode 100644 content/docs/std/algo/ranges/_codes/find/example-Failing-to-find-a-value-when-dealing-with-an-iterator-pair-subrange-unsafely.mdx create mode 100644 content/docs/std/algo/ranges/_codes/find/example-Finding-and-changing-the-found-value.mdx create mode 100644 content/docs/std/algo/ranges/_codes/find/example-Finding-and-printing-the-found-value.mdx create mode 100644 content/docs/std/algo/ranges/_codes/find/example-Finding-and-removing-the-found-value.mdx create mode 100644 content/docs/std/algo/ranges/_codes/find/example-Finding-the-last-suitable-value-thanks-to-reverse-iterators.mdx create mode 100644 content/docs/std/algo/ranges/_codes/find/example-Using-a-projection-to-find-a-value-with-a-given-characteristic1.mdx create mode 100644 content/docs/std/algo/ranges/_codes/find/example-Using-a-projection-to-find-a-value-with-a-given-characteristic2.mdx diff --git a/content/docs/std/algo/ranges/_codes/find/example-Failing-to-find-a-value-safely-when-dealing-with-an-iterator-pair-subrange.mdx b/content/docs/std/algo/ranges/_codes/find/example-Failing-to-find-a-value-safely-when-dealing-with-an-iterator-pair-subrange.mdx new file mode 100644 index 000000000..82148852f --- /dev/null +++ b/content/docs/std/algo/ranges/_codes/find/example-Failing-to-find-a-value-safely-when-dealing-with-an-iterator-pair-subrange.mdx @@ -0,0 +1,25 @@ +```cpp showLineNumbers +#include +#include +#include + +int main() +{ + const std::vector numbers = { + 1, 2, 3, 4, 5, 0, 6 + }; + + const auto zeroInFirstThree = std::ranges::find( + numbers.begin(), numbers.begin() + 3, 0 + ); + + if (zeroInFirstThree != numbers.begin() + 3) + { + std::cout << "there is a 0 in the first three elements"; + } + else + { + std::cout << "there are no 0s in the first three elements"; + } +} +``` \ No newline at end of file diff --git a/content/docs/std/algo/ranges/_codes/find/example-Failing-to-find-a-value-safely.mdx b/content/docs/std/algo/ranges/_codes/find/example-Failing-to-find-a-value-safely.mdx new file mode 100644 index 000000000..b962eaac5 --- /dev/null +++ b/content/docs/std/algo/ranges/_codes/find/example-Failing-to-find-a-value-safely.mdx @@ -0,0 +1,23 @@ +```cpp showLineNumbers +#include +#include +#include + +int main() +{ + const std::vector onlyEvens = { + 2, 4, 6, 8, 10 + }; + + auto positionOf1 = std::ranges::find(onlyEvens, 1); + + if (positionOf1 != onlyEvens.end()) + { + std::cout << "odd element found: " << *positionOf1; + } + else + { + std::cout << "no odd elements found"; + } +} +``` \ No newline at end of file diff --git a/content/docs/std/algo/ranges/_codes/find/example-Failing-to-find-a-value-unsafely.mdx b/content/docs/std/algo/ranges/_codes/find/example-Failing-to-find-a-value-unsafely.mdx new file mode 100644 index 000000000..dc9f3f09f --- /dev/null +++ b/content/docs/std/algo/ranges/_codes/find/example-Failing-to-find-a-value-unsafely.mdx @@ -0,0 +1,16 @@ +```cpp showLineNumbers +#include +#include +#include + +int main() +{ + const std::vector onlyEvens = { + 2, 4, 6, 8, 10 + }; + + auto positionOf1 = std::ranges::find(onlyEvens, 1); + + std::cout << "odd element found: " << *positionOf1; +} +``` \ No newline at end of file diff --git a/content/docs/std/algo/ranges/_codes/find/example-Failing-to-find-a-value-when-dealing-with-an-iterator-pair-subrange-unsafely.mdx b/content/docs/std/algo/ranges/_codes/find/example-Failing-to-find-a-value-when-dealing-with-an-iterator-pair-subrange-unsafely.mdx new file mode 100644 index 000000000..ff10058e5 --- /dev/null +++ b/content/docs/std/algo/ranges/_codes/find/example-Failing-to-find-a-value-when-dealing-with-an-iterator-pair-subrange-unsafely.mdx @@ -0,0 +1,25 @@ +```cpp showLineNumbers +#include +#include +#include + +int main() +{ + const std::vector numbers = { + 1, 2, 3, 4, 5, 0, 6 + }; + + const auto zeroInFirstThree = std::ranges::find( + numbers.begin(), numbers.begin() + 3, 0 + ); + + if (zeroInFirstThree != numbers.end()) + { + std::cout << "there is a 0 in the first three elements"; + } + else + { + std::cout << "there are no 0s in the first three elements"; + } +} +``` \ No newline at end of file diff --git a/content/docs/std/algo/ranges/_codes/find/example-Finding-and-changing-the-found-value.mdx b/content/docs/std/algo/ranges/_codes/find/example-Finding-and-changing-the-found-value.mdx new file mode 100644 index 000000000..54e2f4d3b --- /dev/null +++ b/content/docs/std/algo/ranges/_codes/find/example-Finding-and-changing-the-found-value.mdx @@ -0,0 +1,20 @@ +```cpp showLineNumbers +#include +#include +#include + +int main() +{ + const auto println = [](std::vector const& vec) + { + for (const int element : vec) std::cout << element << ' '; + std::cout << '\n'; + }; + + std::vector values{2, 4, 5, 8, 10}; + println(values); + auto iteratorTo5 = std::ranges::find(values, 5); + *iteratorTo5 = 6; + println(values); +} +``` \ No newline at end of file diff --git a/content/docs/std/algo/ranges/_codes/find/example-Finding-and-printing-the-found-value.mdx b/content/docs/std/algo/ranges/_codes/find/example-Finding-and-printing-the-found-value.mdx new file mode 100644 index 000000000..7e6534699 --- /dev/null +++ b/content/docs/std/algo/ranges/_codes/find/example-Finding-and-printing-the-found-value.mdx @@ -0,0 +1,12 @@ +```cpp showLineNumbers +#include +#include +#include + +int main() +{ + const std::vector values{2, 4, 6, 1, 8, 10}; + const auto iteratorTo1 = std::ranges::find(values, 1); + std::cout << *iteratorTo1; +} +``` \ No newline at end of file diff --git a/content/docs/std/algo/ranges/_codes/find/example-Finding-and-removing-the-found-value.mdx b/content/docs/std/algo/ranges/_codes/find/example-Finding-and-removing-the-found-value.mdx new file mode 100644 index 000000000..75413ef8f --- /dev/null +++ b/content/docs/std/algo/ranges/_codes/find/example-Finding-and-removing-the-found-value.mdx @@ -0,0 +1,31 @@ +```cpp showLineNumbers +#include +#include +#include +#include + +int main() +{ + const auto println = [](const std::vector& vec) + { + for (const int element : vec) std::cout << element << ' '; + std::cout << '\n'; + }; + + std::vector values{2, 4, 6, 1, 8, 10}; + std::string text = "hello, there!"; + + println(values); + std::cout << text << '\n'; + + const auto iteratorTo1 = std::ranges::find(values, 1); + values.erase(iteratorTo1); + + const auto iteratorToComma = std::ranges::find(text, ','); + text.erase(iteratorToComma); + + std::cout << '\n'; + println(values); + std::cout << text << '\n'; +} +``` \ No newline at end of file diff --git a/content/docs/std/algo/ranges/_codes/find/example-Finding-the-last-suitable-value-thanks-to-reverse-iterators.mdx b/content/docs/std/algo/ranges/_codes/find/example-Finding-the-last-suitable-value-thanks-to-reverse-iterators.mdx new file mode 100644 index 000000000..41abf9be8 --- /dev/null +++ b/content/docs/std/algo/ranges/_codes/find/example-Finding-the-last-suitable-value-thanks-to-reverse-iterators.mdx @@ -0,0 +1,33 @@ +```cpp showLineNumbers +#include +#include +#include +#include + +int main() +{ + const auto println = [](std::vector const& vec) + { + for (const int element : vec) std::cout << element << ' '; + std::cout << '\n'; + }; + + std::vector numbers = { + 1, 2, 3, 2, 1 + }; + + const auto first1 = std::ranges::find(numbers, 1); + const auto last1 = std::ranges::find(numbers.rbegin(), numbers.rend(), 1); + + println(numbers); + *first1 = 9; + *last1 = 7; + println(numbers); + + // can also use reverse_view from + auto numbersReversed = numbers | std::views::reverse; + const auto last2 = std::ranges::find(numbersReversed, 2); + *last2 = 0; + println(numbers); +} +``` \ No newline at end of file diff --git a/content/docs/std/algo/ranges/_codes/find/example-Using-a-projection-to-find-a-value-with-a-given-characteristic1.mdx b/content/docs/std/algo/ranges/_codes/find/example-Using-a-projection-to-find-a-value-with-a-given-characteristic1.mdx new file mode 100644 index 000000000..562e7e8bc --- /dev/null +++ b/content/docs/std/algo/ranges/_codes/find/example-Using-a-projection-to-find-a-value-with-a-given-characteristic1.mdx @@ -0,0 +1,21 @@ +```cpp showLineNumbers +#include +#include +#include +#include + +int main() +{ + const std::vector words = { + "some", "of", "these", "words", "are", + "of", "the", "same", "lengths" + }; + + const auto bySize = [](const std::string& s) { + return s.size(); + }; + + const auto ofSize5 = std::ranges::find(words, 5, bySize); + std::cout << *ofSize5; +} +``` \ No newline at end of file diff --git a/content/docs/std/algo/ranges/_codes/find/example-Using-a-projection-to-find-a-value-with-a-given-characteristic2.mdx b/content/docs/std/algo/ranges/_codes/find/example-Using-a-projection-to-find-a-value-with-a-given-characteristic2.mdx new file mode 100644 index 000000000..6e527df96 --- /dev/null +++ b/content/docs/std/algo/ranges/_codes/find/example-Using-a-projection-to-find-a-value-with-a-given-characteristic2.mdx @@ -0,0 +1,19 @@ +```cpp showLineNumbers +#include +#include +#include + +int main() +{ + const std::vector numbers = { + 5, 6, 7, 8 + }; + + const auto byModulus5 = [](const int n) { + return n % 5; + }; + + const auto withRemainder2 = std::ranges::find(numbers, 2, byModulus5); + std::cout << *withRemainder2; +} +``` \ No newline at end of file diff --git a/content/docs/std/algo/ranges/find.mdx b/content/docs/std/algo/ranges/find.mdx index 132305f8c..5f7f54bdc 100644 --- a/content/docs/std/algo/ranges/find.mdx +++ b/content/docs/std/algo/ranges/find.mdx @@ -11,11 +11,23 @@ cppreference_origin_rel: w/cpp/algorithm/ranges/find import SymbolTable, { Symbol } from "@site-comps/SymbolTable"; import Columns from "@site-comps/Columns"; import VersionTabs from "@site-comps/VersionTabs"; +import CustomCodeBlock from "@site-comps/CustomCodeBlock"; + {/* Codes */} -import Signature_SinceCpp20_Detailed from './_codes/find/since-cpp20-detailed.mdx'; -import Signature_SinceCpp20_Simplified from './_codes/find/since-cpp20-simplified.mdx'; +import Signature_SinceCpp20_Detailed from './_codes/find/since-cpp20-detailed.mdx'; +import Signature_SinceCpp20_Simplified from './_codes/find/since-cpp20-simplified.mdx'; +import FullCode_FindingAndPrintingTheFoundValue from './_codes/find/example-Finding-and-printing-the-found-value.mdx'; +import FullCode_FindingAndRemovingTheFoundValue from './_codes/find/example-Finding-and-removing-the-found-value.mdx'; +import FullCode_FindingAndChangingTheFoundValue from './_codes/find/example-Finding-and-changing-the-found-value.mdx'; +import FullCode_UsingAProjectionToFindAValueWithAGivenCharacteristic1 from './_codes/find/example-Using-a-projection-to-find-a-value-with-a-given-characteristic1.mdx'; +import FullCode_UsingAProjectionToFindAValueWithAGivenCharacteristic2 from './_codes/find/example-Using-a-projection-to-find-a-value-with-a-given-characteristic1.mdx'; +import FullCode_FailingToFindAValueSafely from './_codes/find/example-Failing-to-find-a-value-safely.mdx'; +import FullCode_FailingToFindAValueUnsafely from './_codes/find/example-Failing-to-find-a-value-unsafely.mdx'; +import FullCode_FindingTheLastSuitableValueThanksToReverseIterators from './_codes/find/example-Finding-the-last-suitable-value-thanks-to-reverse-iterators.mdx'; +import FullCode_FailingToFindAValueSafelyWhenDealingWithAnIteratorPairSubrange from './_codes/find/example-Failing-to-find-a-value-safely-when-dealing-with-an-iterator-pair-subrange.mdx'; +import FullCode_FailingToFindAValueWhenDealingWithAnIteratorPairSubrangeUnsafely from './_codes/find/example-Failing-to-find-a-value-when-dealing-with-an-iterator-pair-subrange-unsafely.mdx'; # std::ranges::find() algorithm @@ -115,75 +127,66 @@ inline constexpr find_fn find; #### Finding and printing the found value -```cpp showLineNumbers -#include -#include -#include +}> -int main() -{ - const std::vector values{2, 4, 6, 1, 8, 10}; - const auto iteratorTo1 = std::ranges::find(values, 1); - std::cout << *iteratorTo1; -} +```cpp +const std::vector values{2, 4, 6, 1, 8, 10}; +const auto iteratorTo1 = std::ranges::find(values, 1); +std::cout << *iteratorTo1; ``` + + ```plaintext title="Output" 1 ``` #### Finding and removing the found value -```cpp showLineNumbers -#include -#include -#include -#include +}> -int main() -{ - std::vector values{2, 4, 6, 1, 8, 10}; - const auto iteratorTo1 = std::ranges::find(values, 1); - values.erase(iteratorTo1); +```cpp +std::vector values{2, 4, 6, 1, 8, 10}; +std::string text = "hello, there!"; - std::string text = "hello, there!"; - const auto iteratorToComma = std::ranges::find(text, ','); - text.erase(iteratorToComma); +println(values); +std::cout << text << '\n'; - for (const int element : values) std::cout << element << ' '; - std::cout << '\n'; - std::cout << text; -} +const auto iteratorTo1 = std::ranges::find(values, 1); +values.erase(iteratorTo1); + +const auto iteratorToComma = std::ranges::find(text, ','); +text.erase(iteratorToComma); + +std::cout << '\n'; +println(values); +std::cout << text << '\n'; ``` + + ```plaintext title="Output" +2 4 6 1 8 10 +hello, there! + 2 4 6 8 10 hello there! ``` #### Finding and changing the found value -```cpp showLineNumbers -#include -#include -#include +}> -int main() -{ - auto println = [](std::vector const& vec) - { - for (const int element : vec) std::cout << element << ' '; - std::cout << '\n'; - }; - - std::vector values{2, 4, 5, 8, 10}; - println(values); - auto iteratorTo5 = std::ranges::find(values, 5); - *iteratorTo5 = 6; - println(values); -} +```cpp +std::vector values{2, 4, 5, 8, 10}; +println(values); +auto iteratorTo5 = std::ranges::find(values, 5); +*iteratorTo5 = 6; +println(values); ``` + + ```plaintext title="Output" 2 4 5 8 10 2 4 6 8 10 @@ -191,27 +194,18 @@ int main() #### Using a projection to find a value with a given characteristic #1 -```cpp showLineNumbers -#include -#include -#include -#include +}> -int main() -{ - const std::vector words = { - "some", "of", "these", "words", "are", - "of", "the", "same", "lengths" - }; - - const auto bySize = [](const std::string& s) { - return s.size(); - }; +```cpp +const std::vector words = { + "some", "of", "these", "words", "are", + "of", "the", "same", "lengths" +}; - const auto ofSize5 = std::ranges::find(words, 5, bySize); - std::cout << *ofSize5; -} +const auto ofSize5 = std::ranges::find(words, 5, bySize); +std::cout << *ofSize5; ``` + ```plaintext title="Output" these @@ -219,56 +213,46 @@ these #### Using a projection to find a value with a given characteristic #2 -```cpp showLineNumbers -#include -#include -#include +}> -int main() -{ - const std::vector numbers = { - 5, 6, 7, 8 - }; - - const auto byModulus5 = [](const int n) { - return n % 5; - }; +```cpp +const std::vector numbers = { + 5, 6, 7, 8 +}; - const auto withRemainder2 = std::ranges::find(numbers, 2, byModulus5); - std::cout << *withRemainder2; -} +const auto withRemainder2 = std::ranges::find(numbers, 2, byModulus5); +std::cout << *withRemainder2; ``` + + ```plaintext title="Output" 7 ``` #### Failing to find a value safely -```cpp showLineNumbers -#include -#include -#include +}> -int main() -{ - const std::vector onlyEvens = { - 2, 4, 6, 8, 10 - }; +```cpp +const std::vector onlyEvens = { + 2, 4, 6, 8, 10 +}; - auto positionOf1 = std::ranges::find(onlyEvens, 1); +auto positionOf1 = std::ranges::find(onlyEvens, 1); - if (positionOf1 != onlyEvens.end()) - { - std::cout << "odd element found: " << *positionOf1; - } - else - { - std::cout << "no odd elements found"; - } +if (positionOf1 != onlyEvens.end()) +{ + std::cout << "odd element found: " << *positionOf1; +} +else +{ + std::cout << "no odd elements found"; } ``` + + ```plaintext title="Output" no even elements found ``` @@ -279,63 +263,51 @@ Because finding _may_ fail, we should almost always compare the returned iterato #### Failing to find a value unsafely -```cpp -#include -#include -#include +}> -int main() -{ - const std::vector onlyEvens = { - 2, 4, 6, 8, 10 - }; +```cpp +const std::vector onlyEvens = { + 2, 4, 6, 8, 10 +}; - auto positionOf1 = std::ranges::find(onlyEvens, 1); +auto positionOf1 = std::ranges::find(onlyEvens, 1); - std::cout << "odd element found: " << *positionOf1; -} +std::cout << "odd element found: " << *positionOf1; ``` + + :::danger Because `onlyEvens` does **not** contain the searched-for value `1`, it returns the _end_ of the supplied range, i.e., `onlyEvens.end()`. Dereferencing it invokes undefined behavior. ::: #### Finding the last (instead of the first) suitable value thanks to reverse iterators -```cpp showLineNumbers -#include -#include -#include -#include +}> -int main() -{ - auto println = [](std::vector const& vec) - { - for (const int element : vec) std::cout << element << ' '; - std::cout << '\n'; - }; - - std::vector numbers = { - 1, 2, 3, 2, 1 - }; - - const auto first1 = std::ranges::find(numbers, 1); - const auto last1 = std::ranges::find(numbers.rbegin(), numbers.rend(), 1); - - println(numbers); - *first1 = 9; - *last1 = 7; - println(numbers); - - // can also use reverse_view from - auto numbersReversed = numbers | std::views::reverse; - const auto last2 = std::ranges::find(numbersReversed, 2); - *last2 = 0; - println(numbers); +```cpp +std::vector numbers = { + 1, 2, 3, 2, 1 +}; + +const auto first1 = std::ranges::find(numbers, 1); +const auto last1 = std::ranges::find(numbers.rbegin(), numbers.rend(), 1); + +println(numbers); +*first1 = 9; +*last1 = 7; +println(numbers); + +// can also use reverse_view from +auto numbersReversed = numbers | std::views::reverse; +const auto last2 = std::ranges::find(numbersReversed, 2); +*last2 = 0; +println(numbers); } ``` + + ```plaintext title="Output" 1 2 3 2 1 9 2 3 2 7 @@ -344,32 +316,29 @@ int main() #### Failing to find a value safely when dealing with an iterator-pair subrange -```cpp showLineNumbers -#include -#include -#include +}> -int main() -{ - const std::vector numbers = { - 1, 2, 3, 4, 5, 0, 6 - }; +```cpp +const std::vector numbers = { + 1, 2, 3, 4, 5, 0, 6 +}; - const auto zeroInFirstThree = std::ranges::find( - numbers.begin(), numbers.begin() + 3, 0 - ); +const auto zeroInFirstThree = std::ranges::find( + numbers.begin(), numbers.begin() + 3, 0 +); - if (zeroInFirstThree != numbers.begin() + 3) - { - std::cout << "there is a 0 in the first three elements"; - } - else - { - std::cout << "there are no 0s in the first three elements"; - } +if (zeroInFirstThree != numbers.begin() + 3) +{ + std::cout << "there is a 0 in the first three elements"; +} +else +{ + std::cout << "there are no 0s in the first three elements"; } ``` + + ```plaintext title="Output" there are no 0s in the first three elements ``` @@ -381,32 +350,29 @@ When dealing with an iterator-pair subrange, the _end_ of that subrange is the s #### Failing to find a value when dealing with an iterator-pair subrange unsafely -```cpp showLineNumbers -#include -#include -#include +}> -int main() -{ - const std::vector numbers = { - 1, 2, 3, 4, 5, 0, 6 - }; +```cpp +const std::vector numbers = { + 1, 2, 3, 4, 5, 0, 6 +}; - const auto zeroInFirstThree = std::ranges::find( - numbers.begin(), numbers.begin() + 3, 0 - ); +const auto zeroInFirstThree = std::ranges::find( + numbers.begin(), numbers.begin() + 3, 0 +); - if (zeroInFirstThree != numbers.end()) - { - std::cout << "there is a 0 in the first three elements"; - } - else - { - std::cout << "there are no 0s in the first three elements"; - } +if (zeroInFirstThree != numbers.end()) +{ + std::cout << "there is a 0 in the first three elements"; +} +else +{ + std::cout << "there are no 0s in the first three elements"; } ``` + + ```plaintext title="Output" there is a 0 in the first three elements ``` From 78be6addd1337966611507de1cbc69dc107688ec Mon Sep 17 00:00:00 2001 From: Fureeish Date: Sun, 6 Apr 2025 18:01:15 +0200 Subject: [PATCH 4/9] Add error-next-line to listings marked as :::danger --- .../_codes/find/example-Failing-to-find-a-value-unsafely.mdx | 3 ++- ...e-when-dealing-with-an-iterator-pair-subrange-unsafely.mdx | 1 + content/docs/std/algo/ranges/find.mdx | 4 +++- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/content/docs/std/algo/ranges/_codes/find/example-Failing-to-find-a-value-unsafely.mdx b/content/docs/std/algo/ranges/_codes/find/example-Failing-to-find-a-value-unsafely.mdx index dc9f3f09f..cc09a45f7 100644 --- a/content/docs/std/algo/ranges/_codes/find/example-Failing-to-find-a-value-unsafely.mdx +++ b/content/docs/std/algo/ranges/_codes/find/example-Failing-to-find-a-value-unsafely.mdx @@ -11,6 +11,7 @@ int main() auto positionOf1 = std::ranges::find(onlyEvens, 1); - std::cout << "odd element found: " << *positionOf1; +// error-next-line + std::cout << "odd element found: " << *positionOf1; // dereferencing end, undefined behavior } ``` \ No newline at end of file diff --git a/content/docs/std/algo/ranges/_codes/find/example-Failing-to-find-a-value-when-dealing-with-an-iterator-pair-subrange-unsafely.mdx b/content/docs/std/algo/ranges/_codes/find/example-Failing-to-find-a-value-when-dealing-with-an-iterator-pair-subrange-unsafely.mdx index ff10058e5..819cb65ea 100644 --- a/content/docs/std/algo/ranges/_codes/find/example-Failing-to-find-a-value-when-dealing-with-an-iterator-pair-subrange-unsafely.mdx +++ b/content/docs/std/algo/ranges/_codes/find/example-Failing-to-find-a-value-when-dealing-with-an-iterator-pair-subrange-unsafely.mdx @@ -13,6 +13,7 @@ int main() numbers.begin(), numbers.begin() + 3, 0 ); +// error-next-line if (zeroInFirstThree != numbers.end()) { std::cout << "there is a 0 in the first three elements"; diff --git a/content/docs/std/algo/ranges/find.mdx b/content/docs/std/algo/ranges/find.mdx index 5f7f54bdc..c5e4a7343 100644 --- a/content/docs/std/algo/ranges/find.mdx +++ b/content/docs/std/algo/ranges/find.mdx @@ -272,7 +272,8 @@ const std::vector onlyEvens = { auto positionOf1 = std::ranges::find(onlyEvens, 1); -std::cout << "odd element found: " << *positionOf1; +// error-next-line +std::cout << "odd element found: " << *positionOf1; // dereferencing end, undefined behavior ``` @@ -361,6 +362,7 @@ const auto zeroInFirstThree = std::ranges::find( numbers.begin(), numbers.begin() + 3, 0 ); +// error-next-line if (zeroInFirstThree != numbers.end()) { std::cout << "there is a 0 in the first three elements"; From d91e9aef63493b38461f8f1a699274fd484f47ac Mon Sep 17 00:00:00 2001 From: Fureeish Date: Sun, 6 Apr 2025 18:40:03 +0200 Subject: [PATCH 5/9] Apply AAA and east-const styles --- ...dealing-with-an-iterator-pair-subrange.mdx | 6 +- ...example-Failing-to-find-a-value-safely.mdx | 6 +- ...ample-Failing-to-find-a-value-unsafely.mdx | 8 +-- ...ith-an-iterator-pair-subrange-unsafely.mdx | 8 +-- ...e-Finding-and-printing-the-found-value.mdx | 6 +- ...e-Finding-and-removing-the-found-value.mdx | 17 +++--- ...able-value-thanks-to-reverse-iterators.mdx | 17 +++--- ...d-a-value-with-a-given-characteristic1.mdx | 8 +-- ...d-a-value-with-a-given-characteristic2.mdx | 8 +-- content/docs/std/algo/ranges/find.mdx | 56 ++++++++++--------- 10 files changed, 75 insertions(+), 65 deletions(-) diff --git a/content/docs/std/algo/ranges/_codes/find/example-Failing-to-find-a-value-safely-when-dealing-with-an-iterator-pair-subrange.mdx b/content/docs/std/algo/ranges/_codes/find/example-Failing-to-find-a-value-safely-when-dealing-with-an-iterator-pair-subrange.mdx index 82148852f..f138d6d2d 100644 --- a/content/docs/std/algo/ranges/_codes/find/example-Failing-to-find-a-value-safely-when-dealing-with-an-iterator-pair-subrange.mdx +++ b/content/docs/std/algo/ranges/_codes/find/example-Failing-to-find-a-value-safely-when-dealing-with-an-iterator-pair-subrange.mdx @@ -3,13 +3,13 @@ #include #include -int main() +auto main() -> int { - const std::vector numbers = { + auto const numbers = std::vector{ 1, 2, 3, 4, 5, 0, 6 }; - const auto zeroInFirstThree = std::ranges::find( + auto const zeroInFirstThree = std::ranges::find( numbers.begin(), numbers.begin() + 3, 0 ); diff --git a/content/docs/std/algo/ranges/_codes/find/example-Failing-to-find-a-value-safely.mdx b/content/docs/std/algo/ranges/_codes/find/example-Failing-to-find-a-value-safely.mdx index b962eaac5..b1bb23ecc 100644 --- a/content/docs/std/algo/ranges/_codes/find/example-Failing-to-find-a-value-safely.mdx +++ b/content/docs/std/algo/ranges/_codes/find/example-Failing-to-find-a-value-safely.mdx @@ -3,13 +3,13 @@ #include #include -int main() +auto main() -> int { - const std::vector onlyEvens = { + auto const onlyEvens = std::vector{ 2, 4, 6, 8, 10 }; - auto positionOf1 = std::ranges::find(onlyEvens, 1); + auto const positionOf1 = std::ranges::find(onlyEvens, 1); if (positionOf1 != onlyEvens.end()) { diff --git a/content/docs/std/algo/ranges/_codes/find/example-Failing-to-find-a-value-unsafely.mdx b/content/docs/std/algo/ranges/_codes/find/example-Failing-to-find-a-value-unsafely.mdx index cc09a45f7..041b2aa26 100644 --- a/content/docs/std/algo/ranges/_codes/find/example-Failing-to-find-a-value-unsafely.mdx +++ b/content/docs/std/algo/ranges/_codes/find/example-Failing-to-find-a-value-unsafely.mdx @@ -3,15 +3,15 @@ #include #include -int main() +auto main() -> int { - const std::vector onlyEvens = { + auto const onlyEvens = std::vector{ 2, 4, 6, 8, 10 }; - auto positionOf1 = std::ranges::find(onlyEvens, 1); + auto const positionOf1 = std::ranges::find(onlyEvens, 1); -// error-next-line + // error-next-line std::cout << "odd element found: " << *positionOf1; // dereferencing end, undefined behavior } ``` \ No newline at end of file diff --git a/content/docs/std/algo/ranges/_codes/find/example-Failing-to-find-a-value-when-dealing-with-an-iterator-pair-subrange-unsafely.mdx b/content/docs/std/algo/ranges/_codes/find/example-Failing-to-find-a-value-when-dealing-with-an-iterator-pair-subrange-unsafely.mdx index 819cb65ea..3643a642b 100644 --- a/content/docs/std/algo/ranges/_codes/find/example-Failing-to-find-a-value-when-dealing-with-an-iterator-pair-subrange-unsafely.mdx +++ b/content/docs/std/algo/ranges/_codes/find/example-Failing-to-find-a-value-when-dealing-with-an-iterator-pair-subrange-unsafely.mdx @@ -3,17 +3,17 @@ #include #include -int main() +auto main() -> int { - const std::vector numbers = { + auto const numbers = std::vector{ 1, 2, 3, 4, 5, 0, 6 }; - const auto zeroInFirstThree = std::ranges::find( + auto const zeroInFirstThree = std::ranges::find( numbers.begin(), numbers.begin() + 3, 0 ); -// error-next-line + // error-next-line if (zeroInFirstThree != numbers.end()) { std::cout << "there is a 0 in the first three elements"; diff --git a/content/docs/std/algo/ranges/_codes/find/example-Finding-and-printing-the-found-value.mdx b/content/docs/std/algo/ranges/_codes/find/example-Finding-and-printing-the-found-value.mdx index 7e6534699..afa3bdce2 100644 --- a/content/docs/std/algo/ranges/_codes/find/example-Finding-and-printing-the-found-value.mdx +++ b/content/docs/std/algo/ranges/_codes/find/example-Finding-and-printing-the-found-value.mdx @@ -3,10 +3,10 @@ #include #include -int main() +auto main() -> int { - const std::vector values{2, 4, 6, 1, 8, 10}; - const auto iteratorTo1 = std::ranges::find(values, 1); + auto const values = std::vector{2, 4, 6, 1, 8, 10}; + auto const iteratorTo1 = std::ranges::find(values, 1); std::cout << *iteratorTo1; } ``` \ No newline at end of file diff --git a/content/docs/std/algo/ranges/_codes/find/example-Finding-and-removing-the-found-value.mdx b/content/docs/std/algo/ranges/_codes/find/example-Finding-and-removing-the-found-value.mdx index 75413ef8f..9ccdac1be 100644 --- a/content/docs/std/algo/ranges/_codes/find/example-Finding-and-removing-the-found-value.mdx +++ b/content/docs/std/algo/ranges/_codes/find/example-Finding-and-removing-the-found-value.mdx @@ -4,24 +4,27 @@ #include #include -int main() +auto main() -> int { - const auto println = [](const std::vector& vec) + auto const println = [](const std::vector& vec) { - for (const int element : vec) std::cout << element << ' '; + for (auto const element : vec) + { + std::cout << element << ' '; + } std::cout << '\n'; }; - std::vector values{2, 4, 6, 1, 8, 10}; - std::string text = "hello, there!"; + auto values = std::vector{2, 4, 6, 1, 8, 10}; + auto text = std::string("hello, there!"); println(values); std::cout << text << '\n'; - const auto iteratorTo1 = std::ranges::find(values, 1); + auto const iteratorTo1 = std::ranges::find(values, 1); values.erase(iteratorTo1); - const auto iteratorToComma = std::ranges::find(text, ','); + auto const iteratorToComma = std::ranges::find(text, ','); text.erase(iteratorToComma); std::cout << '\n'; diff --git a/content/docs/std/algo/ranges/_codes/find/example-Finding-the-last-suitable-value-thanks-to-reverse-iterators.mdx b/content/docs/std/algo/ranges/_codes/find/example-Finding-the-last-suitable-value-thanks-to-reverse-iterators.mdx index 41abf9be8..c12c81c94 100644 --- a/content/docs/std/algo/ranges/_codes/find/example-Finding-the-last-suitable-value-thanks-to-reverse-iterators.mdx +++ b/content/docs/std/algo/ranges/_codes/find/example-Finding-the-last-suitable-value-thanks-to-reverse-iterators.mdx @@ -4,20 +4,23 @@ #include #include -int main() +auto main() -> int { - const auto println = [](std::vector const& vec) + auto const println = [](std::vector const& vec) { - for (const int element : vec) std::cout << element << ' '; + for (auto const element : vec) + { + std::cout << element << ' '; + } std::cout << '\n'; }; - std::vector numbers = { + auto numbers = std::vector{ 1, 2, 3, 2, 1 }; - const auto first1 = std::ranges::find(numbers, 1); - const auto last1 = std::ranges::find(numbers.rbegin(), numbers.rend(), 1); + auto const first1 = std::ranges::find(numbers, 1); + auto const last1 = std::ranges::find(numbers.rbegin(), numbers.rend(), 1); println(numbers); *first1 = 9; @@ -26,7 +29,7 @@ int main() // can also use reverse_view from auto numbersReversed = numbers | std::views::reverse; - const auto last2 = std::ranges::find(numbersReversed, 2); + auto const last2 = std::ranges::find(numbersReversed, 2); *last2 = 0; println(numbers); } diff --git a/content/docs/std/algo/ranges/_codes/find/example-Using-a-projection-to-find-a-value-with-a-given-characteristic1.mdx b/content/docs/std/algo/ranges/_codes/find/example-Using-a-projection-to-find-a-value-with-a-given-characteristic1.mdx index 562e7e8bc..7dd677572 100644 --- a/content/docs/std/algo/ranges/_codes/find/example-Using-a-projection-to-find-a-value-with-a-given-characteristic1.mdx +++ b/content/docs/std/algo/ranges/_codes/find/example-Using-a-projection-to-find-a-value-with-a-given-characteristic1.mdx @@ -4,18 +4,18 @@ #include #include -int main() +auto main() -> int { - const std::vector words = { + auto const words = std::vector{ "some", "of", "these", "words", "are", "of", "the", "same", "lengths" }; - const auto bySize = [](const std::string& s) { + auto const bySize = [](const std::string& s) { return s.size(); }; - const auto ofSize5 = std::ranges::find(words, 5, bySize); + auto const ofSize5 = std::ranges::find(words, 5, bySize); std::cout << *ofSize5; } ``` \ No newline at end of file diff --git a/content/docs/std/algo/ranges/_codes/find/example-Using-a-projection-to-find-a-value-with-a-given-characteristic2.mdx b/content/docs/std/algo/ranges/_codes/find/example-Using-a-projection-to-find-a-value-with-a-given-characteristic2.mdx index 6e527df96..a9cadd8de 100644 --- a/content/docs/std/algo/ranges/_codes/find/example-Using-a-projection-to-find-a-value-with-a-given-characteristic2.mdx +++ b/content/docs/std/algo/ranges/_codes/find/example-Using-a-projection-to-find-a-value-with-a-given-characteristic2.mdx @@ -3,17 +3,17 @@ #include #include -int main() +auto main() -> int { - const std::vector numbers = { + auto const numbers = std::vector{ 5, 6, 7, 8 }; - const auto byModulus5 = [](const int n) { + auto const byModulus5 = [](const int n) { return n % 5; }; - const auto withRemainder2 = std::ranges::find(numbers, 2, byModulus5); + auto const withRemainder2 = std::ranges::find(numbers, 2, byModulus5); std::cout << *withRemainder2; } ``` \ No newline at end of file diff --git a/content/docs/std/algo/ranges/find.mdx b/content/docs/std/algo/ranges/find.mdx index c5e4a7343..a77427983 100644 --- a/content/docs/std/algo/ranges/find.mdx +++ b/content/docs/std/algo/ranges/find.mdx @@ -22,7 +22,7 @@ import FullCode_FindingAndPrintingTheFoundValue import FullCode_FindingAndRemovingTheFoundValue from './_codes/find/example-Finding-and-removing-the-found-value.mdx'; import FullCode_FindingAndChangingTheFoundValue from './_codes/find/example-Finding-and-changing-the-found-value.mdx'; import FullCode_UsingAProjectionToFindAValueWithAGivenCharacteristic1 from './_codes/find/example-Using-a-projection-to-find-a-value-with-a-given-characteristic1.mdx'; -import FullCode_UsingAProjectionToFindAValueWithAGivenCharacteristic2 from './_codes/find/example-Using-a-projection-to-find-a-value-with-a-given-characteristic1.mdx'; +import FullCode_UsingAProjectionToFindAValueWithAGivenCharacteristic2 from './_codes/find/example-Using-a-projection-to-find-a-value-with-a-given-characteristic2.mdx'; import FullCode_FailingToFindAValueSafely from './_codes/find/example-Failing-to-find-a-value-safely.mdx'; import FullCode_FailingToFindAValueUnsafely from './_codes/find/example-Failing-to-find-a-value-unsafely.mdx'; import FullCode_FindingTheLastSuitableValueThanksToReverseIterators from './_codes/find/example-Finding-the-last-suitable-value-thanks-to-reverse-iterators.mdx'; @@ -130,8 +130,8 @@ inline constexpr find_fn find; }> ```cpp -const std::vector values{2, 4, 6, 1, 8, 10}; -const auto iteratorTo1 = std::ranges::find(values, 1); +auto const values = std::vector{2, 4, 6, 1, 8, 10}; +auto const iteratorTo1 = std::ranges::find(values, 1); std::cout << *iteratorTo1; ``` @@ -146,16 +146,16 @@ std::cout << *iteratorTo1; }> ```cpp -std::vector values{2, 4, 6, 1, 8, 10}; -std::string text = "hello, there!"; +auto values = std::vector{2, 4, 6, 1, 8, 10}; +auto text = std::string("hello, there!"); println(values); std::cout << text << '\n'; -const auto iteratorTo1 = std::ranges::find(values, 1); +auto const iteratorTo1 = std::ranges::find(values, 1); values.erase(iteratorTo1); -const auto iteratorToComma = std::ranges::find(text, ','); +auto const iteratorToComma = std::ranges::find(text, ','); text.erase(iteratorToComma); std::cout << '\n'; @@ -178,9 +178,9 @@ hello there! }> ```cpp -std::vector values{2, 4, 5, 8, 10}; +auto values = std::vector{2, 4, 5, 8, 10}; println(values); -auto iteratorTo5 = std::ranges::find(values, 5); +auto const iteratorTo5 = std::ranges::find(values, 5); *iteratorTo5 = 6; println(values); ``` @@ -192,17 +192,21 @@ println(values); 2 4 6 8 10 ``` +:::info +Despite `iteratorTo5` being `const`, it's still fine to use it to alter the element it points to. That's because the `const` modifier in its declaration applies only to the iterator itself - we can't change **it** (we can't change *to which* element it points to), but we can alter _the object it points to_. +::: + #### Using a projection to find a value with a given characteristic #1 }> ```cpp -const std::vector words = { +auto const words = std::vector{ "some", "of", "these", "words", "are", "of", "the", "same", "lengths" }; -const auto ofSize5 = std::ranges::find(words, 5, bySize); +auto const ofSize5 = std::ranges::find(words, 5, bySize); std::cout << *ofSize5; ``` @@ -216,11 +220,11 @@ these }> ```cpp -const std::vector numbers = { +auto const numbers = std::vector{ 5, 6, 7, 8 }; -const auto withRemainder2 = std::ranges::find(numbers, 2, byModulus5); +auto const withRemainder2 = std::ranges::find(numbers, 2, byModulus5); std::cout << *withRemainder2; ``` @@ -235,11 +239,11 @@ std::cout << *withRemainder2; }> ```cpp -const std::vector onlyEvens = { +auto const onlyEvens = std::vector{ 2, 4, 6, 8, 10 }; -auto positionOf1 = std::ranges::find(onlyEvens, 1); +auto const positionOf1 = std::ranges::find(onlyEvens, 1); if (positionOf1 != onlyEvens.end()) { @@ -259,18 +263,18 @@ no even elements found :::info Because finding _may_ fail, we should almost always compare the returned iterator with the _end_ of the supplied range before we use it. - ::: +::: #### Failing to find a value unsafely }> ```cpp -const std::vector onlyEvens = { +auto const onlyEvens = std::vector{ 2, 4, 6, 8, 10 }; -auto positionOf1 = std::ranges::find(onlyEvens, 1); +auto const positionOf1 = std::ranges::find(onlyEvens, 1); // error-next-line std::cout << "odd element found: " << *positionOf1; // dereferencing end, undefined behavior @@ -287,12 +291,12 @@ Because `onlyEvens` does **not** contain the searched-for value `1`, it returns }> ```cpp -std::vector numbers = { +auto numbers = std::vector{ 1, 2, 3, 2, 1 }; -const auto first1 = std::ranges::find(numbers, 1); -const auto last1 = std::ranges::find(numbers.rbegin(), numbers.rend(), 1); +auto const first1 = std::ranges::find(numbers, 1); +auto const last1 = std::ranges::find(numbers.rbegin(), numbers.rend(), 1); println(numbers); *first1 = 9; @@ -301,7 +305,7 @@ println(numbers); // can also use reverse_view from auto numbersReversed = numbers | std::views::reverse; -const auto last2 = std::ranges::find(numbersReversed, 2); +auto const last2 = std::ranges::find(numbersReversed, 2); *last2 = 0; println(numbers); } @@ -320,11 +324,11 @@ println(numbers); }> ```cpp -const std::vector numbers = { +auto const numbers = std::vector{ 1, 2, 3, 4, 5, 0, 6 }; -const auto zeroInFirstThree = std::ranges::find( +auto const zeroInFirstThree = std::ranges::find( numbers.begin(), numbers.begin() + 3, 0 ); @@ -354,11 +358,11 @@ When dealing with an iterator-pair subrange, the _end_ of that subrange is the s }> ```cpp -const std::vector numbers = { +auto const numbers = std::vector{ 1, 2, 3, 4, 5, 0, 6 }; -const auto zeroInFirstThree = std::ranges::find( +auto const zeroInFirstThree = std::ranges::find( numbers.begin(), numbers.begin() + 3, 0 ); From 98b8473af830692838b7a93b4fc3f2897037710e Mon Sep 17 00:00:00 2001 From: Fureeish Date: Mon, 7 Apr 2025 16:18:49 +0200 Subject: [PATCH 6/9] Add KaTeX dependencies --- docusaurus.config.ts | 14 ++- package-lock.json | 268 +++++++++++++++++++++++++++++++++++++++++++ package.json | 2 + 3 files changed, 283 insertions(+), 1 deletion(-) diff --git a/docusaurus.config.ts b/docusaurus.config.ts index 61ab0377a..66e75e1c1 100644 --- a/docusaurus.config.ts +++ b/docusaurus.config.ts @@ -4,6 +4,8 @@ import path from "path"; // Docusaurus import type { Config } from "@docusaurus/types"; import type * as Preset from "@docusaurus/preset-classic"; +import remarkMath from "remark-math"; +import rehypeKatex from "rehype-katex"; // Custom import * as Themes from "./src/prism/themes"; @@ -114,6 +116,8 @@ const config = { showLastUpdateTime: false, showLastUpdateAuthor: false, exclude: ["**/_codes/**.{mdx}"], + remarkPlugins: [remarkMath], + rehypePlugins: [rehypeKatex], }, blog: { showReadingTime: true, @@ -131,7 +135,15 @@ const config = { } satisfies Preset.Options, ], ], - + stylesheets: [ + { + href: "https://cdn.jsdelivr.net/npm/katex@0.13.24/dist/katex.min.css", + type: "text/css", + integrity: + "sha384-odtC+0UGzzFL/6PNoE8rX/SPcQDXBJ+uRepguP4QkPCm2LBxH3FA3y+fKSiJ+AmM", + crossorigin: "anonymous", + }, + ], themeConfig: { image: "img/favicon.png", diff --git a/package-lock.json b/package-lock.json index 4c5e4482a..ef1189fa8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -23,6 +23,8 @@ "raw-loader": "^4.0.2", "react": "^18.2.0", "react-dom": "^18.2.0", + "rehype-katex": "^7.0.1", + "remark-math": "^6.0.0", "sass": "^1.71.1", "sass-loader": "^14.1.1", "url-loader": "^4.1.1", @@ -4934,6 +4936,12 @@ "integrity": "sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ==", "dev": true }, + "node_modules/@types/katex": { + "version": "0.16.7", + "resolved": "https://registry.npmjs.org/@types/katex/-/katex-0.16.7.tgz", + "integrity": "sha512-HMwFiRujE5PjrgwHQ25+bsLJgowjGjm5Z8FVSf0N6PwgJrwxH0QxzHYDcKsTfV3wva0vzrpqMTJS2jXPr5BMEQ==", + "license": "MIT" + }, "node_modules/@types/mdast": { "version": "4.0.4", "resolved": "https://registry.npmjs.org/@types/mdast/-/mdast-4.0.4.tgz", @@ -9889,6 +9897,55 @@ "node": ">= 0.4" } }, + "node_modules/hast-util-from-dom": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/hast-util-from-dom/-/hast-util-from-dom-5.0.1.tgz", + "integrity": "sha512-N+LqofjR2zuzTjCPzyDUdSshy4Ma6li7p/c3pA78uTwzFgENbgbUrm2ugwsOdcjI1muO+o6Dgzp9p8WHtn/39Q==", + "license": "ISC", + "dependencies": { + "@types/hast": "^3.0.0", + "hastscript": "^9.0.0", + "web-namespaces": "^2.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" + } + }, + "node_modules/hast-util-from-html": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/hast-util-from-html/-/hast-util-from-html-2.0.3.tgz", + "integrity": "sha512-CUSRHXyKjzHov8yKsQjGOElXy/3EKpyX56ELnkHH34vDVw1N1XSQ1ZcAvTyAPtGqLTuKP/uxM+aLkSPqF/EtMw==", + "license": "MIT", + "dependencies": { + "@types/hast": "^3.0.0", + "devlop": "^1.1.0", + "hast-util-from-parse5": "^8.0.0", + "parse5": "^7.0.0", + "vfile": "^6.0.0", + "vfile-message": "^4.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" + } + }, + "node_modules/hast-util-from-html-isomorphic": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/hast-util-from-html-isomorphic/-/hast-util-from-html-isomorphic-2.0.0.tgz", + "integrity": "sha512-zJfpXq44yff2hmE0XmwEOzdWin5xwH+QIhMLOScpX91e/NSGPsAzNCvLQDIEPyO2TXi+lBmU6hjLIhV8MwP2kw==", + "license": "MIT", + "dependencies": { + "@types/hast": "^3.0.0", + "hast-util-from-dom": "^5.0.0", + "hast-util-from-html": "^2.0.0", + "unist-util-remove-position": "^5.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" + } + }, "node_modules/hast-util-from-parse5": { "version": "8.0.3", "resolved": "https://registry.npmjs.org/hast-util-from-parse5/-/hast-util-from-parse5-8.0.3.tgz", @@ -9908,6 +9965,19 @@ "url": "https://opencollective.com/unified" } }, + "node_modules/hast-util-is-element": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/hast-util-is-element/-/hast-util-is-element-3.0.0.tgz", + "integrity": "sha512-Val9mnv2IWpLbNPqc/pUem+a7Ipj2aHacCwgNfTiK0vJKl0LF+4Ba4+v1oPHFpf3bLYmreq0/l3Gud9S5OH42g==", + "license": "MIT", + "dependencies": { + "@types/hast": "^3.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" + } + }, "node_modules/hast-util-parse-selector": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/hast-util-parse-selector/-/hast-util-parse-selector-4.0.0.tgz", @@ -10024,6 +10094,22 @@ "url": "https://github.com/sponsors/wooorm" } }, + "node_modules/hast-util-to-text": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/hast-util-to-text/-/hast-util-to-text-4.0.2.tgz", + "integrity": "sha512-KK6y/BN8lbaq654j7JgBydev7wuNMcID54lkRav1P0CaE1e47P72AWWPiGKXTJU271ooYzcvTAn/Zt0REnvc7A==", + "license": "MIT", + "dependencies": { + "@types/hast": "^3.0.0", + "@types/unist": "^3.0.0", + "hast-util-is-element": "^3.0.0", + "unist-util-find-after": "^5.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" + } + }, "node_modules/hast-util-whitespace": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/hast-util-whitespace/-/hast-util-whitespace-3.0.0.tgz", @@ -11244,6 +11330,31 @@ "node": ">=4.0" } }, + "node_modules/katex": { + "version": "0.16.21", + "resolved": "https://registry.npmjs.org/katex/-/katex-0.16.21.tgz", + "integrity": "sha512-XvqR7FgOHtWupfMiigNzmh+MgUVmDGU2kXZm899ZkPfcuoPuFxyHmXsgATDpFZDAXCI8tvinaVcDo8PIIJSo4A==", + "funding": [ + "https://opencollective.com/katex", + "https://github.com/sponsors/katex" + ], + "license": "MIT", + "dependencies": { + "commander": "^8.3.0" + }, + "bin": { + "katex": "cli.js" + } + }, + "node_modules/katex/node_modules/commander": { + "version": "8.3.0", + "resolved": "https://registry.npmjs.org/commander/-/commander-8.3.0.tgz", + "integrity": "sha512-OkTL9umf+He2DZkUq8f8J9of7yL6RJKI24dVITBmNfZBmri9zYZQrKkuXiKhyfPSu8tUhnVBB1iKXevvnlR4Ww==", + "license": "MIT", + "engines": { + "node": ">= 12" + } + }, "node_modules/keyv": { "version": "4.5.4", "resolved": "https://registry.npmjs.org/keyv/-/keyv-4.5.4.tgz", @@ -11713,6 +11824,25 @@ "url": "https://opencollective.com/unified" } }, + "node_modules/mdast-util-math": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/mdast-util-math/-/mdast-util-math-3.0.0.tgz", + "integrity": "sha512-Tl9GBNeG/AhJnQM221bJR2HPvLOSnLE/T9cJI9tlc6zwQk2nPk/4f0cHkOdEixQPC/j8UtKDdITswvLAy1OZ1w==", + "license": "MIT", + "dependencies": { + "@types/hast": "^3.0.0", + "@types/mdast": "^4.0.0", + "devlop": "^1.0.0", + "longest-streak": "^3.0.0", + "mdast-util-from-markdown": "^2.0.0", + "mdast-util-to-markdown": "^2.1.0", + "unist-util-remove-position": "^5.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" + } + }, "node_modules/mdast-util-mdx": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/mdast-util-mdx/-/mdast-util-mdx-3.0.0.tgz", @@ -12466,6 +12596,81 @@ } ] }, + "node_modules/micromark-extension-math": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/micromark-extension-math/-/micromark-extension-math-3.1.0.tgz", + "integrity": "sha512-lvEqd+fHjATVs+2v/8kg9i5Q0AP2k85H0WUOwpIVvUML8BapsMvh1XAogmQjOCsLpoKRCVQqEkQBB3NhVBcsOg==", + "license": "MIT", + "dependencies": { + "@types/katex": "^0.16.0", + "devlop": "^1.0.0", + "katex": "^0.16.0", + "micromark-factory-space": "^2.0.0", + "micromark-util-character": "^2.0.0", + "micromark-util-symbol": "^2.0.0", + "micromark-util-types": "^2.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" + } + }, + "node_modules/micromark-extension-math/node_modules/micromark-factory-space": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/micromark-factory-space/-/micromark-factory-space-2.0.1.tgz", + "integrity": "sha512-zRkxjtBxxLd2Sc0d+fbnEunsTj46SWXgXciZmHq0kDYGnck/ZSGj9/wULTV95uoeYiK5hRXP2mJ98Uo4cq/LQg==", + "funding": [ + { + "type": "GitHub Sponsors", + "url": "https://github.com/sponsors/unifiedjs" + }, + { + "type": "OpenCollective", + "url": "https://opencollective.com/unified" + } + ], + "license": "MIT", + "dependencies": { + "micromark-util-character": "^2.0.0", + "micromark-util-types": "^2.0.0" + } + }, + "node_modules/micromark-extension-math/node_modules/micromark-util-character": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/micromark-util-character/-/micromark-util-character-2.1.1.tgz", + "integrity": "sha512-wv8tdUTJ3thSFFFJKtpYKOYiGP2+v96Hvk4Tu8KpCAsTMs6yi+nVmGh1syvSCsaxz45J6Jbw+9DD6g97+NV67Q==", + "funding": [ + { + "type": "GitHub Sponsors", + "url": "https://github.com/sponsors/unifiedjs" + }, + { + "type": "OpenCollective", + "url": "https://opencollective.com/unified" + } + ], + "license": "MIT", + "dependencies": { + "micromark-util-symbol": "^2.0.0", + "micromark-util-types": "^2.0.0" + } + }, + "node_modules/micromark-extension-math/node_modules/micromark-util-symbol": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/micromark-util-symbol/-/micromark-util-symbol-2.0.1.tgz", + "integrity": "sha512-vs5t8Apaud9N28kgCrRUdEed4UJ+wWNvicHLPxCa9ENlYuAY31M0ETy5y1vA33YoNPDFTghEbnh6efaE8h4x0Q==", + "funding": [ + { + "type": "GitHub Sponsors", + "url": "https://github.com/sponsors/unifiedjs" + }, + { + "type": "OpenCollective", + "url": "https://opencollective.com/unified" + } + ], + "license": "MIT" + }, "node_modules/micromark-extension-mdx-expression": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/micromark-extension-mdx-expression/-/micromark-extension-mdx-expression-3.0.0.tgz", @@ -16629,6 +16834,25 @@ "node": ">=6" } }, + "node_modules/rehype-katex": { + "version": "7.0.1", + "resolved": "https://registry.npmjs.org/rehype-katex/-/rehype-katex-7.0.1.tgz", + "integrity": "sha512-OiM2wrZ/wuhKkigASodFoo8wimG3H12LWQaH8qSPVJn9apWKFSH3YOCtbKpBorTVw/eI7cuT21XBbvwEswbIOA==", + "license": "MIT", + "dependencies": { + "@types/hast": "^3.0.0", + "@types/katex": "^0.16.0", + "hast-util-from-html-isomorphic": "^2.0.0", + "hast-util-to-text": "^4.0.0", + "katex": "^0.16.0", + "unist-util-visit-parents": "^6.0.0", + "vfile": "^6.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" + } + }, "node_modules/rehype-raw": { "version": "7.0.0", "resolved": "https://registry.npmjs.org/rehype-raw/-/rehype-raw-7.0.0.tgz", @@ -16727,6 +16951,22 @@ "url": "https://opencollective.com/unified" } }, + "node_modules/remark-math": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/remark-math/-/remark-math-6.0.0.tgz", + "integrity": "sha512-MMqgnP74Igy+S3WwnhQ7kqGlEerTETXMvJhrUzDikVZ2/uogJCb+WHUg97hK9/jcfc0dkD73s3LN8zU49cTEtA==", + "license": "MIT", + "dependencies": { + "@types/mdast": "^4.0.0", + "mdast-util-math": "^3.0.0", + "micromark-extension-math": "^3.0.0", + "unified": "^11.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" + } + }, "node_modules/remark-mdx": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/remark-mdx/-/remark-mdx-3.1.0.tgz", @@ -18555,6 +18795,20 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/unist-util-find-after": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/unist-util-find-after/-/unist-util-find-after-5.0.0.tgz", + "integrity": "sha512-amQa0Ep2m6hE2g72AugUItjbuM8X8cGQnFoHk0pGfrFeT9GZhzN5SW8nRsiGKK7Aif4CrACPENkA6P/Lw6fHGQ==", + "license": "MIT", + "dependencies": { + "@types/unist": "^3.0.0", + "unist-util-is": "^6.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" + } + }, "node_modules/unist-util-is": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/unist-util-is/-/unist-util-is-6.0.0.tgz", @@ -18591,6 +18845,20 @@ "url": "https://opencollective.com/unified" } }, + "node_modules/unist-util-remove-position": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/unist-util-remove-position/-/unist-util-remove-position-5.0.0.tgz", + "integrity": "sha512-Hp5Kh3wLxv0PHj9m2yZhhLt58KzPtEYKQQ4yxfYFEO7EvHwzyDYnduhHnY1mDxoqr7VUwVuHXk9RXKIiYS1N8Q==", + "license": "MIT", + "dependencies": { + "@types/unist": "^3.0.0", + "unist-util-visit": "^5.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/unified" + } + }, "node_modules/unist-util-stringify-position": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/unist-util-stringify-position/-/unist-util-stringify-position-4.0.0.tgz", diff --git a/package.json b/package.json index 51ed22bc3..3e9530bb8 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,8 @@ "raw-loader": "^4.0.2", "react": "^18.2.0", "react-dom": "^18.2.0", + "rehype-katex": "^7.0.1", + "remark-math": "^6.0.0", "sass": "^1.71.1", "sass-loader": "^14.1.1", "url-loader": "^4.1.1", From 79050f556afeb064db16e71ddcbebb94764f379e Mon Sep 17 00:00:00 2001 From: Fureeish Date: Mon, 7 Apr 2025 16:45:00 +0200 Subject: [PATCH 7/9] Align some declarations, apply KaTeX for marking numbered elements, and hide some information in the simplified section --- ...xample-Finding-and-printing-the-found-value.mdx | 2 +- ...xample-Finding-and-removing-the-found-value.mdx | 2 +- ...-suitable-value-thanks-to-reverse-iterators.mdx | 2 +- .../ranges/_codes/find/since-cpp20-simplified.mdx | 10 +++++++--- content/docs/std/algo/ranges/find.mdx | 14 ++++++-------- 5 files changed, 16 insertions(+), 14 deletions(-) diff --git a/content/docs/std/algo/ranges/_codes/find/example-Finding-and-printing-the-found-value.mdx b/content/docs/std/algo/ranges/_codes/find/example-Finding-and-printing-the-found-value.mdx index afa3bdce2..5d673fa29 100644 --- a/content/docs/std/algo/ranges/_codes/find/example-Finding-and-printing-the-found-value.mdx +++ b/content/docs/std/algo/ranges/_codes/find/example-Finding-and-printing-the-found-value.mdx @@ -5,7 +5,7 @@ auto main() -> int { - auto const values = std::vector{2, 4, 6, 1, 8, 10}; + auto const values = std::vector{2, 4, 6, 1, 8, 10}; auto const iteratorTo1 = std::ranges::find(values, 1); std::cout << *iteratorTo1; } diff --git a/content/docs/std/algo/ranges/_codes/find/example-Finding-and-removing-the-found-value.mdx b/content/docs/std/algo/ranges/_codes/find/example-Finding-and-removing-the-found-value.mdx index 9ccdac1be..f40d9195a 100644 --- a/content/docs/std/algo/ranges/_codes/find/example-Finding-and-removing-the-found-value.mdx +++ b/content/docs/std/algo/ranges/_codes/find/example-Finding-and-removing-the-found-value.mdx @@ -16,7 +16,7 @@ auto main() -> int }; auto values = std::vector{2, 4, 6, 1, 8, 10}; - auto text = std::string("hello, there!"); + auto text = std::string("hello, there!"); println(values); std::cout << text << '\n'; diff --git a/content/docs/std/algo/ranges/_codes/find/example-Finding-the-last-suitable-value-thanks-to-reverse-iterators.mdx b/content/docs/std/algo/ranges/_codes/find/example-Finding-the-last-suitable-value-thanks-to-reverse-iterators.mdx index c12c81c94..d915f1fce 100644 --- a/content/docs/std/algo/ranges/_codes/find/example-Finding-the-last-suitable-value-thanks-to-reverse-iterators.mdx +++ b/content/docs/std/algo/ranges/_codes/find/example-Finding-the-last-suitable-value-thanks-to-reverse-iterators.mdx @@ -20,7 +20,7 @@ auto main() -> int }; auto const first1 = std::ranges::find(numbers, 1); - auto const last1 = std::ranges::find(numbers.rbegin(), numbers.rend(), 1); + auto const last1 = std::ranges::find(numbers.rbegin(), numbers.rend(), 1); println(numbers); *first1 = 9; diff --git a/content/docs/std/algo/ranges/_codes/find/since-cpp20-simplified.mdx b/content/docs/std/algo/ranges/_codes/find/since-cpp20-simplified.mdx index 8715d00ad..c2294dd6d 100644 --- a/content/docs/std/algo/ranges/_codes/find/since-cpp20-simplified.mdx +++ b/content/docs/std/algo/ranges/_codes/find/since-cpp20-simplified.mdx @@ -7,18 +7,22 @@ constexpr I constexpr ranges::borrowed_iterator_t find( R&& r, const T& value, Proj proj = {} ); ``` +
+Additional information regarding parameter constraints The type of arguments are generic and have the following constraints: - `I` - `std::input_iterator` - `S` - `std::sentinel_for` - `T` - **(none)** - `Proj` - **(none)** -- **(2)** - `R` - `std::ranges::input_range` +- $(2)$ - `R` - `std::ranges::input_range` The `Proj` template argument has a default type of `std::identity` for all overloads. Additionally, each overload has the following constraints: -- **(1)** - `indirect_binary_predicate, const T*>>` -- **(2)** - `indirect_binary_predicate, Proj>, const T*>` +- $(1)$ - `indirect_binary_predicate, const T*>>` +- $(2)$ - `indirect_binary_predicate, Proj>, const T*>` (The `std::` namespace was ommitted here for readability) + +
\ No newline at end of file diff --git a/content/docs/std/algo/ranges/find.mdx b/content/docs/std/algo/ranges/find.mdx index a77427983..219ca562c 100644 --- a/content/docs/std/algo/ranges/find.mdx +++ b/content/docs/std/algo/ranges/find.mdx @@ -49,11 +49,9 @@ If there is no suitable value, i.e., if the finding process fails, the algorithm } }} /> -Returns an iterator to the first element in the range satisfiying specific criteria (or `last` iterator if there is no such iterator): +- $(1)$ Searches for an element equal to `value` (using `operator==`) in a range represented as $[\texttt{first}, \texttt{last})$. Returns `last` if no such element was found. -- **(1)** Searches for an element equal to `value` (using `operator==`) - -- **(2)** Same as **(1)**, but uses `r` as the source range, as if using `ranges::begin(r)` as `first` and `ranges::end(r)` as `last`. +- $(2)$ Same as $(1)$, but uses `r` as the source range, as if using `ranges::begin(r)` as `first` and `ranges::end(r)` as `last`. The function-like entities described on this page are [**niebloids**](/docs/std/algo/niebloids). @@ -82,7 +80,7 @@ Iterator to the first element satisfying the condition or iterator equal to `las ### Complexity -Exactly `last - first` applications of the predicate and projection. +Linear. Exactly `last - first` applications of the predicate and projection. ### Exceptions @@ -130,7 +128,7 @@ inline constexpr find_fn find; }> ```cpp -auto const values = std::vector{2, 4, 6, 1, 8, 10}; +auto const values = std::vector{2, 4, 6, 1, 8, 10}; auto const iteratorTo1 = std::ranges::find(values, 1); std::cout << *iteratorTo1; ``` @@ -147,7 +145,7 @@ std::cout << *iteratorTo1; ```cpp auto values = std::vector{2, 4, 6, 1, 8, 10}; -auto text = std::string("hello, there!"); +auto text = std::string("hello, there!"); println(values); std::cout << text << '\n'; @@ -296,7 +294,7 @@ auto numbers = std::vector{ }; auto const first1 = std::ranges::find(numbers, 1); -auto const last1 = std::ranges::find(numbers.rbegin(), numbers.rend(), 1); +auto const last1 = std::ranges::find(numbers.rbegin(), numbers.rend(), 1); println(numbers); *first1 = 9; From 6b4c941ea919f5744b45051025a30564624ae874 Mon Sep 17 00:00:00 2001 From: Fureeish Date: Mon, 7 Apr 2025 17:15:33 +0200 Subject: [PATCH 8/9] Add projection explanation --- content/docs/std/algo/ranges/find.mdx | 3 +++ 1 file changed, 3 insertions(+) diff --git a/content/docs/std/algo/ranges/find.mdx b/content/docs/std/algo/ranges/find.mdx index 219ca562c..cfd94f982 100644 --- a/content/docs/std/algo/ranges/find.mdx +++ b/content/docs/std/algo/ranges/find.mdx @@ -55,6 +55,9 @@ If there is no suitable value, i.e., if the finding process fails, the algorithm The function-like entities described on this page are [**niebloids**](/docs/std/algo/niebloids). +The elements are passed through the projection `proj` before being compared. +However, if no custom projection is specified (i.e., the default one (`std::identity`) is used), passing them through it doesn't do anything. + ### Parameters From 34f42f5d066daf1ed6a904ed484b33ef8ab19d72 Mon Sep 17 00:00:00 2001 From: Fureeish Date: Mon, 7 Apr 2025 17:16:43 +0200 Subject: [PATCH 9/9] Apply inline code highlighting to the title --- content/docs/std/algo/ranges/find.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/docs/std/algo/ranges/find.mdx b/content/docs/std/algo/ranges/find.mdx index cfd94f982..abefa5c1a 100644 --- a/content/docs/std/algo/ranges/find.mdx +++ b/content/docs/std/algo/ranges/find.mdx @@ -29,7 +29,7 @@ import FullCode_FindingTheLastSuitableValueThanksToReverseIterators import FullCode_FailingToFindAValueSafelyWhenDealingWithAnIteratorPairSubrange from './_codes/find/example-Failing-to-find-a-value-safely-when-dealing-with-an-iterator-pair-subrange.mdx'; import FullCode_FailingToFindAValueWhenDealingWithAnIteratorPairSubrangeUnsafely from './_codes/find/example-Failing-to-find-a-value-when-dealing-with-an-iterator-pair-subrange-unsafely.mdx'; -# std::ranges::find() algorithm +# std::ranges::find() algorithm ### Explanation