C++ <list> begin / end Function
beginandendis the most important iterator function in list, used toGet the beginning and end iterators of the linked list。
beginandendare member functions of the container class:
beginReturns an iterator pointing to the first element of the linked listIteratorendreturn a position pointing to after the last element of the linked listIterator(Sentinel iterator)
These two functions are the foundation of C++ standard library algorithms and range-based for loops.
Word Definitions: beginIndicates "start",endIndicates "end".
Basic syntax and parameters
Syntax format
iterator begin(); const_iterator begin() const; iterator end(); const_iterator end() const;
Function description
- Return Value:
begin()Returns an iterator pointing to the first element,end()Returns the sentinel iterator.
Example
Example 1: Traversing a linked list
Example
#include <iostream>
#include <list>
int main() {
std::list<int> 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;
}
#include <list>
int main() {
std::list<int> 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 execution result:
使用迭代器遍历: 10 20 30 40 50
Example 2: Range-based for loop
Example
#include <iostream>
#include <list>
int main() {
std::list<std::string> names = {"Alice", "Bob", "Charlie"};
std::cout << "Range for loop: ";
for(const auto& name : names) {
std::cout << name << " ";
}
std::cout << std::endl;
return 0;
}
#include <list>
int main() {
std::list<std::string> names = {"Alice", "Bob", "Charlie"};
std::cout << "Range for loop: ";
for(const auto& name : names) {
std::cout << name << " ";
}
std::cout << std::endl;
return 0;
}
Expected execution result:
范围 for 循环: Alice Bob Charlie
Example 3: Using STL algorithms
Example
#include <iostream>
#include <list>
#include <algorithm>
int main() {
std::list<int> numbers = {5, 2, 8, 1, 9, 3};
// Sort
numbers.sort();
std::cout << "After sorting: ";
for(int n : numbers) std::cout << n << " ";
std::cout << std::endl;
// Find
auto it = std::find(numbers.begin(), numbers.end(), 8);
if(it != numbers.end()) {
std::cout << "Found: " << *it << std::endl;
}
return 0;
}
#include <list>
#include <algorithm>
int main() {
std::list<int> numbers = {5, 2, 8, 1, 9, 3};
// Sort
numbers.sort();
std::cout << "After sorting: ";
for(int n : numbers) std::cout << n << " ";
std::cout << std::endl;
// Find
auto it = std::find(numbers.begin(), numbers.end(), 8);
if(it != numbers.end()) {
std::cout << "Found: " << *it << std::endl;
}
return 0;
}
Expected execution result:
排序后: 1 2 3 5 8 9 找到: 8
Example 4: Reverse iterators
Example
#include <iostream>
#include <list>
int main() {
std::list<int> numbers = {1, 2, 3, 4, 5};
std::cout << "Forward: ";
for(auto it = numbers.begin(); it != numbers.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
std::cout << "Reverse: ";
for(auto it = numbers.rbegin(); it != numbers.rend(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
return 0;
}
#include <list>
int main() {
std::list<int> numbers = {1, 2, 3, 4, 5};
std::cout << "Forward: ";
for(auto it = numbers.begin(); it != numbers.end(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
std::cout << "Reverse: ";
for(auto it = numbers.rbegin(); it != numbers.rend(); ++it) {
std::cout << *it << " ";
}
std::cout << std::endl;
return 0;
}
Expected execution result:
正向: 1 2 3 4 5 反向: 5 4 3 2 1
other extensions