where is copy() algorithm located (what header file)?

the code compiles and runs. but i commented out the #include <iterator> purposely and the copy() function still runs. where is this copy() function located since there is no iterator or algorithm header file included. is it in the #include <vector>? if so i would need to call it from a vector object as in dice.copy(), no?

i am using codeblocks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
//#include <iterator>
#include <vector>

int main()
{
    using namespace std;
    int arr[10];
    int casts[10] = {6, 7, 2, 9 ,4 , 11, 8, 7, 10, 5};
    vector<int> dice(10);
    // copy from array to vector
    copy(casts, casts + 10, dice.begin());
    cout << "Let the dice be cast!\n";
    // create an ostream iterator
//    ostream_iterator<int, char> out_iter(cout, " ");
    // copy from vector to output
    copy(dice.begin(), dice.end(), arr);
    cout << endl;
    cout <<"Implicit use of reverse iterator.\n";
    copy(dice.rbegin(), dice.rend(), arr);
    cout << endl;
    cout <<"Explicit use of reverse iterator.\n";
    vector<int>::reverse_iterator ri;  // use if auto doesn't work
    for (ri = dice.rbegin(); ri != dice.rend(); ++ri)
        cout << *ri << ' ';
    cout << endl;
	// cin.get();
    return 0;
}
#include <algorithm>
but i do not have that included in the code.

#include <algorithim> is NOT in my code.
Last edited on
iostream probably includes it for some of its own functions.
Every standard library header is allowed to include other standard library headers, and even partial headers, and it's unspecified which. In your compiler, one of the two headers you #included pulled in <algorithm>, Another compiler won't do that. Even another version of the same compiler may stop doing that (GCC for example has been cleaning up their hidden header dependencies lately pretty aggressively: each new version breaks lots of old source code that uses functions without including their headers).

You should always include every header you use if you want your program to be portable.
ok but if i include the proper hearder file even though it is already inlcuded in another standard library in the code, then from which header file would the function be called from?

so if iostream has internally included the algorithm header and i still manually include the algorithm header from which one would be called?

edit; also, how can i actually open up the standard header files to see exactly how they are coded and work in codeblocks? i cannot actually locate alot of the header files i normally use. i searched the entire codeblocks directry tree.
Last edited on
so if iostream has internally included the algorithm header and i still manually include the algorithm header from which one would be called?


There is only one algorithm header. Standard headers (as well as any header you write should be doing) use include guards or equivalent measures that make second, third, etc #includes of the same file do nothing, so only the fist include directive actually adds code.

Usually every header faile has guard as for example

// algorithm standard header
#pragma once
#ifndef _ALGORITHM_
#define _ALGORITHM_


//...some declarations

#endif.

So the first included header will not allow to include the same header because the manifest constant _ALGORITHM_ will be defined already.
Topic archived. No new replies allowed.