[<img alt="Image 1: C++ Container Class "> C++ Container Class <vector>]
\\n\\n\\n\\n
begin and end are the most important iterator functions in vector, used to obtain the starting and ending iterators of the container.
begin and end are member functions of the container class:
- \\n
beginreturns an iterator pointing to the first element of the container \\nendreturns an iterator pointing to the position after the last element of the container (sentinel iterator) \\n
These two functions are the foundation of C++ standard library algorithms and range-based for loops.
\\n\\nWord meanings: begin means "start", end means "end".
\\n\\n
Basic Syntax and Parameters
\\n\\nbegin and end are member functions of the container class, and calling them requires no parameters.
Syntax Format
\\n\\niterator begin();\\nconst_iterator begin() const;\\niterator end();\\nconst_iterator end() const;\\n\\n\\nParameter Description
\\n\\n- \\n
- Parameters: None\\n
- \\n
- These functions do not accept any parameters. \\n
\\n
Function Description
\\n\\n- \\n
- Return value:\\n
- \\n
begin()returns an iterator pointing to the first element. \\nend()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 to work with algorithms. \\n
- Note: The iterator returned by
end()points to a "virtual" position and cannot be dereferenced, only used for comparison. \\n
\\n\\n
Examples
\\n\\nLet's thoroughly master the usage of begin and end through a series of examples.
Example 1: Basic Usage - Traversing a Container
\\n\\nExample
\\n\\n#include <iostream>\\n\\n#include <vector>\\n\\nint main(){\\n\\n std::vector<int> numbers ={10, 20, 30, 40, 50};\\n\\n // Traverse using iterators\\n std::cout<<"Traverse using iterators: ";\\n for(auto it = numbers.begin(); it != numbers.end();++it){\\n std::cout<<*it <<" ";\\n }\\n std::cout<< std::endl;\\n\\n return 0;\\n}\\n\\n\\nExpected Output:
\\n\\nTraverse using iterators: 10 20 30 40 50\\n\\n\\nCode Analysis:
\\n\\n- \\n
numbers.begin()returns an iterator pointing to the first element (10). \\nnumbers.end()returns an iterator pointing to the position after the last element. \\n- The loop condition
it != numbers.end()ensures all elements are traversed. \\n
Example 2: Range-based For Loop
\\n\\nThe range-based for loop introduced in C++11 uses begin and end under the hood.
Example
\\n\\n#include <iostream>\\n\\n#include <vector>\\n\\n#include <string>\\n\\nint main(){\\n\\n std::vector<std::string> names ={"Alice", "Bob", "Charlie"};\\n\\n // Range-based for loop (internally uses begin/endοΌ\\n std::cout<<"Use range-based for loop: ";\\n for(const auto& name : names){\\n std::cout<< name <<" ";\\n }\\n std::cout<< std::endl;\\n\\n return 0;\\n}\\n\\n\\nExpected Output:
\\n\\nUse range-based for loop: Alice Bob Charlie\\n\\n\\nCode Analysis:
\\n\\n- \\n
- The range-based for loop is a concise syntax for using iterators. \\n
- The compiler automatically calls
begin()andend(). \\n
Example 3: Using STL Algorithms
\\n\\nStandard library algorithms require iterators to specify ranges.
\\n\\nExample
\\n\\n#include <iostream>\\n\\n#include <vector>\\n\\n#include <algorithm>\\n\\nint main(){\\n\\n std::vector<int> numbers ={5, 2, 8, 1, 9, 3};\\n\\n // Sort\\n std::sort(numbers.begin(), numbers.end());\\n\\n std::cout<<"After sorting: ";\\n for(int n : numbers){\\n std::cout<< n <<" ";\\n }\\n std::cout<< std::endl;\\n\\n // Find element\\n auto it = std::find(numbers.begin(), numbers.end(), 8);\\n if(it != numbers.end()){\\n std::cout<<"Found element: "<<*it << std::endl;\\n }\\n\\n return 0;\\n}\\n\\n\\nExpected Output:
\\n\\nAfter sorting: 1 2 3 5 8 9Found element: 8\\n\\n\\nCode Analysis:
\\n\\n- \\n
std::sort(begin, end)uses iterators to specify the sorting range. \\nstd::find(begin, end, value)uses iterators to specify the search range. \\n
Example 4: Reverse Iterators
\\n\\nUsing rbegin and rend allows reverse traversal of the container.
Example
\\n\\n#include <iostream>\\n\\n#include <vector>\\n\\nint main(){\\n\\n std::vector<int> numbers ={1, 2, 3, 4, 5};\\n\\n // ForwardIterate\\n std::cout<<"Forward: ";\\n for(auto it = numbers.begin(); it != numbers.end();++it){\\n std::cout<<*it <<" ";\\n }\\n std::cout<< std::endl;\\n\\n // Reverse traversal (using rbegin/rendοΌ\\n std::cout<<"Reverse: ";\\n for(auto it = numbers.rbegin(); it != numbers.rend();++it){\\n std::cout<<*it <<" ";\\n }\\n std::cout<< std::endl;\\n\\n return 0;\\n}\\n\\n\\nExpected Output:
\\n\\nForward: 1 2 3 4 5Reverse: 5 4 3 2 1\\n\\n\\nCode Analysis:
\\n\\n- \\n
rbegin()returns a reverse iterator pointing to the last element. \\nrend()returns a reverse iterator pointing to before the first element. \\n
Example 5: Iterator Arithmetic
\\n\\nIterators support arithmetic operations, allowing quick jumps to specified positions.
\\n\\nExample
\\n\\n#include <iostream>\\n\\n#include <vector>\\n\\nint main(){\\n\\n std::vector<int> numbers ={10, 20, 30, 40, 50, 60, 70};\\n\\n // Get third element (index 2)\\n auto it = numbers.begin()+2;\\n std::cout<<"begin() + 2 = "<<*it << std::endl;\\n\\n // Calculate distance between two iterators\\n auto first = numbers.begin();\\n auto last = numbers.end();\\n auto distance = last - first;\\n std::cout<<"Container size (distance): "<< distance << std::endl;\\n\\n // Get Middle element\\n auto mid = numbers.begin()+(numbers.size()/2);\\n std::cout<<"Middle element: "<<*mid << std::endl;\\n\\n return 0;\\n}\\n\\n\\nExpected Output:
\\n\\nbegin() + 2 = 30Container size (distance): 7Middle element: 40\\n\\n\\nCode Analysis:
\\n\\n- \\n
- Iterators support arithmetic operations such as
+,-. \\n - You can use
end() - begin()to calculate the container size. \\n
Example 6: Conversion Between Iterators and Indices
\\n\\nIterators and indices can be converted to each other.
\\n\\nExample
\\n\\n#include <iostream>\\n\\n#include <vector>\\n\\nint main(){\\n\\n std::vector<int> numbers ={10, 20, 30, 40, 50};\\n\\n // Convert index to iterator\\n size_t index =2;\\n auto it = numbers.begin()+ index;\\n std::cout<<"Index "<< index <<" Corresponding element: "<<*it << std::endl;\\n\\n // Convert iterator to index\\n auto iter = numbers.begin()+3;\\n size_t idx = iter - numbers.begin();\\n std::cout<<"Index corresponding to iterator: "<< idx << std::endl;\\n\\n return 0;\\n}\\n\\n\\nExpected Output:
\\n\\nIndex 2 Corresponding element: 30Index corresponding to iterator: 3\\n\\n\\nCode Analysis:
\\n\\n- \\n
begin() + indexconverts an index to an iterator. \\niter - begin()converts an iterator to an index. \\n
Example 7: Using Iterators to Modify Elements
\\n\\nIterators return references and can be used to modify elements.
\\n\\nExample
\\n\\n#include <iostream>\\n\\n#include <vector>\\n\\nint main(){\\n\\n std::vector<int> numbers ={1, 2, 3, 4, 5};\\n\\n std::cout<<"Before modification: ";\\n for(int n : numbers) std::cout<< n <<" ";\\n std::cout<< std::endl;\\n\\n // Multiply all elements by 2 using iterators\\n for(auto it = numbers.begin(); it != numbers.end();++it){\\n *it *=2;\\n }\\n\\n std::cout<<"After modification: ";\\n for(int n : numbers) std::cout<< n <<" ";\\n std::cout<< std::endl;\\n\\n return 0;\\n}\\n\\n\\nExpected Output:
\\n\\nBefore modification: 1 2 3 4 5After modification: 2 4 6 8 10\\n\\n\\nCode Analysis:
\\n\\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\\n
[<img alt="Image 2: C++ Container Class "> C++ Container Class <vector>]
YouTip