这适用于OS X:
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
int main() {
std::vector<double> v{9.2, 0.4, 2.5, 6.5, 4.3, 3.1, 9.7, 0.9, 1.2, 2.2, 1.09, 1.3};
if (v.size()) {
if (v.size() % 2) {
std::cout << "Middle: " << v[v.size()/2] << "\n";
} else {
std::cout << "Middle: " << v[v.size()/2 - 1] << "\n";
}
std::nth_element(v.begin(), v.begin() + v.size()/2, v.end());
if (v.size() % 2) {
std::cout << "Median " << v[v.size()/2] << '\n';
} else {
auto second = v[v.size()/2];
std::nth_element(v.begin(), v.begin() + v.size()/2 - 1, v.end());
auto first = v[v.size()/2 - 1];
std::cout << "Median " << " " << (first + second)/2 << '\n';
}
auto result = std::minmax_element(v.begin(), v.end());
std::cout << "Min: " << *result.first << "\nMax: " << *result.second << "\n";
}
return 0;
}
但在macOS崩溃:
WebPreferences *prefs = [webView preferences];
[prefs setDeveloperExtrasEnabled:YES];
WebInspector *inspector = [[WebInspector alloc] initWithWebView:webView];
这个私有API被抛弃了吗?
代码取from here。
答案 0 :(得分:2)
他们已将其重命名为:http://trac.webkit.org/changeset/189654
TLDR:
WebInspector *inspector = [WebInspector alloc];
if ([inspector respondsToSelector:@selector(initWithWebView:)])
[inspector initWithWebView:webView];
else
[inspector initWithInspectedWebView:webView];
如果将来有变化,请查看WebKit source code。