首页 > 解决方案 > STL 库在不同平台上是否有所不同?

问题描述

Windows 上的 C++ 标准模板库 (STL) 与 Linux 或任何其他平台上的不同吗?STL 的标头是否也因平台而异,或者 STL 只是一个标头库及其在 CRT 中的实现?

我们知道编译器因平台而异,C 运行时库也因平台而异。这样一来,即使标准模板库(STL)也因平台而异,这是真的吗?

请澄清这个疑问。

另外,Windows 上的 C++ STL 的名称是什么,Linux 上的名称是什么?

我一直在尝试通过在线阅读各种文章并尝试在脑海中构建一个工作流程以更好地理解这些术语来理解这一点。

标签: c++stl

解决方案


The specification of the C++ Standard Library is not contingent on any particular platform or compiler, although it does depend on the target C++ standard, and its behaviour is dependent function on various properties of a platform.

But the implementation of the C++ Standard Library is extremely dependent on the compiler and operating system. Some of the C++ Standard Library can even hardcoded into compilers.

But you use it in the same way. E.g. for std::cout and std::cin you should always write #include <iostream> as that's what the documentation says you're supposed to do. The names of header files can vary between implementations, but the ones you are supposed to use directly never differ.

This is why it's a good idea not to rely on #includes being available implicitly via other headers, or by using fancy non-standard #includes like <bits/...>. If you do so then you are not writing portable C++.


推荐阅读