--- id: cpp-algorithms title: "C++ Algorithms" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: [" library", "sort find C++", "C++ μ•Œκ³ λ¦¬μ¦˜"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.86 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["cpp", "programming-language", "w3schools", "stl", "algorithm"] raw_sources: ["https://www.w3schools.com/cpp/cpp_algorithms.asp"] applied_in: [] github_commit: "" --- # [[CPP Algorithms]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) Every `` function takes a RANGE (a begin/end iterator pair) instead of a container, which is why `sort(numbers.begin() + 3, numbers.end())` can sort just a SUBSET of a vector by simple iterator arithmetic β€” this is the payoff of the previous chapter's iterator abstraction: because `sort()`/`find()`/`min_element()` never see the container type directly, the exact same function signature works whether the range came from a full vector, a partial vector, or (implicitly) any other iterator-supporting container, something C has no equivalent for since `qsort()` needs a raw array pointer + element count instead of a reusable "range" concept. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **`` header** β€” required for all functions in this chapter (`sort`, `find`, `upper_bound`, `min_element`, `max_element`, `copy`, `fill`). [S1] - **`sort(begin, end)`** β€” sorts a range in ascending order by default (alphabetically for strings, numerically for ints); reversible via `rbegin()`/`rend()` instead of `begin()`/`end()`. [S1] - **Partial-range sort** β€” `sort(numbers.begin() + 3, numbers.end())` sorts only from the 4th element onward, leaving earlier elements untouched β€” only possible because the function operates on an iterator range, not "the whole container." [S1] - **`find(begin, end, value)`** β€” returns an iterator to the first match (or `end()` if not found, implied by the general iterator convention). [S1] - **`upper_bound(begin, end, value)`** β€” finds the first element GREATER than `value`; REQUIRES the range to already be sorted, since it relies on binary-search-like assumptions. [S1] - **`min_element(begin, end)` / `max_element(begin, end)`** β€” return an iterator to the smallest/largest element in the range. [S1] - **`copy(src_begin, src_end, dest_begin)`** β€” copies a range of elements into another container starting at `dest_begin`; the destination must already have enough space allocated (e.g. `vector copiedNumbers(6);`). [S1] - **`fill(begin, end, value)`** β€” overwrites every element in the range with `value`. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - Ascending sort: `sort(cars.begin(), cars.end());` β€” strings sort alphabetically, ints sort numerically. [S1] - Descending sort via reverse iterators: `sort(numbers.rbegin(), numbers.rend());`. [S1] - Search: `auto it = find(numbers.begin(), numbers.end(), 3);` finds the value 3. [S1] - Sorted-data search: `sort(numbers.begin(), numbers.end()); auto it = upper_bound(numbers.begin(), numbers.end(), 5);` β€” must sort FIRST, since `upper_bound` assumes sorted input. [S1] - Min/max: `auto it = min_element(numbers.begin(), numbers.end());` / `auto it = max_element(numbers.begin(), numbers.end());`. [S1] - Copy: `vector copiedNumbers(6); copy(numbers.begin(), numbers.end(), copiedNumbers.begin());`. [S1] - Fill: `vector numbers(6); fill(numbers.begin(), numbers.end(), 35);` sets all 6 elements to 35. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) - **λ²”μœ„(iterator pair) 기반 섀계가 λΆ€λΆ„ 정렬을 κ°€λŠ₯ν•˜κ²Œ 함**: C의 `qsort()`λŠ” λ°°μ—΄ 포인터와 전체 μš”μ†Œ 개수λ₯Ό λ°›μ•„μ•Ό ν–ˆμ§€λ§Œ, C++의 `sort()`λŠ” begin/end iterator만 λ°›μœΌλ―€λ‘œ `numbers.begin() + 3`처럼 iterator μ—°μ‚°λ§ŒμœΌλ‘œ μ»¨ν…Œμ΄λ„ˆμ˜ 일뢀 κ΅¬κ°„λ§Œ μ •λ ¬ν•  수 μžˆλ‹€λŠ” 점이 확인됨 β€” μ΄λŠ” 이전 챕터(Iterators)μ—μ„œ ν™•λ¦½λœ λ²”μœ„ μΆ”μƒν™”μ˜ 직접적인 ν™œμš© 사둀. [S1] - **`upper_bound()`λŠ” μ •λ ¬ μ „μ œ 쑰건이 있음**: `find()`λ‚˜ `min_element()`와 달리 `upper_bound()`λŠ” μ •λ ¬λ˜μ§€ μ•Šμ€ 데이터에 μ‚¬μš©ν•˜λ©΄ μ˜¬λ°”λ₯Έ κ²°κ³Όλ₯Ό 보μž₯ν•˜μ§€ μ•ŠμœΌλ©°, 원문도 λ°˜λ“œμ‹œ λ¨Όμ € `sort()`λ₯Ό ν˜ΈμΆœν•œ λ’€ μ‚¬μš©ν•˜λ„λ‘ λͺ…μ‹œν•¨. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) μ •λ ¬λ˜μ§€ μ•Šμ€ μ •μˆ˜ vectorλ₯Ό λ¨Όμ € sort()둜 μ •λ ¬ν•œ λ’€ upper_bound()둜 νŠΉμ • 값보닀 큰 첫 μ›μ†Œλ₯Ό μ°ΎλŠ” 2단계 μ˜ˆμ œκ°€ μ›λ¬Έμ—μ„œ 직접 μ‹€μ „ ν™œμš© μ‚¬λ‘€λ‘œ μ œμ‹œλ¨. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Partial-range sort and sorted-precondition search (C++): ```cpp #include #include vector numbers = {1, 7, 3, 5, 9, 2}; sort(numbers.begin() + 3, numbers.end()); // sorts only 5,9,2 -> leaves 1,7,3 untouched sort(numbers.begin(), numbers.end()); // must sort first... auto it = upper_bound(numbers.begin(), numbers.end(), 5); // ...for upper_bound to be valid auto smallest = min_element(numbers.begin(), numbers.end()); auto largest = max_element(numbers.begin(), numbers.end()); ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.86 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[C++ Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[CPP Iterators]], [[CPP Vectors]], [[CPP Data Structures]] - **μ°Έμ‘° λ§₯락:** 데이터 ꡬ쑰(STL) μ„Ήμ…˜μ˜ λ§ˆμ§€λ§‰ 챕터 β€” Auto μ„Ήμ…˜μœΌλ‘œ 이어짐. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” C++ Algorithms β€” https://www.w3schools.com/cpp/cpp_algorithms.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "C++ Algorithms" page (Astra wiki-curation, P-Reinforce v3.1 format).