I have a simple program in which I arrange the elements of an int
array in ascending or descending order, and I use the swap()
function to move the elements around. I compiled the program without any errors, and it ran like a charm. I only noticed afterwards that I had forgotten to #include
the library that swap()
is defined in (<algorithm>
, or <utility>
as of C++11
) before I compiled.
Why did it still work? The top of my program looked like this:
#include <iostream>
#include <cstdlib>
using namespace std;
I tried taking out <iostream>
, just to see what would happen, and it predictably put out a bunch of 'cout/cin/endl' was not declared in this scope
errors, but I was surprised to see that it gave some 'swap' was not declared in this scope
errors as well. Does that mean swap()
is defined in <iostream>
? I don't think it should be, should it?
Anyways, this is probably a big long question for a simple answer, but I'm pretty curious. I'm still learning C
and C++
, so I don't know a lot of things, and I couldn't find an answer to this particular mystery via the "Almighty" Google Machine, so here I am.
Thanks in advance!