STL算法之判断式

转接自STL算法
1. equal(v1.beg,v1.end,v2.beg,Perd) 判断v1区间是否所有元素与v2子区间对应满足谓词Perd
2. is_permutation(v1.beg,v1.end,v2.beg,Perd) 判断v1区间是否任何元素都能在v2子区间找到满足谓词Perd
3. mismatch() 返回两段序列第一次出现对应位置不相等的pair值
4. lexicographical_compare() 判断在字典排序下某序列是否小于另一序列
5. is_sorted() 判断区间元素是否排过序
6. is_sorted_until() 返回区间第一个未遵守排序准则的元素
7. is_partitioned() 判断区间能否通过谓词条件分为两组
8. partition_point() 返回分为两组的开始元素
9. is_heap() 区间元素是否形成heap(堆)
10. 10. is_heap_until() 返回区间内第一个不满足形成堆的元素
11. all_of() 是否区间所有元素满足谓词Pred
12. any_of() 是否至少有一个元素满足谓词Pred
13. none_of() 是否区间无元素满足谓词

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
// 1. equal(v1.beg,v1.end,v2.beg,Perd) 判断v1区间是否所有元素与v2子区间对应满足谓词Perd
// 2. is_permutation(v1.beg,v1.end,v2.beg,Perd) 判断v1区间是否任何元素都能在v2子区间找到满足谓词Perd
// 3. mismatch() 返回两段序列第一次出现对应位置不相等的pair值
// 4. lexicographical_compare() 判断在字典排序下某序列是否小于另一序列
// 5. is_sorted() 判断区间元素是否排过序
// 6. is_sorted_until() 返回区间第一个未遵守排序准则的元素
// 7. is_partitioned() 判断区间能否通过谓词条件分为两组
// 8. partition_point() 返回分为两组的开始元素
// 9. is_heap() 区间元素是否形成heap(堆)
// 10.is_heap_until() 返回区间内第一个不满足形成堆的元素
// 11.all_of() 是否区间所有元素满足谓词Pred
// 12.any_of() 是否至少有一个元素满足谓词Pred
// 13.none_of() 是否区间无元素满足谓词
void test(const vector<int>& a,const vector<int>& b)
{
    //1
    if (equal(a.begin(), a.end(), b.begin()))
        cout << "a of b" << endl;
    else
        cout << "a not of b" << endl;
    //2
    vector<int> v1{ 8,3,2,9,1 };
    vector<int> v2{ 8,2,3,9,1,5};
    if (is_permutation(v1.begin(), v1.end(), v2.begin()))
        cout << "v1 of v2 by unsorted " << endl;
    else
        cout << "v1 not of v2 by unsorted" << endl;
    //3
    auto pair = mismatch(v1.begin(),v1.end(), v2.begin());
    if (pair.first != v1.end())
    {
        cout << "the first v1's member not equal to v2's member of v1 is " << *pair.first
             << ends << "of v2 is " << *pair.second << endl;
    }
    //4
    string s1 = "marco";
    string s2 = "working";
    if (lexicographical_compare(s1.begin(), s1.end(), s2.begin(), s2.end()))
        cout << "s1 < s2 by dictionary-sort" << endl;
    //5
    vector<int> v3{ 1,2,4 };
    //默认谓词是less<>()
    if (is_sorted(v3.begin(), v3.end(),less<int>()))
        cout << "v3 had sorted" << endl;
    //6
    vector<int> v4{ 9,8,6,5,4};
    auto fpos = is_sorted_until(v4.begin(), v4.end(), greater<int>());
    if (fpos != v4.end())
    {
        cout << "the first not sorted is " << *fpos << endl;
    }
    auto f = [](const int& value)
    {
        return value > 6;
    };
    //7
    if (is_partitioned(v4.begin(), v4.end(), f))
        cout << "v4 has can divide by value > 6" << endl;
    //8
    auto partpos = partition_point(v4.begin(), v4.end(), f);
    if (partpos != v4.end())
        cout << "the start divide pos is " << *partpos << endl;
    //9
    vector<int> v5{ 9,1,4,1,8 };
    if (is_heap(v5.begin(), v5.end()))
        cout << "v5 can develop to heap" << endl;
    //10
    auto heapos = is_heap_until(v5.begin(), v5.end());
    if (heapos != v5.end())
        cout << "since " << *heapos << " it can't develop to heap" << endl;
    vector<int> v6{ 1,2,3,4,5,6,7 };
    auto f1 = [](const int& value)
    {
        return value > 5 || value == 5;
    };
    //11 12 13
    if (!all_of(v6.begin(), v6.end(), f1))
        cout << "not all >= 5" << endl;
    if (any_of(v6.begin(), v6.end(), f1))
        cout << "at least has a >=5" << endl;
    if (none_of(v6.begin(), v6.end(), f1))
        cout << "none >= 5" << endl;
}
int main()
{
    vector<int> vec0{ 1,2,3,4,5,6,7 };
    vector<int> vec1{ 1,2,3,4,5,6,7,8 };
    test(vec0, vec1);
    system("pause");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值