72

iostreamvsなどの違いに関する情報を見てきましたiostream.h。それらから私が集めたものから、それらの違いは、.h拡張子のないバージョンは名前空間にデータを入力しないのに対し、拡張子のあるバージョンは名前空間にデータを入力することです。

cmathこれはvsでも同じmath.hですか? なぜcmath(およびそれに似た他の多くのファイル) のc代わりに、接頭辞が付いているのmathですか? それらの間にもっと違いがありますか?

4

4 に答える 4

35

I've seen some information about differences between things like iostream vs iostream.h.

[iostream.h] is not a standard header.

it is not an example of the issue you're raising.

[cmath] defines symbols in the std namespace, and may also define symbols in the global namespace. [math.h] defines symbols in the global namespace, and may also define symbols in the std namespace. if you include the former and use an unqualified symbol, it may compile with one compiler but not with another. therefore it's a good idea to use [math.h]. and in general, for such header pairs, to use the [.h] version.

c++98 provided a formal guarantee of the cxxx header not polluting the global namespace. maybe that was why they were defined. however, that was a bit harder to implement than polluting ones, so in practice no standard library implementation that i know of followed the standard in this respect, and so it was finally changed to reflect reality in c++11.

于 2012-05-22T00:28:23.787 に答える
2

名前が で始まるcヘッダーは、C 標準ライブラリのヘッダーから派生したものです。cプレフィックスが削除され、サフィックスが追加された対応するヘッダーは.h、C 標準ライブラリ ヘッダーと同一 (またはほぼ同一) です。

<cmath>std名前空間の下に関連するシンボルを定義します。<math.h>それらをグローバルに定義します。

(私はそれがそれほど単純ではないことを学びました.Alfの答えを見てください。)

于 2012-05-22T00:19:26.560 に答える
0

<cmath>また、すべての<cxxx>ヘッダーは標準 C++ です。つまり、C++ 標準で概説されているように、それらのヘッダーでサポートされているものと、それらの関数がどのように機能するかについて強力な保証があります。std名前空間で一連の関数を定義するだけです。

<math.h>すべての主要な実装でサポートされているにもかかわらず、<xxx.h>ヘッダーは標準 C++ ではありません。ただし、これらは非推奨であるため、実装に含める場合、これらのヘッダーの内容は保証されません。実際、特定の実装では、バージョンとは異なる<cxxx>動作をする関数が提供されていることが観察されています。

したがって、<cxxx>C++ を記述するときは常に を使用し、関数の名前を で修飾する必要がありstd::ますstd::malloc

于 2020-10-20T12:10:01.080 に答える