c++lower_bound(c++lower_bound函数)
本篇文章给大家谈谈c++lower_bound,以及c++lower_bound函数对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、请问 C++ map 的 lower_bound()函数怎么用?
- 2、c++ 输出map每个元素的值
- 3、C++ STL multiset中,lower_bound的区间到底是什么意思?
- 4、c++ stl 中 lower_bound 的第四个参数的用法
- 5、C/C++利用 upper_bound和lower_bound的性质二分查找
- 6、c++中lower_bound与find使用上有什么区别?
请问 C++ map 的 lower_bound()函数怎么用?
我们知道map容器是根据键值进行排序的
lower_bound(k)返回一个迭代器,指向键不小于k的第一个元素
upper_bound(k)返回一个迭代器,指向键大于k的第一个元素
这两个函数常用于态姿multimap容器,用来获取某个键对应的所有元素
给你个程序:
#pragma warning (disable:4786)
#includeiostream
#includestring
#includemap
using namespace std;
int main()
{
multimapstring,int m;
m.insert(make_pair((string)"China",1));
m.insert(make_pair((string)"China",2));
m.insert(make_pair((string)"China",3));
m.insert(make_pair((string)"English"帆厅绝,1));
m.insert(make_pair((string)"English",2));
multimapstring,int::iterator it = m.begin();
while(it != m.end())
{
coutit-first" 伏团"it-secondendl;
it++;
}
coutendl;
multimapstring,int::iterator it1 = m.lower_bound("China"),it2 = m.upper_bound("China");
coutit1-first" "it1-secondendl;
coutit2-first" "it2-secondendl;
return 0;
}
晓得了么,lower_bound(k)得到的迭代器,就是指向第一个键k对应的第一个元素
upper_bound(k)得到的迭代器,就是指向大于键k的第一个元素
c++ 输出map每个元素的值
#includeiostream
#includestring
#includemap
usingnamespacestd;
intmain()
{
multimapstring,intm_map;
strings("中国"),s1("美国");
m_map.insert(make_pair(s,50));
m_map.insert(make_pair(s,55));
m_map.insert(make_pair(s,60));
m_map.insert(make_pair(s1,30));
m_map.insert(make_pair(s1,20));
m_map.insert(make_pair(s1,10));
//方式1
intk;
multimapstring,int::iteratorm;
m=m_map.find(s);
for(k=0;k!=m_map.count(s);k++,m++)
coutm-first"--"m-secondendl;
//方式2
multimapstring,int::iteratorbeg,end;
beg=m_map.lower_bound(s1);
end=m_map.upper_bound(s1);
for(m=beg;m!=end;m++)
coutm-first"--"m-secondendl;
//方式3
beg=m_map.equal_range(s).first;
end=m_map.equal_range(s).second;
for(m=beg;m!=end;m++)
coutm-first"--"m-secondendl;
return0;
}
扩展资料
map构造函数;
mapstring,intmapstring;mapint,stringmapint;
mapsring,charmapstring;mapchar,stringmapchar;
mapchar,intmapchar;map枝厅陵int,charmapint;
如在打枚举中打印“指定值对应的字符伏燃串”时,可是采猛戚用mapint,string的STL实现。
[img]C++ STL multiset中,lower_bound的区间到底是什么意思?
low_bound 返回的迭代器指向该键值关联的第一个实例;upper_bound返回的察陪迭代器则指向最后一个实例的下一个位置;若该键没有关联的元滚李素,则败备蠢low_bound和upper_bound返回相同的迭代器。这个区间的意思是同一键值下包含的元素的个数
c++ stl 中 lower_bound 的第四个参数的用法
使用find_if()函数查找到第一个符合条件的数据后,it就指向了该符合条件的数据,
对于vector的iterator使用++操作时,就指向了当前的下一个元素,但是,下一个元素
并不一定是满足条件的数据,所以不能使用芹简睁++操作来嫌岁指向下一个元素,而是要用循环
来获取所有满足条件的元素,如下:
...
vectorint::iterator
it
=
find_if(intVector.begin(),
intVector.end(),
fun);
if
(
it
!=
intVector.end()
)
{
//此时it指向第一个满足条件的元素了
}
while
(
it
!=
intVecotr.end()
)
{
it++;//让it指想满足条件的元素的下一个元素,即,再次搜索时从满足条件的元素的下一个位置开始
it
=
find_if(it,
intVector.end(),
fun);
if
(
it
!=
intVector.end()
)
{
//此时的it是再次找到的符合要求的元素
}
}
...
=====================================
别的办法最简单的就是排序了,你自定义一个针对你的数据类型做一个排序函数,排好序后,符合你要求的元素就排到了一起了,找到第一个符合你要求的元素后,就可以使用++来挨个访问他们,直到访问到不符合你要求的元素为咐嫌止.
C/C++利用 upper_bound和lower_bound的性质二分查找
就是二分查找而已,这种代码很多的
两个函数的用法类似,在一个左闭右开的有序区间里进行二分查找,需要查找的值由第三个参数给出。
对于upper_bound来说,返回的是被查序列中第一个大于查找值的指针,也就是返回指向被查值查找值的最小指针,lower_bound则是返回的是被查序列中第一个姿或大于等于查找值的指针,也就是返回指向被查值=查找值的最小指针。
不过除此之外,这两个函数还分别有一个重载函数,可以接受第四个参数。如果第则咐四个参数传入greaterType(),其中Type改成对应类型,那么upper_bound则返回指向被查值查找值的最小指针,lower_bound则返回指向被查值=查找值的最小指针。
最后说一点使用的注意事项,先看这么一句话“ The elements in the range shall already be sorted according to this same criterion (operator or comp), or at least partitioned with respect to val”(引用自)。简单来说,如果你用上述两个函数三个参数的那种形式,记得那个左闭右开的区间要为非递减的顺序,如果你给第四个参数传入greaterType(),则区间为非递增的顺序。
附上一段代码,供大家快速检验使用(注释掉的是错误用法):
#include iostream
#include algorithm
using namespace std;
int seq1[] = {1, 2, 3, 3, 4, 5}, seq2[] = {9, 8, 7, 7, 6, 5};
int main()
{
//coutupper_bound(seq1, seq1+6, 3, greaterint()) - seq1endl;
/迹盯伍/coutlower_bound(seq1, seq1+6, 3, greaterint()) - seq1endl;
coutupper_bound(seq1, seq1+6, 3) - seq1endl;
coutlower_bound(seq1, seq1+6, 3) - seq1endl;
coutendl;
coutupper_bound(seq2, seq2+6, 7, greaterint()) - seq2endl;
coutlower_bound(seq2, seq2+6, 7, greaterint()) - seq2endl;
//coutupper_bound(seq2, seq2+6, 7) - seq2endl;
//coutlower_bound(seq2, seq2+6, 7) - seq2endl;
return 0;
}
望采纳,谢谢
c++中lower_bound与find使用上有什么区别?
1、-是指针指向其庆握成员的运算符 .是结构体的成员运算符。最大誉察庆的区别是-前面放的是指针,而.前面跟没让的是结构体变量。2、例如:
1
2
3
4
5
6
7
8
9
struct A
{
int a;
int b;
};
A *point = malloc(sizeof(struct A));
point-a = 1;
A object;
object.a = 1;
关于c++lower_bound和c++lower_bound函数的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。