2

In cppreference.com, it states that the begin() and end() functions are defined in the header file < iterator >. But I still can use the begin() and end() functions without including the < iterator > header. I wonder why? Is it because I use;

using namespace std;

So it is included?

5
  • No, using namespace xxx doesn't include any header files. It only means that you can write begin() instead of std::begin(). The `<iterator> header must be included through some other header that you did include into your .cpp. Commented Feb 25, 2016 at 7:07
  • 3
    Unrelate to answer: Avoid writing using namespace std; in real code. Commented Feb 25, 2016 at 7:10
  • @MohitJain Why is that? Commented Feb 25, 2016 at 7:11
  • @SH.C Check this question Commented Feb 25, 2016 at 7:12
  • @MohitJain Oh, ok, thanks! Commented Feb 25, 2016 at 7:13

2 Answers 2

9

Read from notes on the same page:

In addition to being included in <iterator>, std::begin is guaranteed to become available if any of the following headers are included: <array>, <deque>, <forward_list>, <list>, <map>, <regex>, <set>, <string>, <unordered_map>, <unordered_set>, and <vector>.

Apart from these list of headers, std::begin may get included from some other header also.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh, ok, I didn't see that. That make so much more sense.
1

No, using namespace xxx doesn't include any header files. It only means that you can write begin() instead of std::begin(). The <iterator> header must be included through some other header that you did include into your .cpp.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.