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

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

主站蜘蛛池模板: 中超| 平度市| 海宁市| 无锡市| 婺源县| 襄樊市| 滁州市| 义乌市| 桃园县| 乌鲁木齐县| 黄骅市| 元氏县| 广宁县| 定陶县| 扎兰屯市| 沙河市| 蕲春县| 积石山| 敦煌市| 德庆县| 金湖县| 桓仁| 蕉岭县| 丽江市| 永川市| 宿州市| 德格县| 洛宁县| 乌海市| 彭水| 临猗县| 隆子县| 洪洞县| 孟村| 博爱县| 大厂| 日土县| 凤庆县| 顺平县| 尉犁县| 集贤县|