Effective STL Terms 48

zhaozj2021-02-11  201

Terms 48: Always #include appropriate header files

One of the secondary troubles of STL programming is that although it is easy to build software that can be compiled on one platform, additional #include indication is required on other platforms. This troubles come from a fact: C standard (unlike C standard) Failure to specify which standard header file must or may be used by other standard header files #include. Due to this flexibility, different implementations will choose to do different things.

What does this mean in practice? I can give you some concepts. I used five STL platforms (let them call them A, B, C, D and E), spend some time to test some small procedures to see if I can ignore which standard header file is still successful Compilation. This indirectly tells me which header file #include has other. This is what I found:

For a and c, #includes . For C, #includes . For C and D, #includes . For D, #includes and For D and E, #includes . For all five implementations, #includes .

In addition to #include , I can't make the program that lack the header file through implementing B. According to the Murphy's Law, you will always develop on platforms like A, C, D or E, then transplant to platforms like B, especially when transplantation is very pressure and is tight. in the case of. (Murphy's Law is if there is..... One will lead to failure, then you will choose it). After many years, this "law" gradually entered the scope of idioms, and its connotation was given infinite ideas, and there were many variants, the most famous one was also It is called FINAGLE's LAW, the specific content is: if anything can go wrong, it will. (Will eventually be wrong).) This law is considered to be the best for "Murphy Law" Imitation and elaboration.)

But please don't blame the compiler or library that you want to transplant. If you lack the needed header file, this is your fault. Whenever you quote the elements in the STD name space, you should be responsible for the right header of the #include. If you miss them, your code may also be compiled, but you still lack the necessary header files, while other STL platforms may just resist your code.

To help you remember what you need, what is there in each standard STL related header file, here there is a quick summary:

Almost all containers are in the header files of the same name, for example, Vector in , List is declared in . The exception is and . Declare SET and MULTISET, declares Map and MultiMap. In addition to four algorithms, all algorithms are declared in . The exception is Accumulate (see Terms 37), Inner_Product, Adjacent_Difference, and Partial_Sum. These algorithms are declared in . Special iterators, including iStream_iterators, and ISTREAMBUF_ITERATORS (see Terms 29), declare in . Standard imitation functions (such as Less ) and imitation function adapters (such as NOT1, BIND2ND) declared in . Whenever you use any of the components in a header file, you must make sure to provide the corresponding #include indicator, even if your development platform allows you to compile it. When you find that you can reduce the pressure when you put it into a different platform, your diligence will thus reward.

转载请注明原文地址:https://www.9cbs.com/read-4782.html

New Post(0)