相关疑难解决方法(0)

C++排序的容器版本

我在c ++(http://isocpp.org/blog/2014/12/myths-3)上阅读Stroustrup的博客,当时我找到了一段有趣的代码:

void do_my_sort(vector<double>& v)
{
  sort(v,[](double x, double y) { return x>y; });  // sort v in decreasing order
}

int main()
{
  vector<double> vd;
  // ... fill vd ...
  do_my_sort(v);
  // ...
} 
Run Code Online (Sandbox Code Playgroud)

请注意,sort它不使用sort(v.begin(), v.end(), ...)Stroustrup解释的传统方法:

我使用了容器版本,sort()以避免明确迭代器.

但是,我在C++ 11编译器上尝试了相同的代码,但无法编译.我也在使用ideone的C++ 14编译器上尝试过相同的操作,但它也无法编译,说没有匹配的调用.

为什么是这样?

此外,Stroustrup接下来提到:

我可以进一步使用C++ 14比较对象:

sort(v,greater<>()); // sort v in decreasing order

我已经使用了类似的比较great<>()对于sort在C++ 11也.他为什么说这是一个C++ 14比较对象?

c++ vector c++11

4
推荐指数
1
解决办法
356
查看次数

标签 统计

c++ ×1

c++11 ×1

vector ×1