cb51a_c++_STL_算法_根据第n个元素排序nth_element

cb51a_c++_STL_算法_根据第n个元素排序nth_element
nth_element(b,n,e),比如最大的5个数排序,或者最小的几个数
nth_element(b,n,e,p)
对比:partition()算法,分区算法


error C2675: 一元“++”:“TT88”不定义该运算符或到预定义运算符可接收类型的转换
for (TT88::iterator iter = ideq.begin(); iter != ideq.end(); ++ideq),写错了,应该是++iter

复制代码
/*cb51a_c++_STL_算法_根据第n个元素排序nth_element
nth_element(b,n,e),比如最大的5个数排序,或者最小的几个数
nth_element(b,n,e,p)
对比:partition()算法,分区算法


error C2675: 一元“++”:“TT88”不定义该运算符或到预定义运算符可接收类型的转换
for (TT88::iterator iter = ideq.begin(); iter != ideq.end(); ++ideq),写错了,应该是++iter
*/

#include <iostream>
#include <algorithm>
#include <deque>
#include <iterator>
#include <functional>

using namespace std;

template <class TT88>
void print88(TT88 &ideq)
{
    for (TT88::iterator iter = ideq.begin(); iter != ideq.end(); ++iter)
        cout << *iter << ' ';
    cout << endl;
}

int main()
{
    deque<int> ideq,ideq2;
    for (int i = 3; i <= 7; ++i)
        ideq.push_back(i);
    for (int i = 2; i <= 6; ++i)
        ideq.push_back(i);
    for (int i = 1; i <= 5; ++i)
        ideq.push_back(i);
    ideq2 = ideq;

    print88(ideq);
    nth_element(ideq.begin(), ideq.begin() + 3, ideq.end());//默认less<int>(),升序
    cout << "nth_element排序后:" << endl;
    print88(ideq);

    cout << "用copy,到输出流迭代器显示:" << endl;
    copy(ideq.begin(), ideq.begin() + 4, ostream_iterator<int>(cout, " "));
    cout << endl;

    ideq = ideq2;
    nth_element(ideq.begin(), ideq.begin() + 1, ideq.end(),greater<int>());//降序,大到小
    cout << "降序:" << endl;
    print88(ideq);

    deque<int>::iterator pos;
    //pos = partition(ideq.begin(), ideq.end(), bind2nd(greater<int>(), 3));
    cout << endl;
    ideq = ideq2;
    cout << "分区partition小于等于3放左边,其余放右边" << endl;
    pos = partition(ideq.begin(), ideq.end(), bind2nd(less_equal<int>(), 3));
    print88(ideq);
    copy(ideq.begin(), pos, ostream_iterator<int>(cout, " "));
    return 0;      
}
复制代码

 

posted @   txwtech  阅读(267)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示