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. Where can I download the .cpp files to read the actual implementation?
For example, in the stl_multimap.h or in stl_map.h, it has:
template <class _Key, class _Tp, class _Compare, class _Alloc>
inline void swap(multimap<_Key,_Tp,_Compare,_Alloc>& __x,
multimap<_Key,_Tp,_Compare,_Alloc&开发者_运维技巧gt;& __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 .h files contains the implementations. 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.
Even the page itself states that it is a header-only library, which means that the implementations are included in the headers.
The implementation of the C++ library varies on different compiler/system. If you are using GCC/G++ as your compiler, here you can download the source code from 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++
STL is a template library. 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
The implementations are all in header files. You have to do that with templates. :-/
You can find implementation for swap
in std_algobase.h
.
The SGI STL file names all start with "stl_".
For example, the SGI version vector's implemetation is in file stl_vector.h.
Below is code for swap and iter_swap inside file stl_algobase.h
// 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. This leads to the another question already asked on Stackoverflow: Where are the headers of the C++ standard library.
You can use the bash tool "locate". eg. "locate stl_multimap.h" for me yields:
/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.
If for some horrible reason you use Windows, I'm sure that you'll be able to find an equivalent command with Powershell.
精彩评论