20.2 常用查找算法

20.2 常用查找算法

算法简介:

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

find

功能描述:

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

函数原型:

  • `find(iterator beg, iterator end, value);
    //按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
    //beg 开始迭代器//end 结束迭代器
    //value 查找的元素

下面演示了查找内置数据类型和自定义数据类型。

#include 
#include 
#include 
#include 
using namespace std;

//查找内置数据类型
void test1()
{
     
    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->name = name;
        this->age = age;
    }
    bool operator==(const Person&p)
    {
     
        if (this->name == p.name && this->age == p.age)
        {
     
            return true;
        }
        else
        {
     
            return false;
        }
    }
    string name;
    int age;
};

void test2()
{
     
    vector<Person>v;
    Person p1("aaa", 10);
    Person p2("bbb", 20);
    Person p3("ccc", 30);
    Person p4("ddd", 40);
    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);

    Person pp("bbb", 20);
    vector<Person>::iterator it = find(v.begin(), v.end(), pp);
    if (it == v.end())
    {
     
        cout << "没有找到" << endl;
    }
    else
    {
     
        cout << "找到:姓名:" << it->name << " 年龄:" << it->age << endl;
    }
}

int main()
{
     
    test1();
    test2();
}

20.2 常用查找算法_第1张图片

find_if

功能描述:

  • 按条件查找元素

函数原型:

  • find_if(iterator beg, iterator end, _Pred)
    //按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
    //beg 开始迭代器
    //end 结束迭代器
    //_Pred 函数或者谓词(返回bool类型的仿函数)
#include 
#include 
#include 
#include 
using namespace std;

class GreatFive
{
     
public:
	bool operator()(int val)
	{
     
		return val > 5;
	}
};

void test1()
{
     
	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
     
		v.push_back(i);
	}

	vector<int>::iterator it = find_if(v.begin(), v.end(), GreatFive());
	if (it == v.end())
	{
     
		cout << "未找到" << endl;
	}
	else
	{
     
		cout << "找到了:" << *it << endl;
	}
}

//自定义数据类型
class Person
{
     
public:
	Person(string name, int age)
	{
     
		this->name = name;
		this->age = age;
	}
	string name;
	int age;
};

class Greater20
{
     
public:
	bool operator()(const Person & p)
	{
     
		return p.age > 20;
	}
};

void test2()
{
     
	vector<Person>v;
	Person p1("aaa", 10);
	Person p2("aaa", 20);
	Person p3("aaa", 30);
	Person p4("aaa", 40);
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	vector<Person>::iterator it = find_if(v.begin(), v.end(), Greater20());
	if (it == v.end())
	{
     
		cout << "未找到" << endl;
	}
	else
	{
     
		cout << "找到了:" << it->age << endl;
	}

}

int main()
{
     
	test1();
	test2();
}

20.2 常用查找算法_第2张图片

adjacent_find

功能描述:

  • 查找相邻重复元素

函数原型:

  • adjacent_find(iterator beg, iterator end);
    //查找相邻重复元素,返回相邻元素的第一个位置的迭代器
    //beg 开始迭代器
    //end 结束迭代器
#include 
#include 
#include 
using namespace std;

void test1()
{
     
	vector<int>v;
	v.push_back(0);
	v.push_back(2);
	v.push_back(0);
	v.push_back(3);
	v.push_back(1);
	v.push_back(4);
	v.push_back(3);
	v.push_back(3);

	vector<int>::iterator it = adjacent_find(v.begin(), v.end());
	if (it == v.end())
	{
     
		cout << "未找到相邻重复元素" << endl;
	}
	else
	{
     
		cout << "找到了:" << *it << endl;
	}
}

int main()
{
     
	test1();
}

20.2 常用查找算法_第3张图片

binary_search

功能描述:

  • 查找指定元素是否存在

函数原型:

  • bool binary_search(iterator beg, iterator end, value);
    //查找指定的元素,查到返回true 否则false
    //注意:在无序序列中不可用
    //beg 开始迭代器
    //end 结束迭代器
    //value查找的元素
#include 
#include 
#include 
using namespace std;

void test1()
{
     
	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
     
		v.push_back(i);
	}
	//v.push_back(2);
	//容器必须是有序序列
	bool ret = binary_search(v.begin(), v.end(), 9);
	if (ret)
	{
     
		cout << "找到了" << endl;
	}
	else
	{
     
		cout << "未找到" << endl;
	}
}

int main()
{
     
	test1();
}

20.2 常用查找算法_第4张图片

count

功能描述:

  • 统计元素个数

函数原型:

  • count(iterator beg, iterator end, value);
    //统计元素出现次数
    //beg 开始迭代器
    //end 结束迭代器
    //value 统计的元素
#include 
#include 
#include 
using namespace std;

void test1()
{
     
	vector<int>v;
	v.push_back(10);
	v.push_back(40);
	v.push_back(30);
	v.push_back(40);
	v.push_back(20);
	v.push_back(40);

	int num = count(v.begin(), v.end(), 40);
	cout << "40的元素个数为:" << num << endl;
}

class Person
{
     
public:
	Person(string name, int age)
	{
     
		this->name = name;
		this->age = age;
	}
	bool operator==(const Person &p)
	{
     
		return this->age == p.age;
	}
	string name;
	int age;
};

void test2()
{
     
	vector<Person>v;
	Person p1("刘备", 35);
	Person p2("关羽", 35);
	Person p3("张飞", 35);
	Person p4("赵云", 40);
	Person p5("曹操", 40);
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	v.push_back(p5);

	Person p("诸葛亮", 35);

	int num = count(v.begin(), v.end(), p);
	cout << "和诸葛亮同岁数的个数为:" << num << endl;
}

int main()
{
     
	test1();
}

20.2 常用查找算法_第5张图片

count_if

功能描述:

  • 按条件统计元素个数

函数原型:

  • count_if(iterator beg, iterator end, _Pred);
    //按条件统计元素出现次数
    //beg 开始迭代器
    //end 结束迭代器
    //_Pred 谓词
#include 
#include 
#include 
using namespace std;

class Greater20
{
     
public:
	bool operator()(int val)
	{
     
		return val > 20;
	}
};

void test1()
{
     
	vector<int>v;
	v.push_back(10);
	v.push_back(40);
	v.push_back(30);
	v.push_back(20);
	v.push_back(40);
	v.push_back(20);
	int num = count_if(v.begin(), v.end(), Greater20());
	cout << "大于20的元素个数为:" << num << endl;
}

class Person
{
     
public:
	Person(string name, int age)
	{
     
		this->name = name;
		this->age = age;
	}
	string name;
	int age;
};

class AgeGreater20
{
     
public:
	bool operator()(const Person& p)
	{
     
		return p.age > 20;
	}
};

void test2()
{
     
	vector<Person>v;
	Person p1("刘备", 35);
	Person p2("关羽", 35);
	Person p3("张飞", 35);
	Person p4("赵云", 40);
	Person p5("曹操", 20);
	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(), AgeGreater20());
	cout << "大于20岁的人员个数:" << num << endl;
}

int main()
{
     
	test1();
	test2();
}

20.2 常用查找算法_第6张图片

你可能感兴趣的:(C++完整学习笔记,c++)