(C++) 常用STL容器查找函数find()使用小结

关于查找类函数

顺序查找类

find(first, last, type) //找值

find_if(first, last, function) //找符合function()

下面两个可以理解是字符串匹配

find_end(first1, last1, first2, last2) //在1中找2最后出现的位置

find_first_of(first1, last1, first2, last2) //在1中找2首次出现的位置

二分查找类

要求容器是顺序存储
mapset 本身就是顺序的
vectorstring 需要提前排序

lower_bound 第一个 num 的数值 返回迭代器

upper_bound 第一个 num > 的数值 返回迭代器

binary_search 单纯的二分查找 返回bool

常用容器find()使用举例

vector

普通数组的用法相同

void findVector()
{
    //本身并没有find
    //通过#include<algorithm>实现
    vector<int>arr = {1,68,515,48,5,5,8,4,48,1,2};
    auto it = find(arr.begin(), arr.end(), 5);
    if (it != arr.end())
        cout << *it << endl;
    else
        cout << "没找到" << endl;

    //普通数组的用法相同
    int arr2[] = {1,68,515,48,5,5,8,4,48,1,2};
    int n = sizeof(arr2)/sizeof(arr2[0]);
    auto it2 = find(arr2, arr2+n, 100);
    if (it2 != arr2+n)
        cout << *it2 << endl;
    else
        cout << "没找到" << endl;

    return ;
}

本代码输出:
=我是分割线=
5
没找到
=我是分割线=

string

void findString()
{
    string s = "Hello World!";
    //find的返回值是下标,找不到返回-1
    int pos;

    //默认从头开始找
    pos = s.find('l');
    //输出:2  即第一个'l'是在下标2的位置
    if (pos != -1)
        cout << pos << endl;
    else
        cout << "没找到" << endl;

    //从下标5的位置开始找,包含下标5
    pos = s.find('l', 5);
    //输出:9
    if (pos != -1)
        cout << pos << endl;
    else
        cout << "没找到" << endl;

    //寻找字串也是同样的用法
    pos = s.find("ll");
    //pos = s.find("ll", 3);
    if (pos != -1)
        cout << pos << endl;
    else
        cout << "没找到" << endl;

    return ;
}

本代码输出:
=我是分割线=
2
9
2
=我是分割线=

未找到可以写成 if (pos != -1) 或者if (pos != s.npos)

这里的s.npos表示的是直到字符串结束,是size_t 型的最大值,定义时为static const size_type npos = -1;

此处的原理我们并不需要太细究,只要会使用就行了

参考资料:string::npos - C++ Reference (cplusplus.com)

stack和queue和priority_queue

没有迭代器

deque

void findDeque()
{
    vector<int>arr = {1,68,515,48,5,5,8,4,48,1,2};
    deque<int>dq(arr.begin(), arr.end());

    auto it = find(dq.begin(), dq.end(), 66);
    if (it != dq.end())
        cout << *it << endl;
    else
        cout << "没找到" << endl;

    return ;
}

本代码输出:
=我是分割线=
没找到
=我是分割线=

list

void findList()
{
    vector<int>arr = {1,68,515,48,5,5,8,4,48,1,2};
    list<int>lst(arr.begin(), arr.end());

    auto it = find(lst.begin(), lst.end(), 66);
    if (it != lst.end())
        cout << *it << endl;
    else
        cout << "没找到" << endl;

    return ;
}

本代码输出:
=我是分割线=
没找到
=我是分割线=

set

void findSet()
{

    vector<int>arr = {1,68,515,48,5,5,8,4,48,1,2};
//   unordered_set
//   multiset
//   unordered_multiset
    set<int>st(arr.begin(), arr.end());

    auto it = st.find(8);
    if (it != st.end())
        cout << *it << endl;
    else
        cout << "没找到" << endl;

    return ;
}

本代码输出:
=我是分割线=
8
=我是分割线=

map

void findMap()
{
    vector<int>arr = {1,68,515,48,5,5,8,4,48,1,2};
//   unordered_map
//   multimap
//   unordered_multimap
    map<int, int>mp;
    for (int i = 0; i < arr.size(); i++){
        pair<int, int>pir(arr[i], i);
        mp.insert(pir);
    }

    auto it = mp.find(8);
    if (it != mp.end())
        printf("mp[%d] = %d\n", it->first, it->second);
    else
        puts("没找到");

    return ;
}

本代码输出:
=我是分割线=
mp[8] = 6
=我是分割线=

关于map中通过value找可以可以使用find_if()来实现,具体代码请看下面链接
C++ map 根据value找key

  • 12
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值