C++ Container Class ]\\n\\n\\n\\nbegin and end are the most important iterator functions in vector, used to o..."> C++ Container Class ]\\n\\n\\n\\nbegin and end are the most important iterator functions in vector, used to o...">

Cpp Libs Vector Begin End

[<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.

\\n\\n

begin and end are member functions of the container class:

\\n\\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
\\n\\n

These two functions are the foundation of C++ standard library algorithms and range-based for loops.

\\n\\n

Word meanings: 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 requires no parameters.

\\n\\n

Syntax Format

\\n\\n
iterator begin();\\nconst_iterator begin() const;\\niterator end();\\nconst_iterator end() const;\\n
\\n\\n

Parameter Description

\\n\\n
    \\n
  • Parameters: None\\n
      \\n
    • These functions do not accept any parameters.
    • \\n
  • \\n
\\n\\n

Function Description

\\n\\n
    \\n
  • Return value:\\n
      \\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 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
\\n\\n

Examples

\\n\\n

Let'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 <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\\n

Expected Output:

\\n\\n
Traverse using iterators: 10 20 30 40 50\\n
\\n\\n

Code Analysis:

\\n\\n
    \\n
  1. numbers.begin() returns an iterator pointing to the first element (10).
  2. \\n
  3. numbers.end() returns an iterator pointing to the position after the last element.
  4. \\n
  5. The loop condition it != numbers.end() ensures all elements are traversed.
  6. \\n
\\n\\n

Example 2: Range-based For Loop

\\n\\n

The range-based for loop introduced in C++11 uses begin and end under the hood.

\\n\\n

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\\n

Expected Output:

\\n\\n
Use range-based for loop: Alice Bob Charlie\\n
\\n\\n

Code Analysis:

\\n\\n
    \\n
  • The range-based for loop is a concise syntax for using iterators.
  • \\n
  • The compiler automatically calls begin() and end().
  • \\n
\\n\\n

Example 3: Using STL Algorithms

\\n\\n

Standard library algorithms require iterators to specify ranges.

\\n\\n

Example

\\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\\n

Expected Output:

\\n\\n
After sorting: 1 2 3 5 8 9Found element: 8\\n
\\n\\n

Code Analysis:

\\n\\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\\n

Example 4: Reverse Iterators

\\n\\n

Using rbegin and rend allows reverse traversal of the container.

\\n\\n

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\\n

Expected Output:

\\n\\n
Forward: 1 2 3 4 5Reverse: 5 4 3 2 1\\n
\\n\\n

Code Analysis:

\\n\\n
    \\n
  • rbegin() returns a reverse iterator pointing to the last element.
  • \\n
  • rend() returns a reverse iterator pointing to before the first element.
  • \\n
\\n\\n

Example 5: Iterator Arithmetic

\\n\\n

Iterators support arithmetic operations, allowing quick jumps to specified positions.

\\n\\n

Example

\\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\\n

Expected Output:

\\n\\n
begin() + 2 = 30Container size (distance): 7Middle element: 40\\n
\\n\\n

Code Analysis:

\\n\\n
    \\n
  • Iterators support arithmetic operations such as +, -.
  • \\n
  • You can use end() - begin() to calculate the container size.
  • \\n
\\n\\n

Example 6: Conversion Between Iterators and Indices

\\n\\n

Iterators and indices can be converted to each other.

\\n\\n

Example

\\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\\n

Expected Output:

\\n\\n
Index 2 Corresponding element: 30Index corresponding to iterator: 3\\n
\\n\\n

Code Analysis:

\\n\\n
    \\n
  • begin() + index converts an index to an iterator.
  • \\n
  • iter - begin() converts an iterator to an index.
  • \\n
\\n\\n

Example 7: Using Iterators to Modify Elements

\\n\\n

Iterators return references and can be used to modify elements.

\\n\\n

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    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\\n

Expected Output:

\\n\\n
Before modification: 1 2 3 4 5After modification: 2 4 6 8 10\\n
\\n\\n

Code 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
\\n\\n

[<img alt="Image 2: C++ Container Class "> C++ Container Class <vector>]

← Cpp Libs Array BackCpp Libs Vector Insert β†’

YouTip © 2024-2026 | Home | Learn Technology, Build Dreams!

All content is for educational and learning purposes only.