where we can see the source code of c++ basic functions like sort, set, multiset?
(already googled, couldn’t find)
1 Like
If you’re using mingw, this might be useful. You’ll find the directory structure in the links. All you have to do is fish out the function definitions in the appropriate header files.
3 Likes
ssjgz
October 31, 2019, 5:35am
4
Here is the git repo for libc++ - a modern-ish implementation of the C++ Standard Library.
Many of the things are very heavily optimised and correspondingly hard to read - for example, here is the guts of std::sort
:
::new(__j2) value_type(_VSTD::move(*__first1));
__d.__incr((value_type*)0);
}
}
__h.release();
}
}
template <class _Compare, class _RandomAccessIterator>
void
__sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
{
// _Compare is known to be a reference type
typedef typename iterator_traits<_RandomAccessIterator>::difference_type difference_type;
typedef typename iterator_traits<_RandomAccessIterator>::value_type value_type;
const difference_type __limit = is_trivially_copy_constructible<value_type>::value &&
is_trivially_copy_assignable<value_type>::value ? 30 : 6;
while (true)
{
__restart:
difference_type __len = __last - __first;
This seems to the be the corresponding libstdc++ (gcc
’s implementation of the C++ Standard Library) version:
* @ingroup binary_search_algorithms
* @param __first An iterator.
* @param __last Another iterator.
* @param __val The search term.
* @param __comp A functor to use for comparisons.
* @return An iterator pointing to the first element greater than @p __val,
* or end() if no elements are greater than @p __val.
* @ingroup binary_search_algorithms
*
* The comparison function should have the same effects on ordering as
* the function used for the initial sort.
*/
template<typename _ForwardIterator, typename _Tp, typename _Compare>
_GLIBCXX20_CONSTEXPR
inline _ForwardIterator
upper_bound(_ForwardIterator __first, _ForwardIterator __last,
const _Tp& __val, _Compare __comp)
{
// concept requirements
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
__glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
2 Likes
Install VSCode and you can view the source code of any function by using ctrl + click on the function name.
1 Like