stl lower_bound编译出错

C++语言 码拜 8年前 (2016-04-24) 1237次浏览
#include <iostream>
#include <algorithm>
#include <map>
#include <string>
using namespace std;
int main(){
map<string, int> m;
map<string, int>::iterator it;
m[“1”] = 11;
m[“3”] = 33;
m[“2”] = 22;
for (it = m.begin(); it != m.end(); ++it) {
cout << it->second;  //输出 112233
}
it=lower_bound(m.begin(), m.end(), “2”); //出错
return 0;
}
编译错误信息:
error C2893: 未能使函数模板“unknown-type std::less<void>::operator ()(_Ty1 &&,_Ty2 &&) const”专用化
string类不是已经重载了<了吗?能输出112233说明可以正确排序,为什么lower_bound会出错?
解决方案

20

    const auto code = m.begin() < m.end();

std::string 虽然重载了operator <,但是 m.begin() 返回的是std::string类型的?

160

lower_bound只能用于顺序容器的vector 你只能调用map本人的lower_bound 方法

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明stl lower_bound编译出错
喜欢 (0)
[1034331897@qq.com]
分享 (0)