- Mastering the C++17 STL
- Arthur O'Dwyer
- 483字
- 2021-07-08 10:20:21
A pair of iterators defines a range
Now that we understand the fundamental concept of an iterator, let's put it to some practical use. We've already seen that if you have a pair of iterators as returned from begin() and end(), you can use a for-loop to iterate over all the elements of the underlying container. But more powerfully, you can use some pair of iterators to iterate over any sub-range of the container's elements! Let's say you only wanted to view the first half of a vector:
template<class Iterator>
void double_each_element(Iterator begin, Iterator end)
{
for (auto it = begin; it != end; ++it) {
*it *= 2;
}
}
int main()
{
std::vector<int> v {1, 2, 3, 4, 5, 6};
double_each_element(v.begin(), v.end());
// double each element in the entire vector
double_each_element(v.begin(), v.begin()+3);
// double each element in the first half of the vector
double_each_element(&v[0], &v[3]);
// double each element in the first half of the vector
}
Notice that in the first and second test cases in main() we pass in a pair of iterators derived from v.begin(); that is, two values of type std::vector::iterator. In the third test case, we pass in two values of type int*. Since int* satisfies all the requirements of an iterator type in this case--namely: it is incrementable, comparable, and dereferenceable--our code works fine even with pointers! This example demonstrates the flexibility of the iterator-pair model. (However, in general you should avoid messing around with raw pointers, if you're using a container such as std::vector that offers a proper iterator type. Use iterators derived from begin() and end() instead.)
We can say that a pair of iterators implicitly defines a range of data elements. And for a surprisingly large family of algorithms, that's good enough! We don't need to have access to the container in order to perform certain searches or transformations; we only need access to the particular range of elements being searched or transformed. Going further down this line of thought will eventually lead us to the concept of a non-owning view (which is to a data sequence as a C++ reference is to a single variable), but views and ranges are still more modern concepts, and we ought to finish up with the 1998-vintage STL before we talk about those things.
In the previous code sample, we saw the first example of a real STL-style generic algorithm. Admittedly, double_each_element is not a terribly generic algorithm in the sense of implementing a behavior that we might want to reuse in other programs; but this version of the function is now perfectly generic in the sense of operating only on pairs of Iterators, where Iterator can be any type in the world that implements incrementability, comparability, and dereferenceability. (We'll see a version of this algorithm that is more generic in that first sense in this book's next chapter, when we talk about std::transform.)
- Mastering Ext JS(Second Edition)
- Learning Apex Programming
- Learning Selenium Testing Tools with Python
- Cocos2d-x游戲開發(fā):手把手教你Lua語言的編程方法
- Mastering Concurrency in Go
- Vue.js入門與商城開發(fā)實(shí)戰(zhàn)
- Visual FoxPro程序設(shè)計(jì)
- Visual C#通用范例開發(fā)金典
- 大數(shù)據(jù)分析與應(yīng)用實(shí)戰(zhàn):統(tǒng)計(jì)機(jī)器學(xué)習(xí)之?dāng)?shù)據(jù)導(dǎo)向編程
- 微服務(wù)從小白到專家:Spring Cloud和Kubernetes實(shí)戰(zhàn)
- Python語言實(shí)用教程
- Python程序設(shè)計(jì)與算法基礎(chǔ)教程(第2版)(微課版)
- UI設(shè)計(jì)全書(全彩)
- Scratch·愛編程的藝術(shù)家
- WildFly Cookbook