官术网_书友最值得收藏!

  • 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.)

主站蜘蛛池模板: 万载县| 朝阳区| 行唐县| 石首市| 新兴县| 长白| 玛沁县| 乐业县| 忻州市| 东乡族自治县| 郓城县| 滨海县| 阿城市| 新化县| 惠安县| 奇台县| 三亚市| 丰宁| 湛江市| 丁青县| 泾源县| 清苑县| 南康市| 枝江市| 三亚市| 航空| 伊川县| 青川县| 枞阳县| 谷城县| 兴山县| 浦县| 洪雅县| 泸溪县| 潜江市| 隆化县| 民乐县| 奎屯市| 延安市| 石河子市| 苍梧县|