Removing '#include <algorithm>' does not violate code

This may be a very stupid question, but the book I'm reading instructed me to write a piece of code that uses algorithms to scramble and arrange elements in a vector. For this, the book tells me to use the library of algorithms from the main C ++ library. Ok, so far I understand this, but after writing the code, I wanted to see what would break if I removed this library from the top of my code, and it surprised me that everything was still working.

This is the code I'm talking about. When I remove the "#include algorithm" from the top of the code, nothing will break. How can it be? Shouldn't the random_shuffle part be interrupted if you are not using this library?

#include <iostream> #include <vector> #include <algorithm> #include <ctime> #include <cstdlib> using namespace std; int main() { vector<int>::const_iterator iter; cout << "Creating a list of scores."; vector<int> scores; scores.push_back(1500); scores.push_back(3500); scores.push_back(7500); cout << "\nHigh Scores:\n"; for (iter = scores.begin(); iter != scores.end(); ++iter) { cout << *iter << endl; } cout << "\nFinding a score."; int score; cout << "\nEnter a score to find: "; cin >> score; iter = find(scores.begin(), scores.end(), score); if (iter != scores.end()) { cout << "Score found.\n"; } else { cout << "Score not found.\n"; } cout << "\nRandomizing scores."; srand(static_cast<unsigned int>(time(0))); random_shuffle(scores.begin(), scores.end()); cout << "\nHigh Scores:\n"; for (iter = scores.begin(); iter != scores.end(); ++iter) { cout << *iter << endl; } cout << "\nSorting scores."; sort(scores.begin(), scores.end()); cout << "\nHigh Scores:\n"; for (iter = scores.begin(); iter != scores.end(); ++iter) { cout << *iter << endl; } system("pause"); return 0; } 
+3
source share
7 answers

The reason it works is because it is included in the header, which you also included.

For example, a vector may have included algorithms in it. This is common because they often have a headline only.

However, you cannot rely on a specific implementation of the standard library to include every title. (for example, it can work with MSVC, and it can break with gcc stdlib +++).

For this reason, I highly recommend including what you use, regardless of where it will be compiled. --- Please note that this is slightly different from โ€œwhat you are referencing,โ€ because forward declarations for dots and links in headers can significantly improve build time.

+7
source

The C ++ standard does not define which headers are included in each standard header. This means that, for example, <vector> can #include <algorithm> for implementing library A, but not in implementing library B. It can also change between different editions of the same library or on its different ports. glibC ++, for example, once carefully cleaned their inclusion hierarchies.

This puts us C ++ programmers in some inconvenience, since we must necessarily include the correct ones, although some standard headers have already done this; if we are lazy, we risk that the compilation breaks on other platforms, the system rises and falls.

General rule:

Include what is needed according to the definition of the header. But not more.

โ€œBut no more,โ€ because compilation times may become too slow.

+3
source

The code works because the vector contains an algorithm inside. To check what is included and what is not, you can generate a pre-processor output by passing the -E flag to the compiler.

Write a sample file: temp.C, which has only one line:

 #include <vector> 

Now, if we compile the file as g++ -E temp.C , you can see this algorithm in the output.

+1
source

Breaks here

 janus@Zeus :~$ g++ test.cpp test.cpp: In function 'int main()': test.cpp:27:52: error: no matching function for call to 'find(std::vector<int>::iterator, std::vector<int>::iterator, int&)' test.cpp:27:52: note: candidate is: /usr/include/c++/4.6/bits/streambuf_iterator.h:359:5: note: template<class _CharT2> typename __gnu_cxx::__enable_if<std::__is_char<_CharT2>::__value, std::istreambuf_iterator<_CharT2, std::char_traits<_CharT> > >::__type std::find(std::istreambuf_iterator<_CharT2, std::char_traits<_CharT> >, std::istreambuf_iterator<_CharT2, std::char_traits<_CharT> >, const _CharT2&) test.cpp:39:48: error: 'random_shuffle' was not declared in this scope test.cpp:47:38: error: 'sort' was not declared in this scope janus@Zeus :~$ 

gcc 4.6.1

0
source

The code does not compile for me when I deleted the #include algorithm string. Do you run the risk of patronizing strongly, are you sure you recompiled?

My compiler output:

 a.cpp(28) : error C3861: 'find': identifier not found a.cpp(40) : error C3861: 'random_shuffle': identifier not found a.cpp(48) : error C3861: 'sort': identifier not found 
0
source

Unfortunately, standard headers are allowed to include other standard headers, so there is no guarantee that your code will break if you forget it.

0
source

The #include preprocessor directive does not โ€œincludeโ€ the library in the linking process, it simply instructs the precompiler to read the header file and include it in the source file, which it compiles as a whole.

However, the algorithm header file may be included in another include file.

Note. A specific function or method can be declared and defined in the header file, so binding is not required.

edit: Your code is well written and should work properly. I would advise you to use the predefined endl newline endl and include it at the end of the output sequence, and not at the beginning of the following:

 cout << "Creating a list of scores." << endl; cout << "High Scores:" << endl; 
0
source

Source: https://habr.com/ru/post/1246956/


All Articles