常用查找算法
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
void test01()
{
vector <int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
vector <int>::iterator it = find(v.begin(), v.end(), 5);
if (it != v.end())
{
cout << "找到" << endl;
}
else
{
cout << "没找到" << 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 p ("张三", 12);
Person p2("李四", 54);
Person p3("王二", 34);
Person p4("赵六", 45);
v.push_back(p);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
vector <Person>::iterator it = find(v.begin(), v.end(), p2);
if (it != v.end())
{
cout << "找到" << endl;
cout << "姓名" << it->m_name << " 年龄" << it->m_age << endl;
}
else
{
cout << "没找到" << endl;
}
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//找重复元素
void test01()
{
vector<int> v;
v.push_back(10);
v.push_back(30);
v.push_back(34);
v.push_back(54);
v.push_back(54);
v.push_back(45);
vector<int>::iterator pos = adjacent_find(v.begin(), v.end());
if (pos != v.end())
{
cout << "找到重复元素" << endl;
cout << *pos << endl;
}
else
{
cout << "没找到重复元素" << endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//二分找元素
void test01()
{
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
//v.push_back(2); //如果是无序序列,结果未知
//查找容器中是否有9元素
//注意:容器必须是有序的序列
int ret = binary_search(v.begin(), v.end(), 9);
if (ret)
{
cout << "找到" << endl;
}
else
{
cout << "没找到" << endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}
//总结:二分查找效率很高,值得注意的是查找容器元素必须是有序序列
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
void test01()
{
vector<int> v;
v.push_back(30);
v.push_back(20);
v.push_back(50);
v.push_back(20);
v.push_back(20);
v.push_back(20);
int num = count(v.begin(), v.end(), 20);
cout << "20的个数是:" << 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("关羽", 45);
Person p2("张飞", 45);
Person p3("刘备", 45);
Person p4("曹操", 67);
Person p5("赵云", 87);
Person p("诸葛亮", 45);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
int num = count(v.begin(), v.end(), p);
cout << "和诸葛亮同龄个数:" << num << endl;
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
//总结:统计自定义数据类型时,需要配合重载operator==
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
class Greate20 {
public:
bool operator()(int val)
{
return val >20;
}
};
void test01()
{
vector<int> v;
v.push_back(30);
v.push_back(20);
v.push_back(50);
v.push_back(20);
v.push_back(20);
v.push_back(20);
int num = count_if(v.begin(), v.end(), Greate20());
cout << "大于20的个数" << 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 AGE245 {
public:
bool operator()(const Person &p)
{
return p.m_age > 45;
}
};
void test02()
{
vector<Person> v;
Person p1("关羽", 45);
Person p2("张飞", 45);
Person p3("刘备", 45);
Person p4("曹操", 67);
Person p5("赵云", 87);
Person p("诸葛亮", 45);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
int num = count_if(v.begin(), v.end(), AGE245());
cout << "大于45岁的人数" << num << endl;
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
本文作者: 永生
本文链接: https://www.yys.zone/detail/?id=98
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!
评论列表 (0 条评论)