0
点赞
收藏
分享

微信扫一扫

常用查找算法——STL

木匠0819 2022-04-04 阅读 106
c++算法

文章目录

常用查找算法


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;
}

自定义数据类型的查找
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


指针类型的查找
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;
}

小总结



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(二分查找)

 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;
	}
举报

相关推荐

0 条评论