在参数列表中没有"const"的情况下无法编译 lambda
Can't compile lambda without "const" in the parameter list
#include <stack>
#include <list>
#include <string>
#include <iostream>
#include <algorithm>
#include <vector>
#include <numeric>
using namespace std;
void biggies(vector<string>& str,vector<string>::size_type sz)
{
sort(str.begin(),str.end());
auto end_unique=unique(str.begin(), str.end());
str.erase(end_unique,str.end());
//When I remove the "const" in the parameter list, the code can't compile
stable_sort(str.begin(), str.end(), [](const string&a,const string&b){return a.size()<b.size();});
auto wc=find_if(str.begin(), str.end(), [sz](string& a){return a.size()>=sz;});
for_each(wc, str.end(), [](string& s){cout<<s<<endl;});
}
int main()
{
vector<string>vec{"11","22","1","1111","2222","2","111","222"};
biggies(vec, 2);
}
我在Xcode 6.4和Visual Studio 2015中测试了代码,结果证明,如果没有参数列表中的"const",两者都无法编译。我想知道为什么缺少"const"会破坏编译?我将非常感谢你的回答。
我在标准(N3337)中找不到任何对传递给排序相关算法的比较器的参数类型提出任何特定要求的内容。我所能找到的关于你为什么会有这个问题的提示是:
25.4.2:假定comp不会通过解引用函数应用任何非常数函数迭代器。
这有点间接,但因为它是"假设"你的比较器不会应用任何非const函数给你的算法,我猜这是有效的,算法传递const对象给它;
相关文章:
- 在我的 CLR 项目中添加包含会导致无法在启用 /clr 的情况下编译 WLR
- 如何在没有Cygwin或Visual Studio的情况下编译Magick++?
- 如何在不制作Visual Studio 2017项目的情况下编译C++文件?
- 为什么 GCC 6.3 在没有显式 C++11 支持的情况下编译此大括号初始化列表代码
- 在没有 -g 选项的情况下编译,但我想获得更详细的调试信息
- 在没有 AVX 的情况下编译提升
- 如何在Ubuntu中不加编译器硬化的情况下编译宝石
- 即使我传递了无效的类型参数,我为什么还能在没有错误的情况下编译此代码
- 当我尝试在启用推理引擎的情况下编译 OpenCv 时,出现错误
- 为什么 C 可以在没有库的情况下编译 time()?
- GLSL 无法在没有插值的情况下编译着色器(平面)
- 在不安装 Visual Studio 的情况下编译 Visual Studio 项目
- 在没有-DNDEBUG和-O3的情况下编译时,标准库实现不使用断言有什么原因吗
- 在不使用CRT的情况下编译C++
- 在没有ros的情况下编译roscpp(使用g++)
- 为什么"A<0>=0"中的模板 id 由于大于或等于运算符">="而无法在没有空格的情况下编译?
- 为什么 rand() 在不包含 cstdlib 或使用命名空间 std 的情况下编译
- 在没有Main的情况下编译C程序
- OpenCV:不支持GPU(该库是在不支持CUDA的情况下编译的)
- 为什么VS2008在没有警告的情况下编译时使用了错误的模板逻辑