STL 中查找算法使用总结

顺序查找元素

find

①.头文件

包含在头文件 #include 中。

②.算法作用

使用 == 操作符,从给定的区间中查找和指定元素值相等的第一个元素,返回其迭代器。

③.适用容器

适用于所有的序列容器。

④.代码示例

vector<int> numbers = { 1,2,2,4,5,6};
//在 numbers 中查找第一个值是 2 到元素
auto iter = find(numbers.begin(), numbers.end(), 2);
if (iter != numbers.end())
{
  cout <<"查找到到位置:"<<  iter - numbers.begin() << endl;
}
else
{
  cout << "没有找到" << endl;
}

find_if

①.头文件

包含在头文件 #include 中。

②.算法作用

使用自定义比较函数,从给定的区间中查找符合规则的第一个元素,返回其迭代器。

③.适用容器

适用于所有的顺序容器和关联容器。

④.代码示例

auto cmpFunc = [&](int i) { return i == 4; };
  
vector<int> numbers = { 1,2,2,4,5,6};
//在 numbers 中查找第一个值是 4 到元素
auto iter = find_if(numbers.begin(), numbers.end(), cmpFunc);
if (iter != numbers.end())
{
    cout <<"查找到到位置:"<<  iter - numbers.begin() << endl;
}
else
{
    cout << "没有找到" << endl;
}

顺序查找序列

find_end

①.头文件

包含在头文件 #include 中。

②.算法作用

用于序列匹配,在一个序列中按规则查找子序列最后一次出现的位置,默认规则是相等匹配。

③.适用容器

适用于所有的顺序容器。

④.代码示例

auto cmpFunc = [&](int i,int j) {
    return i == j *2;
};

vector<int> numbers = { 1,2,2,4,8,10 };
vector<int> arry = { 4,5 };
//在 numbers 中查找的最后一次出现的元素值是 arry 中元素值 2 倍的元素
auto iter = find_end(numbers.begin(), numbers.end(), arry.begin(), arry.end(), cmpFunc);
if (iter != numbers.end())
{
  cout << "查找到到位置:" << iter - numbers.begin() << endl;
}
else
{
  cout << "没有找到" << endl;
}

find_first_of

①.头文件

包含在头文件 #include 中。

②.算法作用

用于序列匹配,在一个序列A中按规则查找和序列B匹配到第一个元素的位置,默认规则是相等匹配。

③.适用容器

适用于所有的顺序容器。

④.代码示例

auto cmpFunc = [&](int i,int j) {
    return i == j *2;
};

vector<int> numbers = { 1,2,2,4,8,10 };
vector<int> arry = { 4,5 };
//在 numbers 中查找 和 arry 匹配的第一个元素
auto iter = find_first_of(numbers.begin(), numbers.end(), arry.begin(), arry.end(), cmpFunc);
if (iter != numbers.end())
{
  cout << "查找到到位置:" << iter - numbers.begin() << endl;
}
else
{
  cout << "没有找到" << endl;
}

adjacent_find

①.头文件

包含在头文件 #include 中。

②.算法作用

在指定区间中查找符合规则到两个连续元素,默认规则是相等匹配。

③.适用容器

适用于所有的顺序容器。

④.代码示例

auto cmpFunc = [&](int i,int j) {
    return j == i *2;
};

vector<int> numbers = { 1,2,2,4,8,10 };
//在 numbers 中查找第一次出现的 2 个元素,满足后者是前者到 2 倍
auto iter = adjacent_find(numbers.begin(), numbers.end(),  cmpFunc);
if (iter != numbers.end())
{
  cout << "查找到到位置:" << iter - numbers.begin() << endl;
}
else
{
  cout << "没有找到" << endl;
}

search

①.头文件

包含在头文件 #include 中

②.算法作用

用于序列匹配,在一个序列中按规则查找子序列第一次出现到位置,默认规则是相等匹配。

③.适用容器

适用于所有的顺序容器

④.代码示例

auto cmpFunc = [&](int i,int j) {
    return j == i *2;
};

vector<int> numbers = { 1,2,2,4,8,10 };
vector<int> arry = { 2,4 };
//在 numbers 中查找第一次出现的元素值是 arry 中元素值 0.5 倍的元素
auto iter = search(numbers.begin(), numbers.end(),arry.begin(),arry.end(), cmpFunc);
if (iter != numbers.end())
{
    cout << "查找到到位置:" << iter - numbers.begin() << endl;
}
else
{
    cout << "没有找到" << endl;
}

search_n

①.头文件

包含在头文件 #include 中

②.算法作用

在指定区间中查找符合规则的且重复指定次数的元素,默认规则是相等匹配。

③.适用容器

适用于所有的顺序容器

④.代码示例

auto cmpFunc = [&](int i,int j) {
    return i == j *2;
};

vector<int> numbers = { 1,4,4,4,8,10 };
//在 numbers 查找连续 2 个是 2 的 2 倍到元素
auto iter = search_n(numbers.begin(), numbers.end(),2,2, cmpFunc);
if (iter != numbers.end())
{
    cout << "查找到到位置:" << iter - numbers.begin() << endl;
}
else
{
    cout << "没有找到" << endl;
}

二分查找元素

binary_search

①.头文件

包含在头文件 #include 中

②.算法作用

采用二分查找算法,判断在指定的区间中是否可以查找一个符合规则的元素,指定到值是比较规则到第一个参数,默认规则是 == 运算符:即第一个等于指定元素值的元素

③.适用容器

适用于已排序的顺序容器

④.代码示例

auto cmpFunc = [&](int i,int j) {
    return i> j ;
};

vector<int> numbers = { 1,2,3,4,6 };
//判断在 numbers 中是否存在一个满足 x > 5 到元素 x
auto isok = binary_search(numbers.begin(), numbers.end(),5,cmpFunc);//
if (!isok )
{
  cout << "查找到元素" << endl;
}
else
{
  cout << "没有找到" << endl;
}

lower_bound

①.头文件

包含在头文件 #include 中

②.算法作用

采用二分查找算法,在指定的区间中查找第一个不符合规则的元素,默认规则是 < 运算符:即第一个不小于指定值的元素。

③.适用容器

适用于已排序的顺序容器

④.代码示例

auto cmpFunc = [&](int i,int j) {
    return i % j == 0;
};

vector<int> numbers = { 2,4,8,10,1,13,15 };
//在 numbers 查找第一个不是 2 的倍数到元素
auto iter = lower_bound(numbers.begin(), numbers.end(),2,cmpFunc);
if (iter != numbers.end())
{
  cout << "查找到到位置:" << iter - numbers.begin()<<",元素值:"<< *iter << endl;
}
else
{
  cout << "没有找到" << endl;
}

upper_bound

①.头文件

包含在头文件 #include 中。

②.算法作用

采用二分查找算法,在指定的区间中查找第一个符合规则的元素,指定到值是比较规则到第一个参数,默认规则是 < 运算符:即第一个大于指定元素值的元素。

③.适用容器

适用于已排序的顺序容器。

④.代码示例

auto cmpFunc = [&](int i,int j) {
return i> j ;
};

vector<int> numbers = { 1,2,3,4,6 };
//在 numbers 查找第一个满足 5 > x 到元素 x
auto iter = upper_bound(numbers.begin(), numbers.end(),5,cmpFunc);//
if (iter != numbers.end())
{
  cout << "查找到到位置:" << iter - numbers.begin()<<",元素值:"<< *iter << endl;
}
else
{
  cout << "没有找到" << endl;
}

equal_range

①.头文件

包含在头文件 #include 中。

②.算法作用

采用二分查找算法,返回两个迭代器:第一个迭代器返回的规则和 lower_bound 一样,第二个迭代器返回的规则和 upper_bound 一样 。

③.适用容器

适用于已排序的顺序容器。

④.代码示例

auto cmpFunc = [&](int i,int j) {
return i<j ;
};

vector<int> numbers = { 1,2,3,4,4,4,4,5,6,7,8,9 };
auto range = equal_range(numbers.begin(), numbers.end(),4);//
if (range.first != numbers.end())
{
  //第一个不满足 i > 4 的位置
  cout << "起始位置:" << range.first - numbers.begin() << " 元素值 "<< *range.first << endl;
  //第一个满足 4 < i 的位置
  cout << "结束位置:" << range.second - numbers.begin() << " 元素值 " << *range.second << endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值