简体   繁体   English

在哪里下载 C++ STL 源代码 .h 和 .cpp 文件?

[英]Where to download C++ STLsource code both .h and .cpp files?

I downloaded the STL source code from http://www.sgi.com/tech/stl/download.html , but it only has the .h for the function declaration.我从http://www.sgi.com/tech/stl/download.html下载了 STL 源代码,但它只有函数声明的 .h。 Where can I download the .cpp files to read the actual implementation?我在哪里可以下载 .cpp 文件以阅读实际实现?

For example, in the stl_multimap.h or in stl_map.h, it has:例如,在 stl_multimap.h 或 stl_map.h 中,它有:

template <class _Key, class _Tp, class _Compare, class _Alloc>
inline void swap(multimap<_Key,_Tp,_Compare,_Alloc>& __x, 
                 multimap<_Key,_Tp,_Compare,_Alloc>& __y) {
  __x.swap(__y);
}

I want to know the actual implementation of the swap as in我想知道交换的实际实现,如

__x.swap(__y);

I don't see where the actual code for swap is.我看不到交换的实际代码在哪里。 In here, it just calls itself.在这里,它只是调用自己。

The implementation of the C++ library varies on different compiler/system. C++ 库的实现在不同的编译器/系统上有所不同。 If you are using GCC/G++ as your compiler, here you can download the source code from http://gcc.gnu.org/libstdc++/ .如果您使用 GCC/G++ 作为编译器,您可以从http://gcc.gnu.org/libstdc++/下载源代码。

Or you can anonymously checkout the source code using this command:或者您可以使用以下命令匿名签出源代码

svn checkout svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3 libstdc++

The .h files contains the implementations. .h 文件包含实现。 Many of the headers on that page are just wrappers around other headers or provide typedefs, but if you look at a file like stl_set.h , you will see that it has all the definitions of functions for the set class.该页面上的许多标头只是其他标头的包装器或提供 typedef,但是如果您查看stl_set.h类的文件,您将看到它具有set类的所有函数定义。

Even the page itself states that it is a header-only library, which means that the implementations are included in the headers.甚至页面本身也声明它是一个仅标头库,这意味着实现包含在标头中。

STL is a template library. STL 是一个模板库。 I hope you will find the implementation in header files only.我希望您只能在头文件中找到实现。

The isn't anything else.这不是别的。 Note that the page says:请注意,页面上写着:

This distribution of the STL consists entirely of header files: there is no need to link to any library files STL 的这个发行版完全由头文件组成:不需要链接到任何库文件

The implementations are all in header files.实现都在头文件中。 You have to do that with templates.你必须用模板来做到这一点。 :-/ :-/

您可以在std_algobase.h找到swapstd_algobase.h

The SGI STL file names all start with "stl_". SGI STL 文件名都以“stl_”开头。

For example, the SGI version vector's implemetation is in file stl_vector.h.例如,SGI 版本向量的实现在文件 stl_vector.h 中。

Below is code for swap and iter_swap inside file stl_algobase.h下面是文件 stl_algobase.h 中的 swap 和 iter_swap 代码

// swap and iter_swap

template <class _ForwardIter1, class _ForwardIter2, class _Tp>
inline void __iter_swap(_ForwardIter1 __a, _ForwardIter2 __b, _Tp*) {
  _Tp __tmp = *__a;
  *__a = *__b;
  *__b = __tmp;
}

template <class _ForwardIter1, class _ForwardIter2>
inline void iter_swap(_ForwardIter1 __a, _ForwardIter2 __b) {
  __STL_REQUIRES(_ForwardIter1, _Mutable_ForwardIterator);
  __STL_REQUIRES(_ForwardIter2, _Mutable_ForwardIterator);
  __STL_CONVERTIBLE(typename iterator_traits<_ForwardIter1>::value_type,
                    typename iterator_traits<_ForwardIter2>::value_type);
  __STL_CONVERTIBLE(typename iterator_traits<_ForwardIter2>::value_type,
                    typename iterator_traits<_ForwardIter1>::value_type);
  __iter_swap(__a, __b, __VALUE_TYPE(__a));
}

template <class _Tp>
inline void swap(_Tp& __a, _Tp& __b) {
  __STL_REQUIRES(_Tp, _Assignable);
  _Tp __tmp = __a;
  __a = __b;
  __b = __tmp;
}

You don't need to download the files.您无需下载文件。 You're able to use the STL, so some versions of the library must be on your computer somewhere.您可以使用 STL,因此某些版本的库必须在您的计算机上的某个地方。 This leads to the another question already asked on Stackoverflow: Where are the headers of the C++ standard library .这导致了 Stackoverflow 上已经提出的另一个问题: C++ 标准库的头文件在哪里

You can use the bash tool "locate".您可以使用 bash 工具“定位”。 eg.例如。 "locate stl_multimap.h" for me yields: “定位 stl_multimap.h” 对我来说产生:

/usr/include/c++/5/bits/stl_multimap.h
/usr/include/c++/6/bits/stl_multimap.h
/usr/include/c++/7/bits/stl_multimap.h
/usr/include/c++/8/bits/stl_multimap.h
/usr/lib/gcc-snapshot/include/c++/9/bits/stl_multimap.h

Once you have a look at the directories it should become pretty obvious where everything else is too.一旦您查看了目录,其他所有内容的位置都应该变得非常明显。

In each of those locations I'll find the different compiler versions of the file.在每个位置,我都会找到文件的不同编译器版本。 For my computer, all the gcc 7.* files are in my /usr/include/c++/7 directory.对于我的计算机,所有 gcc 7.* 文件都在我的/usr/include/c++/7目录中。

If for some horrible reason you use Windows, I'm sure that you'll be able to find an equivalent command with Powershell.如果您出于某种可怕的原因使用 Windows,我相信您将能够使用 Powershell 找到等效的命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM