c++矢量源代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4304783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:04:56  来源:igfitidea点击:

c++ vector source code

c++stlstd

提问by vrbilgi

I am trying to get the vector source code to see how the standard std or stl vector is implemented.

我正在尝试获取向量源代码以查看标准 std 或 stl 向量是如何实现的。

This is for learning purpose. Now the question is where can i find the source code. Even source code of other C++ Container also helpful.

这是为了学习目的。现在的问题是我在哪里可以找到源代码。甚至其他 C++ Container 的源代码也很有帮助。

回答by Steve Townsend

There is no 'standard' vector - the standard defines behaviour and interface (and some implementation details, such as contiguous storage) but the code is a matter for compiler writers to determine.

没有“标准”向量——标准定义了行为和接口(以及一些实现细节,例如连续存储),但代码是编译器编写者确定的问题。

Your compiler should have its own <vector>header file, have you checked for this on your build include path? Once you find that you should also see the other STL containers in their respective headers. The list for Microsoft Visual C++ is here, including some that are proprietary, so watch out for that per the below sample disclaimer:

您的编译器应该有自己的<vector>头文件,您是否在构建包含路径中检查过这个文件?一旦您发现您还应该在各自的标头中看到其他 STL 容器。Microsoft Visual C++ 的列表在这里,包括一些专有的,因此请注意以下示例免责声明:

In Visual C++ .NET 2003, members of the <hash_map>and <hash_set>header files are no longer in the std namespace, but rather have been moved into the stdext namespace. See stdext Namespace for more information.

在 Visual C++ .NET 2003 中,头文件<hash_map><hash_set>头文件的成员不再位于 std 命名空间中,而是移到了 stdext 命名空间中。有关更多信息,请参阅 stdext 命名空间。

On my installation of Visual C++ Express 2010, they are in this folder:

在我安装的 Visual C++ Express 2010 中,它们位于以下文件夹中:

c:\program files\microsoft visual Studio 10.0\vc\include

c:\程序文件\Microsoft Visual Studio 10.0\vc\include

回答by Yang Yuan

Different runtime has different implementation.

不同的运行时有不同的实现。

But I guess this is what you want, the widely used gcc implementation: https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/std/vector

但我想这就是你想要的,广泛使用的 gcc 实现:https: //github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/std/vector

It is the main header file, and the implementation is in https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/bits/stl_vector.hand https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/bits/stl_bvector.h

它是主要的头文件,实现在 https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/bits/stl_vector.hhttps:// github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/bits/stl_bvector.h

It use MACRO to make the code run in good performance and fit in variable situation, but make it hard to read, wish you good luck.

它使用 MACRO 使代码具有良好的运行性能并适合可变情况,但使其难以阅读,祝您好运。

回答by James McNellis

Most if not all of the std::vectorsource code should be contained in the <vector>header itself.

大多数(如果不是全部)std::vector源代码都应该包含在<vector>标头本身中。

The standard library containers are all class templates and as such, their definitions and the definitions of all of their member functions are contained in their respective headers.

标准库容器都是类模板,因此,它们的定义和所有成员函数的定义都包含在它们各自的头文件中。

Note that there is no One True Implementation of any of the containers; each C++ Standard Library implementation is free to implement each container as it sees fit, so long as it meets the requirements for the container.

请注意,任何容器都没有一个真正的实现;每个 C++ 标准库实现都可以自由地实现它认为合适的每个容器,只要它满足容器的要求。