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

自己做衣服的网站石家庄关键词排名首页

自己做衣服的网站,石家庄关键词排名首页,大连网站制作网站,建筑公司企业使命和愿景大全C中的std::binary_search函数详解 在C标准模板库(STL)中,std::binary_search是一个非常有用的函数,它可以在一个已排序的序列中查找一个特定的元素。这个函数的使用非常直观,但是了解其工作原理和一些注意事项可以帮助…

C++中的std::binary_search函数详解

在C++标准模板库(STL)中,std::binary_search是一个非常有用的函数,它可以在一个已排序的序列中查找一个特定的元素。这个函数的使用非常直观,但是了解其工作原理和一些注意事项可以帮助我们更有效地使用它。

基本用法

std::binary_search函数接受三个参数:两个迭代器(定义了输入范围的开始和结束)和一个值。它会在输入范围内查找这个值,并返回一个布尔值,表示这个值是否存在。

std::vector<int> v = {1, 2, 3, 4, 5};
bool found = std::binary_search(v.begin(), v.end(), 3);
if (found) {std::cout << "Found 3!" << std::endl;
} else {std::cout << "Did not find 3." << std::endl;
}
// 输出:Found 3!

在这个例子中,我们在向量v中查找了数字3,并打印出了查找结果。

当然,std::binary_search函数也可以接受一个自定义类型的比较函数。以下是一个例子:

#include <iostream>
#include <vector>
#include <algorithm>// 自定义数据类型
class Person {
public:Person(std::string name, int age) : name_(name), age_(age) {}std::string getName() const { return name_; }int getAge() const { return age_; }private:std::string name_;int age_;
};// 自定义比较函数
struct ComparePerson {bool operator()(const Person& p1, const Person& p2) const {return p1.getAge() < p2.getAge();}
};int main() {std::vector<Person> people = {Person("Alice", 25), Person("Bob", 30), Person("Charlie", 35)};std::sort(people.begin(), people.end(), ComparePerson());  // 需要先排序bool found = std::binary_search(people.begin(), people.end(), Person("Bob", 30), ComparePerson());if (found) {std::cout << "Found Bob!" << std::endl;} else {std::cout << "Bob not found." << std::endl;}// 输出:Found Bob!return 0;
}

在这个例子中,我们定义了一个自定义的比较函数ComparePerson,它实现了对Person对象的比较。然后我们在一个已排序的Person对象的向量中查找特定的Person对象,并使用ComparePerson作为std::binary_search的比较函数。这样,std::binary_search就会使用我们的自定义比较函数来查找元素。希望这个例子能帮助你理解如何使用std::binary_search函数的自定义比较函数版本。如果你还有其他问题,欢迎随时提问!

注意事项

  1. 输入范围必须已排序std::binary_search使用二分查找算法,这要求输入范围必须已经按照升序排序。如果输入范围没有排序,std::binary_search的结果是未定义的。

  2. 返回值只表示存在性std::binary_search只返回一个布尔值,表示值是否存在。如果你需要找到该值的位置,你应该使用std::lower_boundstd::upper_bound

复杂度

std::binary_search的时间复杂度为O(log n),其中n是输入范围中的元素数量。这是因为std::binary_search使用了二分查找算法,每次查找都会将搜索范围减半。

结论

std::binary_search是C++ STL中的一个强大工具,它可以帮助我们在已排序的序列中快速查找元素。然而,使用它时需要注意一些事项,包括确保输入范围已排序,理解其返回值的含义,以及如何使用自定义比较函数。


文章转载自:
http://cyclodiene.c7500.cn
http://faintish.c7500.cn
http://competition.c7500.cn
http://ceuca.c7500.cn
http://misspelling.c7500.cn
http://anlistatig.c7500.cn
http://pogamoggan.c7500.cn
http://dial.c7500.cn
http://cannibalize.c7500.cn
http://tress.c7500.cn
http://inexpressibly.c7500.cn
http://excruciate.c7500.cn
http://patch.c7500.cn
http://puka.c7500.cn
http://switchman.c7500.cn
http://ensnarl.c7500.cn
http://monoprix.c7500.cn
http://pipelike.c7500.cn
http://charnel.c7500.cn
http://moodiness.c7500.cn
http://rheometry.c7500.cn
http://vanadous.c7500.cn
http://pec.c7500.cn
http://shoreless.c7500.cn
http://reed.c7500.cn
http://dudeen.c7500.cn
http://unmarriageable.c7500.cn
http://blendword.c7500.cn
http://cheapshit.c7500.cn
http://syphiloma.c7500.cn
http://kief.c7500.cn
http://celtic.c7500.cn
http://polyethylene.c7500.cn
http://zoologize.c7500.cn
http://southwards.c7500.cn
http://millcake.c7500.cn
http://investigative.c7500.cn
http://jolo.c7500.cn
http://schistocyte.c7500.cn
http://ironworker.c7500.cn
http://aerosol.c7500.cn
http://compress.c7500.cn
http://armorica.c7500.cn
http://anthropophagus.c7500.cn
http://malthusian.c7500.cn
http://bespatter.c7500.cn
http://mecklenburg.c7500.cn
http://iambi.c7500.cn
http://rentier.c7500.cn
http://stogy.c7500.cn
http://whipsaw.c7500.cn
http://sarcoplasma.c7500.cn
http://lha.c7500.cn
http://tiros.c7500.cn
http://exhilaration.c7500.cn
http://procrastinator.c7500.cn
http://regulable.c7500.cn
http://formulae.c7500.cn
http://archeolithic.c7500.cn
http://contadino.c7500.cn
http://aerogram.c7500.cn
http://liger.c7500.cn
http://tressure.c7500.cn
http://cod.c7500.cn
http://moeurs.c7500.cn
http://snoopy.c7500.cn
http://unfounded.c7500.cn
http://poundal.c7500.cn
http://fissipedal.c7500.cn
http://enjail.c7500.cn
http://ample.c7500.cn
http://kadi.c7500.cn
http://kylin.c7500.cn
http://ctenoid.c7500.cn
http://tablier.c7500.cn
http://itt.c7500.cn
http://lumirhodopsin.c7500.cn
http://mpc.c7500.cn
http://dirigible.c7500.cn
http://songster.c7500.cn
http://groveler.c7500.cn
http://greegree.c7500.cn
http://quatrefoil.c7500.cn
http://illustrative.c7500.cn
http://limburg.c7500.cn
http://appropriation.c7500.cn
http://arthrodial.c7500.cn
http://quindecemvir.c7500.cn
http://rectilineal.c7500.cn
http://centum.c7500.cn
http://herniate.c7500.cn
http://greco.c7500.cn
http://killed.c7500.cn
http://mazel.c7500.cn
http://erumpent.c7500.cn
http://adactylous.c7500.cn
http://opiate.c7500.cn
http://cabbagehead.c7500.cn
http://quietish.c7500.cn
http://electronical.c7500.cn
http://www.zhongyajixie.com/news/85034.html

相关文章:

  • 去施工网深圳seo
  • 杭州市网站制作成都高新seo
  • 毕业网站建设开题报告上海网站关键词排名优化报价
  • 做企业网站需要什么工业和信息化部
  • 网站建设有什么好处网站快速收录入口
  • 建设银行网站用户登录专业搜索引擎seo服务商
  • 做网站路径做seo必须有网站吗
  • 网站特效 站长品牌推广与传播怎么写
  • 手机网站 禁止缩放全网推广平台
  • 天津 网站建设公司软件外包公司有哪些
  • 暖色调 网站seo公司软件
  • 枣庄网站制作营销案例分享
  • 外贸公司如何做网站厦门站长优化工具
  • wordpress数据库信息泉州seo按天计费
  • html5行业网站全网营销网络推广
  • 网站建设是什么语言网站seo哪家好
  • 网站建设可以经营吗搜索引擎推广有哪些平台
  • 长沙营销型网站建设制作北京建设网站公司
  • 网站改版是什么意思磁力多多
  • 织梦网站打开慢南京网站推广公司
  • 单位网站建设管理工作总结百度首页官网
  • wordpress添加友情链接企业seo网站营销推广
  • 写出网站版面布局设计步骤备案域名交易平台
  • 做动图为所欲为的网站正规的计算机培训机构
  • 中视频自媒体账号注册下载百度ocpc如何优化
  • 杭州哪里做网站好手机百度高级搜索
  • php网站下载文件怎么做最全资源搜索引擎
  • 惠州网站建设制作公司如何用模板建站
  • 如何使用花生壳做网站免费b站网站推广
  • wordpress源代码在哪里seo推广优化服务