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

网站建设视频vs桂林seo顾问

网站建设视频vs,桂林seo顾问,企业网站建设专业公司,设计的网站有哪些文章目录常用查找算法findfind_ifadjacent_findbinary_searchcountcount_if常用查找算法 算法简介: find//查找元素 find_if//按条件查找元素 adjacent_find//查找相邻重复元素 binary_search//二分查找法 count//统计元素个数 count_if//按条件统计元素个数find …

文章目录

  • 常用查找算法
    • find
    • find_if
    • adjacent_find
    • binary_search
    • count
    • count_if


常用查找算法

算法简介:

find//查找元素
find_if//按条件查找元素
adjacent_find//查找相邻重复元素
binary_search//二分查找法
count//统计元素个数
count_if//按条件统计元素个数

find

功能:查找指定元素,找到返回指定元素的迭代器,找不到返回结束迭代器end()。

函数原型:

find(iterator beg,iterator end,value);
//beg开始迭代器
//end结束迭代器
//value查找的元素
#include<iostream>
using namespace std;
#include<vector>//查找内置数据类型
void test01()
{vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}//查找容器中是否有5这个元素vector<int>::iterator it = find(v.begin(), v.end(), 5);if (it == v.end()){cout << "没有找到!" << endl;}else{cout << "找到:" << *it << endl;}
}//查找自定义数据类型
class person
{
public:person(string name, int age){this->m_name = name;this->m_age = age;}//重载== 底层find知道如何对比person数据类型bool operator==(const person& p){if (this->m_name == p.m_name && this->m_age == p.m_age){return true;}else{return false;}}string m_name;int m_age;
};void test02()
{vector<person> v;//创建数据person p1("aaa", 18);person p2("bbb", 19);person p3("ccc", 20);person p4("ddd", 21);//放入到容器中v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);//查找p2这个人是否存在vector<person>::iterator it = find(v.begin(), v.end(), p2);if (it == v.end()){cout << "没有找到" << endl;}else{cout << "找到元素 姓名:" << it->m_name << " 年龄:" << it->m_age << endl;}
}int main()
{test01();test02();system("pause");return 0;
}

find_if

功能:按条件查找元素,找到返回指定元素的迭代器,找不到返回结束迭代器位置end()。

函数原型:

find_if(iterator beg,iterator end,_Pred);
//beg开始迭代器
//end结束迭代器
//_Pred函数或者谓词(返回bool类型的仿函数)
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>//查找内置数据类型
class greater5
{
public:bool operator()(int val){return val > 5;}
};void test01()
{vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}//查找容器中是否有大于5的vector<int>::iterator it = find_if(v.begin(), v.end(), greater5());if (it == v.end()){cout << "没有找到!" << endl;}else{cout << "找到大于5的数为:" << *it << endl;//6}
}//查找自定义数据类型
class person
{
public:person(string name, int age){this->m_name = name;this->m_age = age;}string m_name;int m_age;
};class greater20
{
public:bool operator()(person& p){return p.m_age > 20;}
};void test02()
{vector<person> v;//创建数据person p1("aaa", 18);person p2("bbb", 19);person p3("ccc", 20);person p4("ddd", 21);//放入到容器中v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);//查找容器中是否有年龄大于20的vector<person>::iterator it = find_if(v.begin(), v.end(), greater20());if (it == v.end()){cout << "没有找到" << endl;}else{cout << "找到元素 姓名:" << it->m_name << " 年龄:" << it->m_age << endl;}
}int main()
{test01();test02();system("pause");return 0;
}

adjacent_find

功能:查找相邻重复元素,返回相邻元素的第一个元素的迭代器。

函数原型:

adjacent_find(iterator beg,iterator end);
//beg开始迭代器
//end结束迭代器
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>void test01()
{vector<int> v;v.push_back(0);v.push_back(2);v.push_back(3);v.push_back(0);v.push_back(2);v.push_back(1);v.push_back(1);v.push_back(4);vector<int>::iterator pos = adjacent_find(v.begin(), v.end());if (pos == v.end()){cout << "未找到相邻重复元素" << endl;}else{cout << "找到相邻重复元素:" << *pos << endl;}
}int main()
{test01();system("pause");return 0;
}

binary_search

功能:查找指定元素是否存在,查到返回true,否则返回false,二分查找法查找效率很高。

注意:在无序序列中不可用。

函数原型:

bool binary_search(iterator beg,iterator value);
//beg开始迭代器
//end结束迭代器
//value查找的元素
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>//binary_search查找元素必须是有序序列
void test01()
{vector<int> v;for (int i = 0; i < 10; i++){v.push_back(i);}v.push_back(5);//尾部插入5这个元素,变为无序序列,binary_search查找不到元素//查找容器中是否有9这个元素bool ret = binary_search(v.begin(), v.end(), 9);if (ret){cout << "找到元素" << endl;}else{cout << "未找到元素" << endl;}
}int main()
{test01();system("pause");return 0;
}

count

功能:统计元素个数。

函数原型:

count(iterator beg,iterator end,value);
//beg开始迭代器
//end结束迭代器
//value统计的元素
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>//统计内置数据类型
void test01()
{vector<int>v;v.push_back(1);	v.push_back(2);v.push_back(5);v.push_back(7);v.push_back(8);v.push_back(2);v.push_back(5);v.push_back(2);//统计5的个数int num = count(v.begin(), v.end(), 5);cout << "5的个数为:" << num << endl;
}//统计自定义数据类型
class person
{
public:person(string name, int age){this->m_name = name;this->m_age = age;}bool operator==(const person& p){if (this->m_age == p.m_age){return true;}else{return false;}}string m_name;int m_age;
};void test02()
{vector<person> v;//创建数据person p1("aaa", 18);person p2("bbb", 19);person p3("ccc", 18);person p4("ddd", 21);person p("ggg", 18);//放入到容器中v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);//统计容器中与p年龄相同的人int num = count(v.begin(), v.end(), p);cout << "和ggg年龄相同的人数:" << num << endl;}int main()
{test01();test02();system("pause");return 0;
}

总结:统计自定义数据类型时,需要配合重载operator==。

count_if

功能:按提条件统计元素个数。

函数原型:

count_if(iterator beg,iterator end,_Pred);
//beg开始迭代器
//end结束迭代器
//_Pred谓词
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>//统计内置数据类型
class greater5
{
public:bool operator()(int val){return val > 5;}
};void test01()
{vector<int>v;v.push_back(1);v.push_back(2);v.push_back(5);v.push_back(7);v.push_back(8);v.push_back(2);v.push_back(5);v.push_back(2);//统计5的个数int num = count_if(v.begin(), v.end(), greater5());cout << "大于5的个数为:" << num << endl;
}//统计自定义数据类型
class person
{
public:person(string name, int age){this->m_name = name;this->m_age = age;}string m_name;int m_age;
};class greater20
{
public:bool operator()(const person& p){return p.m_age > 20;//如果年龄大于20返回真,否则返回假}
};void test02()
{vector<person> v;//创建数据person p1("aaa", 23);person p2("bbb", 19);person p3("ccc", 20);person p4("ddd", 21);person p("ggg", 18);//放入到容器中v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);//统计容器中与p年龄相同的人int num = count_if(v.begin(), v.end(), greater20());cout << "年龄大于20的人数:" << num << endl;
}int main()
{test01();test02();system("pause");return 0;
}

http://www.fp688.cn/news/159133.html

相关文章:

  • 我想给别人做网站制作网站需要的技术与软件
  • 微信公众号开发文档店铺seo是什么意思
  • 网站设计是平面设计吗四川网络推广seo
  • 怎么找做网站平台公司seo关键词优化策略
  • 深圳广东网站建设套餐淘宝网页版
  • 越秀网站建设策划长春网站制作推广
  • 微官网怎么注册seo 工具分析
  • 万户做的网站安全吗seo外包网络公司
  • 学院二级网站建设方案模板今日关注
  • 临海做 网站新冠疫苗接种最新消息
  • 东莞做网站做什么赚钱短视频平台推广方案
  • 威客做的比较好的网站seopc流量排名官网
  • 淘宝客网站用什么软件做seo手机排名软件
  • 苏州 网站建设 app百度竞价网站
  • 哪些网站做外贸在线seo超级外链工具
  • 专业做微视频的网站武汉seo公司哪家好
  • 北京手机网站建设外包江苏网页设计
  • 公司网站备案需要什么材料建设企业网站多少钱
  • 网站建设工作 方案可以推广发广告的app
  • 局域网搭建惠州seo招聘
  • 苏州做网站公司什么是seo
  • 深圳网站建设html5seo搜索引擎优化知乎
  • 可以进入的网站网站推广的平台
  • 个人网站建设好之后怎么赚钱百度手机浏览器
  • 一个公司可以做多少网站广东网站seo策划
  • 中央政府网站的建设的意见竞价推广托管公司介绍
  • 天津网站优化步骤短信营销
  • 域名和网站建站公司链接营销qq官网
  • 全国做网站哪家好合肥seo按天收费
  • 医疗wordpressseo外链要做些什么