C++ begin()
The begin() function is a member function used with C++ array containers and the standard std::begin() function that returns an iterator pointing to the first element of an array or array-like container. It provides a standardized way to access the beginning of containers, enabling iteration through elements and compatibility with range-based for loops and STL algorithms.
Syntax
For std::array containers:
arrayname.begin()
For regular arrays using std::begin():
std::begin(arrayname)
Parameters:
No parameters are required.
Returns:
Returns an iterator (random-access for std::array) pointing to the first element.
Example 1: Basic Iterator Usage with begin()
This example demonstrates the basic usage of begin() with std::array to iterate through elements:
#include <iostream>#include <array>int main() {// Create an array with 5 elementsstd::array<int, 5> numbers = {10, 20, 30, 40, 50};// Use begin() to get iterator to first elementauto it = numbers.begin();std::cout << "First element: " << *it << std::endl;// Iterate through all elements using begin() and end()for (auto iter = numbers.begin(); iter != numbers.end(); ++iter) {std::cout << *iter << " ";}std::cout << std::endl;return 0;}
The output of this code is:
First element: 1010 20 30 40 50
Example 2: Range-Based for Loop With begin()
This example shows how begin() works behind the scenes with range-based for loops in real-world scenarios like processing student grades:
#include <iostream>#include <array>int main() {// Student test scoresstd::array<int, 6> scores = {85, 92, 78, 96, 88, 91};// Calculate average using range-based for loop// (which internally uses begin() and end())int total = 0;for (const auto& score : scores) {total += score;}double average = static_cast<double>(total) / scores.size();std::cout << "Student Scores: ";for (const auto& score : scores) {std::cout << score << " ";}std::cout << std::endl;std::cout << "Average Score: " << average << std::endl;return 0;}
The output of this code is:
Student Scores: 85 92 78 96 88 91Average Score: 88.33
Codebyte Example: Using std::begin() with Regular Arrays
This example demonstrates using std::begin() with traditional C-style arrays for compatibility with STL algorithms:
Frequently Asked Questions
1. What is the difference between std::array data() and begin()?
data() returns a pointer to the underlying array data, providing direct access to the raw array elements. begin() returns an iterator to the first element, which is designed for safe iteration and compatibility with STL algorithms.
2. What does arr begin() return?
For std::array, begin() returns a random-access iterator, not just a bidirectional iterator. Random-access iterators are more powerful and support additional operations.
3. What does begin() do in a C++ array?
begin() provides a standardized way to obtain an iterator to the first element of an array container. It enables iteration through elements, compatibility with range-based for loops, and integration with STL algorithms for operations like sorting, searching, and transforming data.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C++ on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
- Beginner Friendly.11 hours