文章目录
常用查找算法
find
通过值来寻找容器中的数据,然后返回迭代器
内置数据类型的查找
int main()
{
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
vector<int>::iterator pos = find(v.begin(), v.end(), 5);
if (pos != v.end())
{
cout << *pos << endl;
}
else
{
cout << "没有找到" << endl;
}
return 0;
}
自定义数据类型的查找
find是通过“==”来判断容器中是否存在要查找的元素,系统是不认识自定义数据类型的,所以需要来通过重写“= =”来进行自定义数据类型的查找
class Person
{
public:
Person(string name, int age)
{
this->m_age = age;
this->m_name = name;
}
bool operator== (const Person&p)//重写==
{
return this->m_name == p.m_name && this->m_age == p.m_age;
}
string m_name;
int m_age;
};
int main()
{
vector<Person> v;
Person p1("a", 10);
Person p2("b", 20);
Person p3("c", 30);
Person p4("d", 40);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
vector<Person>::iterator pos = find(v.begin(), v.end(), p2);
if (pos != v.end())
{
cout << (*pos).m_name << " " << (*pos).m_age << endl;
}
else
{
cout << "没有找到" << endl;
}
return 0;
}
find_if
通过某条件去查找相应的元素,返回的类型也是迭代器。
指针类型的查找
这里是指针类型的数据的查找(容器中的元素的数据类型是指针类型),如果直接使用find的话,结果是没有意义的,因为通过值的查找的结果肯定是不一样的(地址肯定是不一样的),所以需要过某条件去查找(条件可以通过直接修改仿函数中的对应条件,也可以通过将具有对应的相同数据的指针元素与仿函数绑定的形式(适配器)去查找)
class Person
{
public:
Person(string name,int age)
{
this->m_name = name;
this->m_age = age;
}
string m_name;
int m_age;
};
class MyCompare : public binary_function<Person*,Person*,bool>
{
public:
bool operator()(Person* p1,Person* p2) const
{
return p1->m_name == p2->m_name && p1->m_age == p2->m_age;
}
};
int main()
{
vector<Person*> v;
Person p1("a", 10);
Person p2("b", 20);
Person p3("c", 30);
Person p4("d", 40);
v.push_back(&p1);
v.push_back(&p2);
v.push_back(&p3);
v.push_back(&p4);
Person* p = new Person("c", 30);
vector<Person*>::iterator pos = find_if(v.begin(), v.end(), bind2nd(MyCompare(),p));
if (pos != v.end())
{
cout <<"找到了对应的元素"<<"姓名是:"<< (*pos)->m_name << endl;
}
else
{
cout << "没有找到" << endl;
}
return 0;
}
小总结:
find和find_if的第一个和第二个参数是指查找迭代器的范围。find的第三个参数是查找的值,find_if的第三个参数是可调用函数,该函数只能有一个传入参数(这个参数是重载()的仿函数)并返回布尔值,若大于一个参数则需要使用bind系列函数
adjacent_find(相邻重复元素)
查找相邻的重复元素,返回值是迭代器
int main()
{
vector<int>v;
v.push_back(1);
v.push_back(3);
v.push_back(2);
v.push_back(5);
v.push_back(5);
//容器中显然是有相邻的重复元素的,那么使用这个函数就会有反馈
vector<int>::iterator pos = adjacent_find(v.begin(), v.end());
if (pos != v.end())
{
cout << "找到了相邻的重复元素是" << *pos << endl;//这个迭代器指向的第一个相邻重复元素的位置
}
else
{
cout << "没有找到" << endl;
}
return 0;
}
binary_search(二分查找)
通过二分查找法的方法查找容器里面有没有对应的迭代器,返回值是bool类型。前提是容器中的元素必须是有序的。
int main()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
bool ret = binary_search(v.begin(), v.end(), 1);
if (ret)
{
cout << "查到了相应的数据" << endl;
}
else
{
cout << "没有查到相应的数据" << endl;
}
return 0;
}
count(统计)
统计对应数据的个数
int main()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
v.push_back(3);
v.push_back(3);
int num = count(v.begin(), v.end(), 2);
cout << num << endl;
return 0;
}
count_if
统计符合条件的数据的个数,需要谓词:仿函数
class GreaterThan3
{
public:
bool operator()(int val)
{
return val > 3;
}
};
int main()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
v.push_back(3);
v.push_back(3);
int num = count_if(v.begin(), v.end(), GreaterThan3());
cout << num << endl;//6
return 0;
}