map STL问题
关键字为int型,如何查找关键字与某数最接近的元素比如(2,···)(3,···)···(6,···)想查找关键字与4最接近的是哪个,怎么查?...
关键字为int型,如何查找关键字与某数最接近的元素
比如(2,···)(3,···)···(6,···)
想查找关键字与4最接近的是哪个,怎么查? 展开
比如(2,···)(3,···)···(6,···)
想查找关键字与4最接近的是哪个,怎么查? 展开
1个回答
展开全部
是这样的,举个例子:
map<int, double> mp;
.......
mp[2] = 1.32;
mp[3] = 1.3;
......
mp[6] = 1.23;
用map<int, double>::iterator ite = mp.lower_bound(4);//找出大于等于键值4的pair值
或用map<int, double>::iterator ite = mp.upper_bound(4);//找出大于键值4的pair值
下完是我写完整的测试程序,希望对你有用
#include <map>
#include<iostream>
using namespace std;
int main()
{
map<int, double> mp;
mp[0] = 1;
mp[1] = 3;
mp[2] = 1.32;
mp[3] = 1.3;
mp[4] = 2;
mp[5] = 4;
map<int, double>::iterator ite;
ite = mp.lower_bound(4);
cout<<ite->first<<endl;
ite = mp.upper_bound(4);
cout<<ite->first<<endl;
return 0;
}
map<int, double> mp;
.......
mp[2] = 1.32;
mp[3] = 1.3;
......
mp[6] = 1.23;
用map<int, double>::iterator ite = mp.lower_bound(4);//找出大于等于键值4的pair值
或用map<int, double>::iterator ite = mp.upper_bound(4);//找出大于键值4的pair值
下完是我写完整的测试程序,希望对你有用
#include <map>
#include<iostream>
using namespace std;
int main()
{
map<int, double> mp;
mp[0] = 1;
mp[1] = 3;
mp[2] = 1.32;
mp[3] = 1.3;
mp[4] = 2;
mp[5] = 4;
map<int, double>::iterator ite;
ite = mp.lower_bound(4);
cout<<ite->first<<endl;
ite = mp.upper_bound(4);
cout<<ite->first<<endl;
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询