Algorithms Expect Iterators
Stryg for at vise menuen
An iterator range in C++ is defined by two iterators: one pointing to the first element and one pointing just past the last. STL algorithms operate on these ranges, allowing them to work on any part of a container or any iterable sequence described by iterators.
STL algorithms work on iterator ranges, not containers. By passing begin() and end(), you specify the range to process, allowing the same algorithm to work with any container that provides compatible iterators.
main.cpp
1234567891011121314151617181920212223#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> numbers = {5, 2, 8, 1, 9}; // Sort the entire vector using iterators std::sort(numbers.begin(), numbers.end()); // Find the value 8 using iterators auto it = std::find(numbers.begin(), numbers.end(), 8); std::cout << "Sorted vector: "; for (int n : numbers) std::cout << n << ' '; std::cout << '\n'; if (it != numbers.end()) std::cout << "Found value: " << *it << '\n'; else std::cout << "Value not found\n"; }
In the code above, you first create a std::vector<int> containing several numbers. The std::sort algorithm is called with numbers.begin() and numbers.end(), which mark the start and end of the range to sort—here, the entire vector. The algorithm does not need to know that it is working on a vector; it simply processes the elements between the two iterators.
Next, std::find is used with the same iterator range to search for the value 8. Again, the algorithm only requires the start and end iterators, not the container itself. The result is an iterator pointing to the found element, or to numbers.end() if the value is not found. This demonstrates how algorithms can operate generically on any sequence defined by iterators.
main.cpp
12345678910111213141516#include <iostream> #include <list> #include <algorithm> int main() { std::list<int> numbers = {3, 7, 4, 6, 2}; // Use std::find with iterators on a list auto it = std::find(numbers.begin(), numbers.end(), 4); if (it != numbers.end()) std::cout << "Found value: " << *it << '\n'; else std::cout << "Value not found\n"; }
Using std::find on a std::list works exactly the same way as with a std::vector: you provide numbers.begin() and numbers.end() to define the range to search. The algorithm does not care that the container is a list rather than a vector. This flexibility is the direct result of using iterator ranges rather than container-specific interfaces.
By relying on iterators, STL algorithms can be reused with any compatible container, or even with custom data structures that implement the necessary iterator interface. This enables you to write code that is both more reusable and more adaptable to different scenarios.
Different STL algorithms require different iterator capabilities. For example, std::sort needs random access iterators and works with std::vector, but not std::list. In contrast, std::find only requires input iterators, so it works with many containers. Always check an algorithm's iterator requirements to ensure compatibility.
Tak for dine kommentarer!
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat