查找函数

STL find()

find() 函数本质上是一个模板函数,用于在指定范围内查找和目标元素值相等的第一个元素。

如下为 find() 函数的语法格式:

InputIterator find (InputIterator first, InputIterator last, const T& val);

其中,first 和 last 为输入迭代器,[first, last) 用于指定该函数的查找范围;val 为要查找的目标元素。

正因为 first 和 last 的类型为输入迭代器,因此该函数适用于所有的序列式容器。

另外,该函数会返回一个输入迭代器,当 find() 函数查找成功时,其指向的是在 [first, last) 区域内查找到的第一个目标元素;如果查找失败,则该迭代器的指向和 last 相同

值得一提的是,find() 函数的底层实现,其实就是用==运算符将 val 和 [first, last) 区域内的元素逐个进行比对。这也就意味着,[first, last) 区域内的元素必须支持==运算符。

  1. #include <iostream> // std::cout
  2. #include <algorithm> // std::find
  3. #include <vector> // std::vector
  4. using namespace std;
  5. int main() {
  6. //find() 函数作用于普通数组
  7. char stl[] ="http://c.biancheng.net/stl/";
  8. //调用 find() 查找第一个字符 'c'
  9. char * p = find(stl, stl + strlen(stl), 'c');
  10. //判断是否查找成功
  11. if (p != stl + strlen(stl)) {
  12. cout << p << endl;
  13. }
  14. //find() 函数作用于容器
  15. std::vector<int> myvector{ 10,20,30,40,50 };
  16. std::vector<int>::iterator it;
  17. it = find(myvector.begin(), myvector.end(), 30);
  18. if (it != myvector.end())
  19. cout << "查找成功:" << *it;
  20. else
  21. cout << "查找失败";
  22. return 0;
  23. }

程序执行结果为:

  1. c.biancheng.net/stl/
  2. 查找成功:30

可以看到,find() 函数除了可以作用于序列式容器,还可以作用于普通数组。

STL find_if()

find_if() 同 find() 一样,为在输入迭代器所定义的范围内查找单个对象的算法,它可以在前两个参数指定的范围内查找可以使第三个参数指定的谓词返回 true 的第一个对象。谓词不能修改传给它的对象。

find_if() 会返回一个指向被找到对象的迭代器,如果没有找到对象,会返回这个序列的结束迭代器

可以按如下方式使用 find_if() 来查找 numbers 中第一个大于 value 的元素:

  1. int value {5};
  2. auto iter1 = std::find_if(std::begin(numbers), std::end(numbers),[value](int n) { return n > value; });
  3. if(iter1 != std::end(numbers))
  4. std::cout << *iter1 << " was found greater than " << value << ".\n";

find_if() 的第三个参数是一个 lambda 表达式的谓词。这个 lambda 表达式以值的方式捕获 value,并在 lambda 参数大于 value 时返回 true。这段代码会找到一个值为 46 的元素。

二分查找的函数

二分查找的函数有 3 个:

1.lower_bound(起始地址,结束地址,要查找的数值) 返回大于或等于val的第一个元素位置。返回的是数值第一个出现的位置。

2.upper_bound(起始地址,结束地址,要查找的数值) 返回大于val的第一个元素位置。返回的是数值最后一个 出现的位置。

3.binary_search(起始地址,结束地址,要查找的数值) 返回的是是否存在这么一个数,是一个bool值。
binary_search() 用来在一个有序区间中使用二分法查找元素是否在这个区间中,该算法的返回值为bool表示是否存在。

  1. template <class ForwardIterator, class T>
  2. bool binary_search (ForwardIterator first, ForwardIterator last, const T& val)
  3. {
  4. first = std::lower_bound(first,last,val);
  5. return (first!=last && !(val<*first));
  6. }

举例:

数组 a(下标从1开始) : 1 2 3 3 3 4 5。查找 3 :

  1. int position1 = lower_bound(a+1,a+n,3) - a; //position1 = 3
  2. int position2 = upper_bound(a+1,a+n,3) - a; //position2 = 5

需要注意的是:如果数组中没有找到所求元素,函数就会返回一个假想的插入位置。

int a[6] = {0,1,2,4,5,7};
    int position1 = lower_bound(a+1,a+6,3) - a;
    int position2 = upper_bound(a+1,a+6,3) - a;
    cout << position1 << endl;
    cout << position2 << endl;
// position1 = position2 = 3

二分查找的首要条件是数列有序!

区间查找(区间整体匹配)

  1. search() 查找子区间首次出现的位置
  • 与find() 用来查找单个元素相比,search() 用来查找一个子区间。比如 从myvector中查找区间[20, 30] 的位置 ```cpp int needle1[] = {20,30};

it = std::search (myvector.begin(), myvector.end(), needle1, needle1+2);

if (it!=myvector.end())

std::cout << “needle1 found at position “ << (it-myvector.begin()) << ‘\n’;


- search() 支持自定义比较函数,比如查询给定区间中每个元素比目标区间小1的子区间:
```cpp
#include <iostream>
#include <vector>

using namespace std;

bool cmpFunction (int i, int j) {

  return (i-j==1);

}

int main(int, char**) {


int myints[] = {1,2,3,4,5,1,2,3,4,5};

std::vector<int> haystack (myints,myints+10);

int needle2[] = {1,2,3};

// using predicate comparison:

std::vector<int>::iterator it = std::search (haystack.begin(), haystack.end(), needle2, needle2+3, cmpFunction);

if (it!=haystack.end())
    std::cout << "needle2 found at position " << (it-haystack.begin()) << '\n';
  else
    std::cout << "needle2 not found\n";

  return 0;
}

输出:

needle2 found at position 1
  1. find_end() 查找子区间最后一次出现的位置
  • search()查找子区间第一次出现的位置,而find_end() 用来查找子区间最后一次出现的位置, find_end()支持自定义比较函数。

排序sort

sort(first_pointer,first_pointer+n,cmp)

该函数可以给数组,或者链表list、向量排序。

实现原理:sort并不是简单的快速排序,它对普通的快速排序进行了优化,此外,它还结合了插入排序和推排序。系统会根据你的数据形式和数据量自动选择合适的排序方法,这并不是说它每次排序只选择一种方法,它是在一次完整排序中不同的情况选用不同方法,比如给一个数据量较大的数组排序,开始采用快速排序,分段递归,分段之后每一段的数据量达到一个较小值后它就不继续往下递归,而是选择插入排序,如果递归的太深,他会选择推排序。

此函数有3个参数:

参数1:第一个参数是数组的首地址,一般写上数组名就可以,因为数组名是一个指针常量。

参数2:第二个参数相对较好理解,即首地址加上数组的长度n(代表尾地址的下一地址)。

参数3:默认可以不填,如果不填sort会默认按数组升序排序。也就是1,2,3,4排序。也可以自定义一个排序函数,改排序方式为降序什么的,也就是4,3,2,1这样。

使用此函数需先包含:

#include <algorithm>

并且导出命名空间:

using namespace std;

sort不只是能像上面那样简单的使用,我们可以对sort进行扩展,关键就在于第三个参数,我们想降序排列,或者说我不是一个简简单单的数组,而是结构体、类怎么办,下面给出一些方法和例子。

方法一:定义比较函数(最常用)

//情况一:数组排列
int A[100];
bool cmp1(int a,int b)//int为数组数据类型
{
return a>b;//降序排列
//return a<b;//默认的升序排列
}
sort(A,A+100,cmp1);

//情况二:结构体排序
Student Stu[100];
bool cmp2(Student a,Student b)
{
return a.id>b.id;//按照学号降序排列
//return a.id<b.id;//按照学号升序排列
}
sort(Stu,Stu+100,cmp2);

注:比较方法也可以放在结构体中或类中定义。

//情况一:在结构体内部重载
struct Student{
    int id;
    string name;
    double grade;

    bool operator<(const Student& s)
    {
        return id>s.id;//降序排列
        //return id<s.id;//升序排列
    }
};
vector<Student> V;
sort(V.begin(),V.end());


//情况二:函数对象
struct Less
{
    bool operator()(const Student& s1, const Student& s2)
    {
        return s1.id<s2.id; //升序排列
    }
};
sort(sutVector.begin(),stuVector.end(),Less());

方法二:使用标准库函数

另外,其实我们还可以再懒一点,在标准库中已经有现成的。它在哪呢?答案是functional,我们include进来试试看。

functional提供了一堆基于模板的比较函数对象,它们是:equal_to<Type>not_equal_to<Type>greater<Type>greater_equal<Type>less<Type>less_equal<Type>。这些东西的用法看名字就知道了。

在这里,我么sort要用到的也只是greater和less就足够了,用法如下:

升序:sort(begin,end,less<data-type>())

降序:sort(begin,end,greater<data-type>())

方法三:使用lambda表达式

与functional同理,我们可以使用Lambda表达式代替functional接口。

比如Leetcode 179. Largest Number 中自定义排序如下:

string largestNumber(vector<int>& nums) {
    //自定义拼接后比大小的函数
    sort(nums.begin(),nums.end(),[](const int& a, const int& b){
        long ax = 10;
        long bx = 10;
        while(ax<=a){ //大于a的最小10的幂
            ax*=10;
        }
        while(bx<=b){
            bx*=10;
        }
        return a*bx+b>b*ax+a;
    });

    if (nums[0] == 0) {
        return "0";
    }
    string out;
    for(int& x:nums){
        out+=to_string(x);
    }
    return out;
}