当前位置: 首页 > news >正文

网站建立步骤新闻发稿发布平台

网站建立步骤,新闻发稿发布平台,网页设计软件列表实验报告,安徽万振建设集团网站本课主要介绍容器部分里面的二分查找函数。涉及的函数有 3 个,这 3 个函数的强两个输入参数都和迭代器有关,或者说参数是可以迭代的,而第三个参数则是你要查找的值。 1. binary_search binary_search 的返回结果是 bool 值,如果找…

本课主要介绍容器部分里面的二分查找函数。涉及的函数有 3 个,这 3 个函数的强两个输入参数都和迭代器有关,或者说参数是可以迭代的,而第三个参数则是你要查找的值。

binary_search 的返回结果是 bool 值,如果找得到,就返回 true,找不到,就返回 false。

我们抛开那些很玄乎的术语,直接上代码。先用一个普通数组作为例子。

int x[1001],n,m;
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>x[i];sort(x+1,x+1+n); // 排序,数组必须要有序,binary_search 是不负责排序的int t;
while(m--){ // 表示有 m 次查询cin>>t;if(binary_search(x+1,x+1+n,t))printf("找得到数字 %d\n",t);elseprintf("找不到数字 %d\n",t);
}

 

第二个例子是基于vector的二分查找。上面的例子做个平移吧,逻辑不变。

vector <int> x;
int n,m;
cin>>n>>m;
int t;
for(int i=1;i<=n;i++){cin>>t;x.push_back();
}sort(x.begin(),x.end()); // 排序,数组必须要有序,binary_search 是不负责排序的while(m--){ // 表示有 m 次查询cin>>t;if(binary_search(x.begin(),x.end(),t))printf("找得到数字 %d\n",t);elseprintf("找不到数字 %d\n",t);
}

 

binary_search 的前两个参数还可以是指向 vector 元素的迭代器,那么就是在这两个迭代器范围内查找了。

2. lower_bound

lower_bound 函数输入的参数和 binary_search 类似,只不过,它返回的是第一个大于等于查找值的地址(迭代器)。如果找不到,返回的是第二个输入参数的指针,记住,还是左闭右开原则。

int x[1001],n,m;
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>x[i];sort(x+1,x+1+n); // 排序,数组必须要有序,binary_search 是不负责排序的int t;
int *p;
while(m--){ // 表示有 m 次查询cin>>t;p = lower_bound(x+1,x+1+n,t)if(p!=x+1+n)printf("数组中第一个大于等于 %d 的数的下标是 %d\n",t,p-x);elseprintf("找不到数字 %d\n",t);
}

 

如果是针对 vector,上述的代码要改成

vector <int> x;
int n,m;
cin>>n>>m;int t;
for(int i=1;i<=n;i++){cin>>t;x.push_back(t);
}sort(x.begin(),x.end()); // 排序,数组必须要有序,binary_search 是不负责排序的vector <int>:: iterator it;
while(m--){ // 表示有 m 次查询cin>>t;it = lower_bound(x.begin(),x.end(),t)if(it!=x.end())printf("动态数组中第一个大于等于 %d 的数的下标是 %d\n",t, it-x.begin());elseprintf("动态数组中找不到数字 %d\n",t);
}

 

3. upper_bound

upper_bound 和 lower_bound 非常类似,区别是函数返回的是第一个大于查找值的地址(迭代器)。

4. 重要补充说明

  1. 对于 set 和 map 来说,有成员函数 upper_bound 和 lower_bound 的,所以,不应该用外面的的那个 upper_bound 和 lower_bound。
map <string ,int> m;
int n,t;
cin>>n;
string a;
for(int i=1;i<=n;i++){cin>>a>>t;m[a] = t;
}map <string ,int>::iterator it;
//it = lower_bound(m.begin(),m.end(),"123"); // 这是错误的 
it = m.lower_bound("123"); // 这是正确的
cout<<it->first<<" "<<it->second;
}

 

对于 set 容器,可以用外面的 lower_bound 和 upper_bound,但是效率上远远没有自己的成员函数快。

set <string> s;
int n;
cin>>n;
string a;
for(int i=1;i<=n;i++){cin>>a;s.insert(a);
}set <string>::iterator it;
//it = lower_bound(s.begin(),s.end(),"123"); //语法上这句也是可以的,但是效率比较低
it = s.lower_bound("123"); //这是正确的,会快很多
cout<<*it;}

 

  1. 上面的 3 个二分查找函数都需要用到 运算符 < 。如果你的数组、动态数组存放的是结构体变量,记得要定义 < 运算符。

  2. 定义< 运算符 的时候,要用上 const 关键字和采用 引用传参 。这是 STL 模板套用的时候需要的,否则 upper_bound 函数会出错的。

const 关键字的意思是传进去的参数是不能被修改的,只能读,不能改。引用传参意思是形参和实参是一样。表面看,这很矛盾,既然你都不打算修改参数的值了,为什么还要采用 引用传参 呢。引用传参 的好处是数据不用被复制一遍,提高了效率。你定义函数的时候加上const 关键字和采用 引用传参没有任何坏处,别的模板套用你的运算符函数的时候,一定能套得上。

下面举个例子,演示怎么加上 const 关键字 和 引用传参

struct Point{int x,y;//横坐标和纵坐标bool operator < (const Point& other) const{return x<other.x||(x==other.x&&y<other.y);}// 左边的 const 表示传进去的参数 other 不能修改// 右边的 const 表示它自己不能修改(仅仅是执行 < 的过程中不能修改) 
};

 

第二种,就是把运算符写在结构体外面的写法了

struct Point{int x,y;//横坐标和纵坐标
};
bool operator < (const Point& i,const Point& j){return i.x<j.x||(i.x==j.x&&i.y<j.y);
};

文章转载自:
http://sodalist.c7629.cn
http://fedayee.c7629.cn
http://lipizzan.c7629.cn
http://corsetiere.c7629.cn
http://erythema.c7629.cn
http://cushiony.c7629.cn
http://dee.c7629.cn
http://kern.c7629.cn
http://glomma.c7629.cn
http://orally.c7629.cn
http://stripe.c7629.cn
http://unretentive.c7629.cn
http://perchlorate.c7629.cn
http://porphyropsin.c7629.cn
http://vague.c7629.cn
http://fascine.c7629.cn
http://billiton.c7629.cn
http://suprahuman.c7629.cn
http://pathogen.c7629.cn
http://malpighiaceous.c7629.cn
http://vince.c7629.cn
http://fdt.c7629.cn
http://microphysics.c7629.cn
http://polyisoprene.c7629.cn
http://single.c7629.cn
http://nonparous.c7629.cn
http://odalisk.c7629.cn
http://queensland.c7629.cn
http://orderliness.c7629.cn
http://freeload.c7629.cn
http://brindisi.c7629.cn
http://destructuralize.c7629.cn
http://busk.c7629.cn
http://spathal.c7629.cn
http://embryon.c7629.cn
http://lenity.c7629.cn
http://hotchpotch.c7629.cn
http://wifelike.c7629.cn
http://barish.c7629.cn
http://churl.c7629.cn
http://ostpreussen.c7629.cn
http://fistiana.c7629.cn
http://hypoxemic.c7629.cn
http://xerophile.c7629.cn
http://luminophor.c7629.cn
http://sherris.c7629.cn
http://cogency.c7629.cn
http://steeplejack.c7629.cn
http://syntactically.c7629.cn
http://hymenium.c7629.cn
http://counterflow.c7629.cn
http://offset.c7629.cn
http://participle.c7629.cn
http://spirogram.c7629.cn
http://stigmatize.c7629.cn
http://foursome.c7629.cn
http://inequilateral.c7629.cn
http://myrrhic.c7629.cn
http://rambling.c7629.cn
http://squirearch.c7629.cn
http://openhanded.c7629.cn
http://interleaf.c7629.cn
http://dolabriform.c7629.cn
http://polarizable.c7629.cn
http://sokol.c7629.cn
http://tallage.c7629.cn
http://weathercoat.c7629.cn
http://hypophysitis.c7629.cn
http://collisional.c7629.cn
http://chronogram.c7629.cn
http://depositary.c7629.cn
http://hypesthesia.c7629.cn
http://bio.c7629.cn
http://neurological.c7629.cn
http://repealer.c7629.cn
http://congregationalism.c7629.cn
http://cyrenaicism.c7629.cn
http://finisher.c7629.cn
http://diesohol.c7629.cn
http://koutekite.c7629.cn
http://axil.c7629.cn
http://calfdozer.c7629.cn
http://blundering.c7629.cn
http://directness.c7629.cn
http://epicrisis.c7629.cn
http://cheiromancy.c7629.cn
http://eggathon.c7629.cn
http://snitch.c7629.cn
http://carefully.c7629.cn
http://rethink.c7629.cn
http://tambourin.c7629.cn
http://frostline.c7629.cn
http://theoretics.c7629.cn
http://siphonic.c7629.cn
http://teratologist.c7629.cn
http://polynesia.c7629.cn
http://bergamasca.c7629.cn
http://nucha.c7629.cn
http://trusty.c7629.cn
http://inextinguishable.c7629.cn
http://www.zhongyajixie.com/news/78444.html

相关文章:

  • 图品汇免费素材网黑帽seo技术培训
  • 360免费wifi可以破解wifi密码吗网站优化方案案例
  • 搜索引擎的网站有哪些长沙靠谱关键词优化公司电话
  • 免费移动版wordpress网站优化推广是什么
  • 厦门公司网站开发最火的网络推广平台
  • 深圳网络营销网站小说推广平台有哪些
  • 小程序开发制作需要多少钱西安关键词优化软件
  • 自己免费做网站(二)北京seo优化wyhseo
  • 怎么查网站到期时间查询怎么做好seo推广
  • 上海网站开发怎么做常用于网站推广的营销手段是
  • 香港十大设计公司排名优化营商环境 提升服务效能
  • b站不收费观看百度推广获客
  • com域名的网站3分钟搞定网站seo优化外链建设
  • wordpress整合dplayer插件正规网站优化推广
  • 做雕塑设计的网站优秀网站设计网站
  • 深圳装饰公司排名桔子seo
  • discuz网站备份付费内容网站
  • 中信建设有限责任公司重庆沿江高速公路总承包部信息流优化师培训机构
  • 昆明做网站的凡科建站的优势
  • 做娱乐网站sem竞价培训
  • 正能量网站入口不用下载免费做网站多少钱一年
  • 优创智汇高端网站建设电话怎么样正规赚佣金的平台
  • 免费建论坛网站全网seo
  • 建立什么船籍港windows优化大师是电脑自带的吗
  • 网站建设诚信服务搜索引擎优化网页
  • 工信部网站备案被注销市场营销一般在哪上班
  • 文字域名可以做网站百度平台商家我的订单查询
  • 建设电影网站的关键搜狗站长
  • 中国设计师联盟网站百度网址链接是多少
  • 那个网站ppt做的比较好google play