C++ <array> begin / end function
beginandendIt is the most important iterator function in array, used toObtain the begin and end iterators of the container.。
beginandendare member functions of the container class:
beginreturns an iterator to the first element of the containerIteratorendReturns an iterator to the position after the last element of the container.Iterator(Sentinel iterator)
These two functions are the foundation of the C++ standard library algorithms and range-based for loops.
Word Definitions: beginIndicates "begin",endIndicates "end".
Basic syntax and parameters
beginandendThey are member functions of the container class, and calling them requires no arguments.
Syntax format
iterator begin() noexcept; const_iterator begin() const noexcept; iterator end() noexcept; const_iterator end() const noexcept;
Parameter description
- Parameter: No parameters
- These functions do not accept any parameters.
Function description
- Return Value:
begin()Returns an iterator to the first element.end()Returns an iterator (sentinel) to the position after the last element.
- Effect: The returned iterator can be used to traverse the container or work with algorithms.
- Note:
end()The returned iterator points to a "virtual" position, cannot be dereferenced, and can only be used for comparison.
Example
Let us, through a series of examples, thoroughly masterbeginandendUsage.
Example 1: Basic usage - traversing containers
Example
#include <array>
int main() {
std::array<int, 5> numbers = {10, 20, 30, 40, 50};
// Traverse using an iterator
std::cout << "Traverse using an iterator: ";
for(auto it = numbers.begin(); it != numbers.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
return 0;
}
Expected output:
使用迭代器遍历: 10 20 30 40 50
Code analysis:
numbers.begin()Returns an iterator pointing to the first element (10).numbers.end()Returns an iterator to the position after the last element.- Loop condition
it != numbers.end()Ensures traversal of all elements.
Example 2: Range-based for loop
The range-based for loop introduced in C++11 internally usesbeginandend。
Example
#include <array>
#include <string>
int main() {
std::array<std::string, 3> names = {"Alice", "Bob", "Charlie"};
// Range-based for loop (internally uses begin/end)
std::cout << "Use range-based for loop: ";
for(const auto& name : names) {
std::cout << name << " ";
}
std::cout << std::endl;
return 0;
}
Expected output:
使用范围 for 循环: Alice Bob Charlie
Code analysis:
- The range-based for loop is a concise way of using iterators.
- The compiler automatically calls
begin()andend()。
Example 3: Using STL algorithms
Standard library algorithms need to use iterators to specify ranges.
Example
#include <array>
#include <algorithm>
int main() {
std::array<int, 6> numbers = {5, 2, 8, 1, 9, 3};
// Sort
std::sort(numbers.begin(), numbers.end());
std::cout << "After sorting: ";
for(int n : numbers) {
std::cout << n << " ";
}
std::cout << std::endl;
// Find element
auto it = std::find(numbers.begin(), numbers.end(), 8);
if(it != numbers.end()) {
std::cout << "Found element: " << *it << std::endl;
}
return 0;
}
Expected output:
排序后: 1 2 3 5 8 9 找到元素: 8
Code analysis:
std::sort(begin, end)Use iterators to specify the sorting range.std::find(begin, end, value)Use iterators to specify the search range.
Example 4: Reverse iterators
UsagerbeginandrendCan traverse the container in reverse.
Example
#include <array>
int main() {
std::array<int, 5> numbers = {1, 2, 3, 4, 5};
// Forward traversal
std::cout << "Forward: ";
for(auto it = numbers.begin(); it != numbers.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
// Reverse traversal (using rbegin/rend)
std::cout << "Reverse: ";
for(auto it = numbers.rbegin(); it != numbers.rend(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
return 0;
}
Expected output:
正向: 1 2 3 4 5 反向: 5 4 3 2 1
Code analysis:
rbegin()Returns a reverse iterator pointing to the last element.rend()Returns a reverse iterator pointing to the position before the first element.
Example 5: Iterator arithmetic
Iterators support arithmetic operations, allowing quick jumps to a specified position.
Example
#include <array>
int main() {
std::array<int, 7> numbers = {10, 20, 30, 40, 50, 60, 70};
// Get the third element (index 2)
auto it = numbers.begin() + 2;
std::cout << "begin() + 2 = " << *it << std::endl;
// Calculate the distance between two iterators
auto first = numbers.begin();
auto last = numbers.end();
auto distance = last - first;
std::cout << "Container size (distance): " << distance << std::endl;
// Get the middle element
auto mid = numbers.begin() + (numbers.size() / 2);
std::cout << "Middle element: " << *mid << std::endl;
return 0;
}
Expected output:
begin() + 2 = 30 容器大小(距离): 7 中间元素: 40
Code analysis:
- Iterator support
+、-Arithmetic operations, etc. - You can use
end() - begin()Compute the container size.
Example 6: Using iterators to modify elements
The iterator returns a reference, which can be used to modify elements.
Example
#include <array>
int main() {
std::array<int, 5> numbers = {1, 2, 3, 4, 5};
std::cout << "Before modification: ";
for(int n : numbers) std::cout << n << " ";
std::cout << std::endl;
// Multiply all elements by 2 using iterators
for(auto it = numbers.begin(); it != numbers.end(); ++it) {
*it *= 2;
}
std::cout << "After modification: ";
for(int n : numbers) std::cout << n << " ";
std::cout << std::endl;
return 0;
}
Expected output:
修改前: 1 2 3 4 5 修改后: 2 4 6 8 10
Code analysis:
- Iterator dereferencing (
*it) returns a reference to the element. - You can directly modify this reference to change the value of the element.
other extensions