5

To obtain a pointer to the first and last elements of an array, we can use the begin and end functions: https://stackoverflow.com/a/14595314/5761266

However, I notice that the program is compiled just fine if I omit the #include <iterator>. Why is this the case since these functions are defined in the <iterator> header?

The only header I used is <iostream>.
The program I used: http://coliru.stacked-crooked.com/a/28b2b449aae19a47

Community
  • 1
  • 1
S Wang
  • 213
  • 3
  • 11
  • 2
    That header may already be included in one of the headers you do include. – Anon Mail Aug 12 '16 at 21:23
  • 3
    Very likely that in the C++ implementation you are using one of the other headers you have included includes ``. Do not expect this to be the same in other C++ implementations or even the next revision of the implementation you are using. – user4581301 Aug 12 '16 at 21:24
  • Standard headers are allowed to include other standard headers. – zneak Aug 12 '16 at 21:27
  • 1
    just include it, it has proper header guards anyway, so you're literally not saving anything by including it. If you think runtime will be affected, you're wrong because `#include` is a pre processor thing. – Ryan Aug 12 '16 at 21:27
  • You got (un)lucky that it was included by some `#include` set that eventually lead to what you *did* include. Don't assume implicit inclusion. Use what the standard says to use. If it says "blah" is declared in `` or ``, then take it as gospel to include at least one of those. Anything else is a crapshoot. – WhozCraig Aug 12 '16 at 21:28

5 Answers5

7

A function's return type must be fully defined beforehand. This means that the iterator header must be included with any iterable object that defines a begin() and end() method and returns a complete iterator object*.

What this means is that whenever you use a standard container (e.g. std::vector, std::map, std::list, etc) the standard library header files must #include <iterator> somewhere before the begin() and end() methods are declared.

Therefore you won't need to #include <iterator> yourself, because by doing #include <vector> (for example) you are automatically including iterator.h as well.

Remember that when you #include a header file, any other header files included within that header file are automatically #included in your code file as well.


*Unless the iterator type is completely custom and does not use standard iterator hierarchy categories.

Marc.2377
  • 7,807
  • 7
  • 51
  • 95
Karl Nicoll
  • 16,090
  • 3
  • 51
  • 65
  • Small correction: a hypothetical `vector` header might have its own overload of `begin` or only a forward declaration. It doesn't necessarily have to include anything. – user4581301 Aug 12 '16 at 21:44
  • Great answer. In fact I just discarded mine (before posting) to upvote it. In OP's case, most likely his implementation of `iostream` is based on `iterator` at some point. This is the case in Windows with MSVC and MinGW. And in both of them, it's possible to trace which files are including which. – Marc.2377 Aug 12 '16 at 22:19
2

It is very likely that in the C++ implementation you are using one of the other headers you have included includes <iterator>. Do not expect this to be the same in other C++ implementations or even the next revision of the implementation you are using.

Best practice is to always include all headers you need in whichever file needs them.

This prevents problems and mystery bugs in the future should an include be removed from another included header or what looks like an include turns out to merely be a forward declaration of key classes or functions.

This also assists greatly in portability. The next tool chain you use could be laid out differently, not to mention other programmers who may inherit your code or wish to use it in another project.

From the student coder point of view, it really sucks when you hand in code that compiles on your PC, but not the marker's.

This sounds silly and repetitive, but a properly written header has include guards to prevent the header from being included more than one in any translation unit.

This also leaves breadcrumbs for other developers who can see at a glance what headers are used by a given file. For this reason do not include everything just in case you might need it. Creating a monolithic list of unnecessary headers also slows compilation.

user4581301
  • 33,082
  • 7
  • 33
  • 54
1

As of C++14, the function templates

  • std::begin
  • std::end
  • std::cbegin
  • std::cend
  • std::rbegin
  • std::rend
  • std::crbegin
  • std::crend

are provided not only by <iterator>, but also by

  • <array>
  • <deque>
  • <forward_list>
  • <list>
  • <map>
  • <regex>
  • <set>
  • <string>
  • <unordered_map>
  • <unordered_set>
  • <vector>

As others have mentioned, other headers are also permitted to include <iterator>, but this is not guaranteed and may change from one implementation to another.

Brian Bi
  • 111,498
  • 10
  • 176
  • 312
  • Could you also provide a source, in the form of a link, for those of us interested on learning more about this change? – Marc.2377 Aug 12 '16 at 23:00
  • @Marc.2377 Can you explain what you mean by "change"? – Brian Bi Aug 12 '16 at 23:29
  • Sure. You're saying "As of C++14, (...) are provided not only by " - this suggests that before C++14, these function templates were provided only by . Hence the "change". – Marc.2377 Aug 12 '16 at 23:59
  • @Marc.2377 The functions `begin` and `end` were introduced in C++11 and the header list applies to them. The other six functions mentioned were added in C++14. – Brian Bi Aug 13 '16 at 00:00
0

Basic function of iterator like 'begin' and 'end' are included in other STLs like map, vector so you do not need to include iterator explicitly, but if you want to use iterator features like 'istream_iterator' or 'back_inserter', you have to include iterator in your program.

0

All the **c++ standard template library ** contains facility for iterator

In other words ,#include <iterator> is subset of #include <STL libraries>

Ritik Kamboj
  • 806
  • 6
  • 10