STL_算法_查找算法(search_n)

C++ Primer 学习中。。。

 

简单记录下我的学习过程 (代码为主)

 

search_n    //查找连续的n个满足条件的。。

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<deque>
#include<vector>
#include<algorithm>
using namespace std;
/*************************************************************************************
std::search_n           所有容器适用                                       algorithm
--------------------------------------------------------------------------------------
template <class ForwardIterator, class Size, class T>
ForwardIterator search_n ( ForwardIterator first, ForwardIterator last,
                           Size count, const T& value );

template <class ForwardIterator, class Size, class T, class BinaryPredicate>
ForwardIterator search_n ( ForwardIterator first, ForwardIterator last,
                           Size count, const T& value, BinaryPredicate pred );


eg:
template<class ForwardIterator, class Size, class T>
ForwardIterator search_n ( ForwardIterator first, ForwardIterator last,
                           Size count, const T& value )
{
    ForwardIterator it, limit;
    Size i;

    limit=first;
    advance(limit,distance(first,last)-count);

    while (first!=limit)
    {
        it = first;
        i=0;
        while (*it==value)       // or: while (pred(*it,value)) for the pred version
        {
            ++it;
            if (++i==count) return first;
        }
        ++first;
    }
    return last;
}
**************************************************************************************/


bool mypredicate (int i, int j)
{
    return (i==j);
}

int main ()
{
    int myints[]= {10,20,30,30,20,10,10,10,20};
    vector<int> myvector (myints,myints+9);

    vector<int>::iterator it;

    // using default comparison:
    /*** search_n(s,e,cnt,num);找区间内[s,e)连续cnt个的num 返回的是找到的第一个num的迭代器***/
    it = search_n (myvector.begin(), myvector.end(), 2, 30);

    if (it!=myvector.end())
        cout << "two 30s found at position " << int(it-myvector.begin()) << endl;
    else
        cout << "match not found" << endl;

    // using predicate comparison:
    /*** search_n(s,e,cnt,num,cmp);找区间内[s,e)连续cnt个的num符合cmp()条件的,返回的是找到的第一个num的迭代器***/
    it = search_n (myvector.begin(), myvector.end(), 3, 10, mypredicate);

    if (it!=myvector.end())
        cout << "three 10s found at position " << int(it-myvector.begin()) << endl;
    else
        cout << "match not found" << endl;

    //连续的4个大于10的数
    it = search_n (myvector.begin(), myvector.end(), 4, 10, greater<int>());//谓词//其实本函数不规范。
//    it = search_n_if (myvector.begin(), myvector.end(), 4, bind2nd(greater<int>(),10) );//很遗憾,并没有search_n_if
    if (it!=myvector.end())
        cout << "连续的4个大于10的数 " << int(it-myvector.begin()) << endl;
    else
        cout << "match not found" << endl;

    return 0;
}
/******
Output:
    two 30s found at position 2
    three 10s found at position 5
    连续的4个大于10的数 1
******/

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Java中,STL(Standard Template Library)算法被称为Java集合框架(Java Collections Framework)。它提供了一组用于操作和管理数据的类和接口。Java集合框架包括以下主要接口和类: 1. Collection接口:是所有集合类的根接口,定义了集合类的基本操作,如添加、删除、遍历等。常见的实现类有List、Set和Queue。 2. List接口:继承自Collection接口,表示有序的集合,允许重复元素。常见的实现类有ArrayList和LinkedList。 3. Set接口:继承自Collection接口,表示无序的集合,不允许重复元素。常见的实现类有HashSet和TreeSet。 4. Queue接口:继承自Collection接口,表示队列,按照先进先出(FIFO)的顺序处理元素。常见的实现类有LinkedList和PriorityQueue。 除了上述基本接口和类外,Java集合框架还提供了一些其他的类和接口,用于特定的需求和场景,例如Map接口用于键值对的存储和操作,以及各种实现了Map接口的类如HashMap和TreeMap。 Java集合框架提供了丰富的算法和方法来操作集合数据,例如排序、查找、过滤等。常见的算法包括: 1. 排序算法:Java集合框架提供了Collections类中的sort()方法,可以对List集合进行排序。常见的排序算法有冒泡排序、插入排序和快速排序等。 2. 查找算法:Java集合框架提供了Collections类中的binarySearch()方法,可以在有序的List集合中进行二分查找。 3. 过滤算法:Java集合框架提供了Stream API,可以使用filter()方法对集合进行过滤,根据指定的条件筛选出符合要求的元素。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值