Cpp Libs Array Swap Begin End
[ C++ Container Class ](#)\\n\\n* * *\\n\\n`begin` and `end` are the most important iterator functions in array, used to **get the starting and ending iterators of the container**.\\n\\n`begin` and `end` are member functions of the container class:\\n\\n* `begin` returns an **iterator** pointing to the first element of the container\\n* `end` returns an **iterator** pointing to the position after the last element of the container (sentinel iterator)\\n\\nThese two functions are the foundation of C++ standard library algorithms and range-based for loops.\\n\\n**Word Definition**: `begin` means "start", `end` means "end".\\n\\n* * *\\n\\n## Basic Syntax and Parameters\\n\\n`begin` and `end` are member functions of the container class, and calling them does not require parameters.\\n\\n### Syntax Format\\n\\niterator begin() noexcept; const_iterator begin() const noexcept; iterator end() noexcept; const_iterator end() const noexcept;\\n### Parameter Description\\n\\n* **Parameters**: No parameters\\n * These functions do not accept any parameters.\\n\\n### Function Description\\n\\n* **Return Value**: \\n * `begin()` returns an iterator pointing to the first element.\\n * `end()` returns an iterator pointing to the position after the last element (sentinel).\\n\\n* **Effect**: The returned iterators can be used to traverse the container or work with algorithms.\\n* **Note**: The iterator returned by `end()` points to a "virtual" position and cannot be dereferenced; it can only be used for comparison.\\n\\n* * *\\n\\n## Examples\\n\\nLet's thoroughly master the usage of `begin` and `end` through a series of examples.\\n\\n### Example 1: Basic Usage - Traversing a Container\\n\\n## Example\\n\\n#include \\n\\n#include \\n\\nint main(){\\n\\n std::array numbers ={10, 20, 30, 40, 50};\\n\\n// Traverse using iterators\\n\\n std::cout<<"Traverse using iterators: ";\\n\\nfor(auto it = numbers.begin(); it != numbers.end();++it){\\n\\n std::cout<<*it <<" ";\\n\\n}\\n\\n std::cout<< std::endl;\\n\\nreturn 0;\\n\\n}\\n\\n**Expected Output:**\\n\\nTraverse using iterators: 10 20 30 40 50\\n**Code Analysis:**\\n\\n1. `numbers.begin()` returns an iterator pointing to the first element (10).\\n2. `numbers.end()` returns an iterator pointing to the position after the last element.\\n3. The loop condition `it != numbers.end()` ensures that all elements are traversed.\\n\\n### Example 2: Range-based for Loop\\n\\nThe range-based for loop introduced in C++11 uses `begin` and `end` under the hood.\\n\\n## Example\\n\\n#include \\n\\n#include \\n\\n#include \\n\\nint main(){\\n\\n std::array names ={"Alice", "Bob", "Charlie"};\\n\\n// Range-based for loop (internally uses begin/endοΌ\\n\\n std::cout<<"Use range-based for loop: ";\\n\\nfor(const auto& name : names){\\n\\n std::cout<< name <<" ";\\n\\n}\\n\\n std::cout<< std::endl;\\n\\nreturn 0;\\n\\n}\\n\\n**Expected Output:**\\n\\nUse range-based for loop: Alice Bob Charlie\\n**Code Analysis:**\\n\\n* The range-based for loop is a concise way of writing using iterators.\\n* The compiler will automatically call `begin()` and `end()`.\\n\\n### Example 3: Using STL Algorithms\\n\\nStandard library algorithms require iterators to specify ranges.\\n\\n## Example\\n\\n#include \\n\\n#include \\n\\n#include \\n\\nint main(){\\n\\n std::array numbers ={5, 2, 8, 1, 9, 3};\\n\\n// Sort\\n\\n std::sort(numbers.begin(), numbers.end());\\n\\nstd::cout<<"SortPost: ";\\n\\nfor(int n : numbers){\\n\\n std::cout<< n <<" ";\\n\\n}\\n\\n std::cout<< std::endl;\\n\\n// Find element\\n\\nauto it = std::find(numbers.begin(), numbers.end(), 8);\\n\\nif(it != numbers.end()){\\n\\n std::cout<<"Found element: "<<*it << std::endl;\\n\\n}\\n\\nreturn 0;\\n\\n}\\n\\n**Expected Output:**\\n\\nSortPost: 1 2 3 5 8 9Found element: 8\\n**Code Analysis:**\\n\\n* `std::sort(begin, end)` uses iterators to specify the sorting range.\\n* `std::find(begin, end, value)` uses iterators to specify the search range.\\n\\n### Example 4: Reverse Iterators\\n\\nUsing `rbegin` and `rend` allows you to traverse the container in reverse.\\n\\n## Example\\n\\n#include \\n\\n#include \\n\\nint main(){\\n\\n std::array numbers ={1, 2, 3, 4, 5};\\n\\n// ForwardIterate\\n\\n std::cout<<"Forward: ";\\n\\nfor(auto it = numbers.begin(); it != numbers.end();++it){\\n\\n std::cout<<*it <<" ";\\n\\n}\\n\\n std::cout<< std::endl;\\n\\n// Reverse traversal (using rbegin/rendοΌ\\n\\n std::cout<<"Reverse: ";\\n\\nfor(auto it = numbers.rbegin(); it != numbers.rend();++it){\\n\\n std::cout<<*it <<" ";\\n\\n}\\n\\n std::cout<< std::endl;\\n\\nreturn 0;\\n\\n}\\n\\n**Expected Output:**\\n\\nForward: 1 2 3 4 5Reverse: 5 4 3 2 1\\n**Code Analysis:**\\n\\n* `rbegin()` returns a reverse iterator pointing to the last element.\\n* `rend()` returns a reverse iterator pointing to the position before the first element.\\n\\n### Example 5: Iterator Arithmetic\\n\\nIterators support arithmetic operations, allowing you to quickly jump to a specified position.\\n\\n## Example\\n\\n#include \\n\\n#include \\n\\nint main(){\\n\\n std::array numbers ={10, 20, 30, 40, 50, 60, 70};\\n\\n// Get third element (index 2)\\n\\nauto it = numbers.begin()+2;\\n\\n std::cout<<"begin() + 2 = "<<*it << std::endl;\\n\\n// Calculate distance between two iterators\\n\\nauto first = numbers.begin();\\n\\nauto last = numbers.end();\\n\\nauto distance = last - first;\\n\\n std::cout<<"Container size (distance): "<< distance << std::endl;\\n\\n// getMiddle element\\n\\nauto mid = numbers.begin()+(numbers.size()/2);\\n\\n std::cout<<"Middle element: "<<*mid << std::endl;\\n\\nreturn 0;\\n\\n}\\n\\n**Expected Output:**\\n\\nbegin() + 2 = 30Container size (distance): 7Middle element: 40\\n**Code Analysis:**\\n\\n* Iterators support arithmetic operations such as `+` and `-`.\\n* You can use `end() - begin()` to calculate the container size.\\n\\n### Example 6: Modifying Elements Using Iterators\\n\\nIterators return references, which can be used to modify elements.\\n\\n## Example\\n\\n#include \\n\\n#include \\n\\nint main(){\\n\\n std::array numbers ={1, 2, 3, 4, 5};\\n\\nstd::cout<<"Before modification: ";\\n\\nfor(int n : numbers) std::cout<< n <<" ";\\n\\n std::cout<< std::endl;\\n\\n// Multiply all elements by 2 using iterators\\n\\nfor(auto it = numbers.begin(); it != numbers.end();++it){\\n\\n*it *=2;\\n\\n}\\n\\nstd::cout<<"After modification: ";\\n\\nfor(int n : numbers) std::cout<< n <<" ";\\n\\n std::cout<< std::endl;\\n\\nreturn 0;\\n\\n}\\n\\n**Expected Output:**\\n\\nBefore modification: 1 2 3 4 5After modification: 2 4 6 8 10\\n**Code Analysis:**\\n\\n* Dereferencing an iterator (`*it`) returns a reference to the element.\\n* You can directly modify this reference to change the element's value.\\n\\n* * C++ Container Class ](#)
YouTip