根据Scott Meyers的说法,在他的Effective STL书中 - 第46项.他声称这std::sort比std::qsort内联的事实要快670%.我测试了自己,我看到qsort更快:(!有谁可以帮我解释这个奇怪的行为?
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <cstdio>
const size_t LARGE_SIZE = 100000;
struct rnd {
int operator()() {
return rand() % LARGE_SIZE;
}
};
int comp( const void* a, const void* b ) {
return ( *( int* )a - *( int* )b );
}
int main() {
int ary[LARGE_SIZE];
int ary_copy[LARGE_SIZE];
// generate random data
std::generate( ary, ary + LARGE_SIZE, rnd() );
std::copy( ary, ary …Run Code Online (Sandbox Code Playgroud)