【C++提高编程-08】----C++ STL之常用查找算法

🎩 欢迎来到技术探索的奇幻世界👨‍💻

📜 个人主页@一伦明悦-CSDN博客

✍🏻 作者简介: C++软件开发、Python机器学习爱好者

🗣️ 互动与支持💬评论      👍🏻点赞      📂收藏     👀关注+

如果文章有所帮助,欢迎留下您宝贵的评论,

点赞加收藏支持我,点击关注,一起进步!

前言

       STL(Standard Template Library)是C++标准库的一部分,提供了丰富的数据结构和算法,用于处理数据和实现常见的计算任务。STL中的算法分为几类,包括遍历算法、修改算法、排序算法、查找算法、数值算法等,每类算法都有其特定的应用场景和功能。

正文

01-查找算法之find用法

       find 是STL中的一个算法,用于在容器中查找指定值的元素。以下是关于 find 的详细介绍和用法示例:

find 用法详解

语法

template <class InputIterator, class T>
InputIterator find (InputIterator first, InputIterator last, const T& value);
  • InputIterator:表示容器或范围的迭代器类型,用于指定查找范围。
  • T:表示要查找的值的类型。
  • first:表示要查找的范围的起始位置。
  • last:表示要查找的范围的结束位置,不包含在范围内。
  • value:表示要查找的值。

参数

  • first:要查找的范围的起始位置。
  • last:要查找的范围的结束位置,不包含在范围内。
  • value:要查找的值。

功能

find 算法在 [first, last) 范围内查找第一个等于 value 的元素,并返回指向该元素的迭代器。如果未找到匹配元素,则返回 last

返回值

如果找到匹配的元素,则返回指向该元素的迭代器;如果未找到,则返回 last

示例

假设有一个整数向量 numbers,我们想要查找值为 3 的元素。可以这样使用 find

#include <iostream>
#include <vector>
#include <algorithm> // 包含 find 函数

int main() {
    std::vector<int> numbers = {1, 2, 3, 4, 5};

    // 使用 find 查找值为 3 的元素
    std::vector<int>::iterator it = std::find(numbers.begin(), numbers.end(), 3);

    // 检查是否找到
    if (it != numbers.end()) {
        std::cout << "Found the value: " << *it << std::endl;
    } else {
        std::cout << "Value not found." << std::endl;
    }

    return 0;
}

输出结果将是 Found the value: 3。在这个例子中,find 函数在整数向量 numbers 中找到了值为 3 的元素,并返回指向该元素的迭代器。

注意事项

  • find 算法对于有序和无序容器都适用,但对于有序容器,可以使用更高效的二分查找算法。
  • 如果范围为空 [first, last),则 find 返回 last
  • find 算法的时间复杂度为线性时间,即 O(n),其中 n 是范围内的元素个数。

通过 find 算法,可以方便地在容器中查找指定值的元素,是处理查找操作的常用工具之一。

下面给出具体代码:

这段代码主要展示了如何使用 std::find 函数来查找容器中特定值或自定义对象的元素,并对查找结果进行处理。

函数 test01()

void test01() {
    vector<int> v;
    for (int i = 0; i < 10; i++) {
        v.push_back(i + 1); // 将整数 1 到 10 添加到 vector 中
    }
    
    // 使用 find 查找容器中是否有值为 5 的元素
    vector<int>::iterator it = find(v.begin(), v.end(), 5);
    
    if (it == v.end()) {
        cout << "没有找到!" << endl;
    } else {
        cout << "找到:" << *it << endl;
    }
}
  • 功能解析
    • 创建了一个存储整数的 vector,并通过循环将整数 1 到 10 添加到 vector 中。
    • 使用 std::find 函数在 vector 中查找值为 5 的元素。
    • 如果找到了该元素,则输出 “找到: 5”;如果未找到,则输出 “没有找到!”。

类 Person 和函数 test02()

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

void test02() {
    vector<Person> v;
    // 创建 Person 对象
    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);
    
    // 使用 find 查找 Person 对象 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;
    }
}
  • 功能解析
    • 定义了一个 Person 类,具有姓名和年龄两个成员变量,并重载了 == 运算符,以便 std::find 函数可以根据对象的成员比较来查找。
    • 在 test02() 函数中,创建了一个存储 Person 对象的 vector,并向其中添加了几个 Person 对象。
    • 使用 std::find 函数在 vector 中查找特定的 Person 对象 p2
    • 如果找到了 p2,则输出其姓名和年龄;如果未找到,则输出 “没有找到!”。

        这段代码演示了 std::find 函数在不同类型的容器(vector<int> 和 vector<Person>)中的用法。对于基本类型(如 int),std::find 直接比较值;对于自定义类型(如 Person),需要重载 == 运算符来进行比较。通过 std::find 函数,可以在容器中方便地查找特定值或对象,并根据查找结果执行相应的操作。

#include <algorithm>
#include <vector>
#include <string>
void test01() {
	vector<int> v;
	for (int i = 0; i < 10; i++) {
		v.push_back(i + 1);
	}
	//查找容器中是否有 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;
	}
	//重载==
	bool operator==(const Person& p)
	{
		if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
		{
			return true;
		}
		return false;
	}
public:
	string m_Name;
	int m_Age;
};
void test02() {
	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);
	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;
	}
}

02-查找算法之find_if用法

       find_if 是STL中的另一个算法,与 find 类似,但允许使用自定义的条件(谓词)来查找元素。以下是关于 find_if 的详细介绍和用法示例:

find_if 用法详解

语法

template <class InputIterator, class UnaryPredicate>
InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred);
  • InputIterator:表示容器或范围的迭代器类型,用于指定查找范围。
  • UnaryPredicate:是一个函数或函数对象,用于定义查找条件。
  • first:表示要查找的范围的起始位置。
  • last:表示要查找的范围的结束位置,不包含在范围内。
  • pred:是一个谓词(返回值为 bool 的函数或函数对象),用于定义查找条件。

参数

  • first:要查找的范围的起始位置。
  • last:要查找的范围的结束位置,不包含在范围内。
  • pred:定义了查找条件的谓词。

功能

find_if 算法在 [first, last) 范围内查找第一个满足条件 pred 的元素,并返回指向该元素的迭代器。如果未找到匹配元素,则返回 last

返回值

如果找到满足条件的元素,则返回指向该元素的迭代器;如果未找到,则返回 last

示例

假设有一个整数向量 numbers,我们想要查找第一个大于 3 的元素。可以这样使用 find_if

#include <iostream>
#include <vector>
#include <algorithm> // 包含 find_if 函数

bool isGreaterThan3(int value) {
    return value > 3;
}

int main() {
    std::vector<int> numbers = {1, 2, 3, 4, 5};

    // 使用 find_if 查找第一个大于 3 的元素
    std::vector<int>::iterator it = std::find_if(numbers.begin(), numbers.end(), isGreaterThan3);

    // 检查是否找到
    if (it != numbers.end()) {
        std::cout << "Found the first element greater than 3: " << *it << std::endl;
    } else {
        std::cout << "No element greater than 3 found." << std::endl;
    }

    return 0;
}

输出结果将是 Found the first element greater than 3: 4。在这个例子中,find_if 函数在整数向量 numbers 中找到了第一个大于 3 的元素 4,并返回指向该元素的迭代器。

注意事项

  • find_if 算法允许使用自定义的谓词 pred,使得查找条件更加灵活。
  • 谓词可以是一个函数指针、函数对象或者 lambda 表达式,用于定义查找条件。
  • 如果范围为空 [first, last),则 find_if 返回 last
  • find_if 算法的时间复杂度为线性时间,即 O(n),其中 n 是范围内的元素个数。

通过 find_if 算法,可以方便地在容器中查找满足特定条件的元素,是处理条件性查找操作的常用工具之一。

下面给出具体代码分析应用过程:

这段代码展示了如何使用 find_if 算法查找满足特定条件的元素,包括内置数据类型和自定义数据类型。

  • 首先,定义了两个谓词类 GreaterFive 和 Greater20,分别用于判断数字是否大于 5 和 20
  • 然后,通过 find_if 函数在两个不同的场景下进行查找:
    1. 在存储了整数的 vector 中,查找第一个大于 5 的数字。
    2. 在存储了自定义类型 Person 的 vector 中,查找年龄大于 20 的人。

在每个场景中,都创建了一个谓词对象,并将其作为参数传递给 find_if 函数,以指定查找条件。如果找到满足条件的元素,则返回指向该元素的迭代器;否则返回 end() 迭代器,表示未找到。

#include <algorithm>
#include <vector>
#include <string>
//内置数据类型
class GreaterFive
{
public:
	bool operator()(int val)
	{
		return val > 5;
	}
};
void test01() {
	vector<int> v;
	for (int i = 0; i < 10; i++) {
		v.push_back(i + 1);
	}
	vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive());
	if (it == v.end()) {
		cout << "没有找到!" << endl;
	}
	else {
		cout << "找到大于5的数字:" << *it << endl;
	}
}
//自定义数据类型
class Person {
public:
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}
public:
	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", 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);
	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;
}

03-查找算法之adjacent_find用法

       adjacent_find 是STL中的另一个算法,用于在容器或范围中查找相邻的重复元素,并返回指向第一对重复元素的迭代器。以下是关于 adjacent_find 的详细介绍和用法示例:

adjacent_find 用法详解

语法

template <class ForwardIterator>
ForwardIterator adjacent_find (ForwardIterator first, ForwardIterator last);
  • ForwardIterator:表示容器或范围的前向迭代器类型,用于指定查找范围。
  • first:表示要查找的范围的起始位置。
  • last:表示要查找的范围的结束位置,不包含在范围内。

参数

  • first:要查找的范围的起始位置。
  • last:要查找的范围的结束位置,不包含在范围内。

功能

adjacent_find 算法在 [first, last) 范围内查找第一对相邻的重复元素,并返回指向第一对重复元素的第一个元素的迭代器。如果未找到重复元素,则返回 last

返回值

如果找到相邻的重复元素,则返回指向第一对重复元素的第一个元素的迭代器;如果未找到,则返回 last

示例

假设有一个整数向量 numbers,我们想要查找第一对相邻的重复元素。可以这样使用 adjacent_find

#include <iostream>
#include <vector>
#include <algorithm> // 包含 adjacent_find 函数

int main() {
    std::vector<int> numbers = {1, 2, 2, 3, 4, 4, 5};

    // 使用 adjacent_find 查找第一对相邻的重复元素
    std::vector<int>::iterator it = std::adjacent_find(numbers.begin(), numbers.end());

    // 检查是否找到
    if (it != numbers.end()) {
        std::cout << "Found the first adjacent duplicate: " << *it << std::endl;
    } else {
        std::cout << "No adjacent duplicates found." << std::endl;
    }

    return 0;
}

输出结果将是 Found the first adjacent duplicate: 2。在这个例子中,adjacent_find 函数在整数向量 numbers 中找到了第一对相邻的重复元素 2,并返回指向这对重复元素的第一个 2 的迭代器。

注意事项

  • adjacent_find 算法仅查找相邻的重复元素,不支持自定义条件。
  • 如果范围为空 [first, last),则 adjacent_find 返回 last
  • adjacent_find 算法的时间复杂度为线性时间,即 O(n),其中 n 是范围内的元素个数。

通过 adjacent_find 算法,可以方便地在容器中查找并处理相邻的重复元素,是处理这类需求的常用工具之一。

        这段代码演示了如何使用 adjacent_find 算法来查找给定整数向量中的第一对相邻重复元素:

#include <algorithm>
#include <vector>
#include <iostream>

void test01()
{
    // 创建一个整数向量
    std::vector<int> v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(5);
    v.push_back(2);
    v.push_back(4);
    v.push_back(4);
    v.push_back(3);

    // 使用 adjacent_find 查找第一对相邻的重复元素
    std::vector<int>::iterator it = std::adjacent_find(v.begin(), v.end());

    // 检查是否找到相邻重复元素
    if (it == v.end()) {
        std::cout << "找不到相邻重复元素!" << std::endl;
    }
    else {
        std::cout << "找到相邻重复元素为: " << *it << std::endl;
    }
}

int main() {
    test01();
    return 0;
}

在这个例子中,adjacent_find 函数查找整数向量 v 中的第一对相邻重复元素。输出结果将是 “找到相邻重复元素为: 2”,因为整数向量中第一对相邻重复元素为 2

04-查找算法之binary_search用法

       binary_search 是STL中的一个查找算法,用于在已排序的序列中快速查找特定元素。以下是关于 binary_search 的详细介绍和用法示例:

binary_search 用法详解

语法

template <class ForwardIterator, class T>
bool binary_search (ForwardIterator first, ForwardIterator last, const T& val);
  • ForwardIterator:表示容器或范围的前向迭代器类型,用于指定查找范围。
  • T:表示要查找的元素的类型。
  • first:表示要查找的范围的起始位置。
  • last:表示要查找的范围的结束位置,不包含在范围内。
  • val:要查找的目标值。

参数

  • first:要查找的范围的起始位置。
  • last:要查找的范围的结束位置,不包含在范围内。
  • val:要查找的目标值。

功能

binary_search 算法在 [first, last) 范围内执行二分查找,寻找是否存在值等于 val 的元素。如果找到目标元素,则返回 true;否则返回 false

返回值

  • true:如果找到值等于 val 的元素。
  • false:如果未找到值等于 val 的元素。

示例

假设有一个已排序的整数向量 numbers,我们想要查找其中是否包含特定的值。可以这样使用 binary_search

#include <iostream>
#include <vector>
#include <algorithm> // 包含 binary_search 函数

int main() {
    std::vector<int> numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    // 使用 binary_search 查找值为 5 的元素
    bool found = std::binary_search(numbers.begin(), numbers.end(), 5);

    // 检查是否找到
    if (found) {
        std::cout << "找到值为 5 的元素!" << std::endl;
    } else {
        std::cout << "未找到值为 5 的元素." << std::endl;
    }

    return 0;
}

输出结果将是 “找到值为 5 的元素!”。在这个例子中,binary_search 函数在已排序的整数向量 numbers 中成功找到值为 5 的元素。

注意事项

  • binary_search 算法要求在调用之前,被搜索的范围必须是已排序的。如果范围不是有序的,结果是未定义的。
  • 如果有多个元素的值等于 valbinary_search 可能返回任意一个匹配元素的位置。
  • binary_search 算法的时间复杂度为对数时间 O(log n),其中 n 是范围内的元素个数。这使得它非常适合于大型数据集的快速查找。

通过 binary_search 算法,可以高效地判断已排序容器中是否包含特定值,是处理此类查找需求的首选工具之一。

这段代码展示了如何使用 binary_search 算法在已排序的整数向量中查找特定元素的过程:

#include <algorithm>
#include <vector>
#include <iostream>

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

    // 使用 binary_search 进行二分查找
    bool ret = std::binary_search(v.begin(), v.end(), 2);

    // 检查是否找到目标元素
    if (ret)
    {
        std::cout << "找到了值为 2 的元素" << std::endl;
    }
    else
    {
        std::cout << "未找到值为 2 的元素" << std::endl;
    }
}

int main() {
    test01();
    return 0;
}

在这个例子中,首先创建了一个整数向量 v,包含了从0到9的整数。然后使用 binary_search 函数来查找向量中是否包含值为 2 的元素。输出结果将是 “找到了值为 2 的元素”,因为向量 v 中确实包含值为 2 的元素。

这段代码的要点包括:

  • 使用 std::binary_search 函数进行二分查找,该函数要求输入的序列必须是已排序的。
  • 如果查找到目标元素,binary_search 返回 true,否则返回 false
  • 这种二分查找算法的时间复杂度为对数时间 O(log n),效率较高,特别适用于大型数据集的查找操作。

通过这样的例子和解释,可以清晰地理解和使用 binary_search 算法来实现快速的查找功能。

05-查找算法之count用法     

count 算法详解

语法

template <class InputIterator, class T>
typename iterator_traits<InputIterator>::difference_type
  count (InputIterator first, InputIterator last, const T& val);
  • InputIterator:表示容器或范围的输入迭代器类型,用于指定查找范围。
  • T:表示要计数的元素的类型。
  • first:表示要计数的范围的起始位置。
  • last:表示要计数的范围的结束位置,不包含在范围内。
  • val:要计数的目标值。

参数

  • first:要计数的范围的起始位置。
  • last:要计数的范围的结束位置,不包含在范围内。
  • val:要计数的目标值。

功能

count 算法用于计算范围 [first, last) 中值等于 val 的元素个数,并返回这个计数值。

返回值

  • count 函数返回一个整数值,表示范围内值等于 val 的元素个数。

示例

假设有一个整数向量 numbers,我们想要计算其中特定值的出现次数。可以这样使用 count

#include <iostream>
#include <vector>
#include <algorithm> // 包含 count 函数

int main() {
    std::vector<int> numbers = {1, 2, 2, 3, 2, 4, 2, 5};

    // 使用 count 统计值为 2 的元素个数
    int numTwos = std::count(numbers.begin(), numbers.end(), 2);

    std::cout << "值为 2 的元素出现次数为: " << numTwos << std::endl;

    return 0;
}

输出结果将是 “值为 2 的元素出现次数为: 4”。在这个例子中,count 函数统计了向量 numbers 中值为 2 的元素出现的次数,并将结果输出。

注意事项

  • count 算法不要求输入范围是有序的,它可以在任意顺序的范围中进行计数。
  • 如果范围中没有匹配 val 的元素,count 将返回 0
  • count 算法的时间复杂度为线性时间 O(n),其中 n 是范围内的元素个数。这使得它在大多数情况下都能够快速计算出结果。

通过 count 算法,可以方便地统计容器中特定值的出现次数,适用于各种数据结构和查找需求。

        这段代码演示了如何使用STL中的 count 算法来统计容器中特定元素出现的次数,包括内置数据类型和自定义数据类型的情况。

内置数据类型的示例 (test01 函数)

#include <algorithm>
#include <vector>
#include <iostream>

void test01()
{
    std::vector<int> v = {1, 2, 4, 5, 3, 4, 4};

    // 统计值为 4 的元素个数
    int num = std::count(v.begin(), v.end(), 4);

    std::cout << "值为 4 的元素个数为: " << num << std::endl;
}

int main() {
    test01();
    return 0;
}
  • 说明
    • test01 函数中首先创建了一个整数向量 v,包含了一些整数。
    • 使用 std::count 函数统计向量 v 中值为 4 的元素个数。
    • 输出结果将是 “值为 4 的元素个数为: 3”,因为向量 v 中值为 4 的元素出现了3次。

自定义数据类型的示例 (test02 函数)

#include <algorithm>
#include <vector>
#include <iostream>
#include <string>

class Person
{
public:
    Person(std::string name, int age)
        : m_Name(name), m_Age(age) {}

    bool operator==(const Person & p) const // 注意此处要声明为 const
    {
        return this->m_Age == p.m_Age;
    }

    std::string m_Name;
    int m_Age;
};

void test02()
{
    std::vector<Person> v;
    v.push_back(Person("刘备", 35));
    v.push_back(Person("关羽", 35));
    v.push_back(Person("张飞", 35));
    v.push_back(Person("赵云", 30));
    v.push_back(Person("曹操", 25));

    Person p("诸葛亮", 35);
    int num = std::count(v.begin(), v.end(), p);

    std::cout << "年龄为 35 的人数为: " << num << std::endl;
}

int main() {
    test02();
    return 0;
}
  • 说明
    • test02 函数中定义了一个 Person 类,包含姓名和年龄属性,并重载了 operator== 以便 count 函数可以进行正确比较。
    • 创建了一个 Person 对象的向量 v,并初始化几个对象。
    • 创建了一个名为 p 的 Person 对象,年龄为 35
    • 使用 std::count 函数统计向量 v 中年龄为 35 的人数。
    • 输出结果将是 “年龄为 35 的人数为: 3”,因为在向量 v 中有三个人的年龄是 35

总结

  • count 算法用于在指定范围内统计与给定值相等的元素个数。
  • 对于内置数据类型,直接使用即可。
  • 对于自定义数据类型,需要重载 operator== 以实现元素比较。

通过这两个示例,展示了 count 算法在不同数据类型场景下的使用方法及效果。

#include <algorithm>
#include <vector>
//内置数据类型
void test01()
{
	vector<int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(4);
	v.push_back(5);
	v.push_back(3);
	v.push_back(4);
	v.push_back(4);
	int num = count(v.begin(), v.end(), 4);
	cout << "4的个数为: " << 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("刘备", 35);
	Person p2("关羽", 35);
	Person p3("张飞", 35);
	Person p4("赵云", 30);
	Person p5("曹操", 25);
	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 = " << num << endl;
}
int main() {
	//test01();
	test02();
	system("pause");
	return 0;
}

总结

       查找算法(如 std::count)用于在容器或范围内查找特定元素或计算特定条件的元素个数。这些算法在C++标准库中提供了广泛的支持,能够处理多种数据类型和复杂的查找需求。总结如下:

  1. 基本概念

    • 查找算法用于在给定范围内搜索特定元素或满足特定条件的元素。
    • C++标准库提供了多种查找算法,如 std::countstd::findstd::find_if 等。
  2. 使用方法

    • 内置数据类型:对于基本数据类型(如 intdouble),直接使用标准库提供的算法即可,如使用 std::count 统计特定值出现的次数。
    • 自定义数据类型:对于自定义类型(如 class Person),需确保实现了比较操作符(通常是 operator==),以便算法能够正确比较对象。
  3. 算法特性

    • 参数:通常需要指定搜索范围的起始和结束迭代器,以及要搜索的值或条件。
    • 返回值:返回满足条件的元素个数或具体位置(视具体算法而定)。
  4. 注意事项

    • 查找算法的效率取决于具体实现,但大多数情况下时间复杂度是线性的(O(n)),其中 n 是范围内的元素个数。
    • 对于自定义数据类型,确保重载了所需的比较操作符,以确保算法能正确运作。
  5. 示例总结

    • 使用 std::count 可以快速统计容器中特定值的出现次数。
    • 使用 std::find 可以查找第一个匹配的元素。
    • 使用 std::find_if 可以根据自定义条件查找第一个符合条件的元素。

查找算法是C++标准库中常用的一部分,能够帮助开发者高效地处理数据查找和统计需求,同时也能应对各种复杂的应用场景。

  • 25
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一伦明悦

感谢,您的支持是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值