首页 > 编程语言 >C++ STL(algorithm)

C++ STL(algorithm)

时间:2023-06-18 13:04:39浏览次数:55  
标签:ForwardIterator stu1 algorithm val STL bound C++ int cout

1 字符和整数排序

#include <iostream>
#include <algorithm>
using namespace std;
void stl1()
{
    int a[]={-1,9,-34,100,45,2,98,32};
    int len=sizeof(a)/sizeof(int);
    sort(a,a+len);//由小到大排列
    sort(a,a+len,greater<int>());    //由大到小排列 
}
void stl2()
{
    char a[]="zasdasdkjhaskjnasdjh";
    int len=sizeof(a)/sizeof(char);
    sort(a,a+len,greater<char>());    //由大到小排列     
    sort(a,a+len);    
}

2 结构体排序

//结构体排序 
struct Node
{
    int x,y;
}p[1001];
int cmp(Node a,Node b)
{
    if (a.x!=b.x)
    {
        return a.x<b.x;    
    }    
    return a.y<b.y;
} 
void stl3()
{
    int n;
    n=3;
    for (int i=0;i<n;i++)
    {
        cin>>p[i].x>>p[i].y;
    } 
    sort(p,p+n,cmp);
    //cout<<"OK";    
    for (int i=0;i<n;i++)
    {
        cout<<p[i].x<<" "<<p[i].y<<endl;
    }
}

3 学生成绩求和,排序

#include <iostream>
#include <algorithm>
using namespace std;
struct studen1
{
    char ID[10];
    char name[20];
    int maths1;
    int chinese1;
    int english1;
    int count1;    
};
int cmpstu(studen1 a11,studen1 b11)
{
    return a11.count1<b11.count1;
}
void fun123()
{
    struct studen1 stu1[3];
    /*
    struct studen1 stu1[3]={{"1001","jack",90,80,70},
        {"1002","tom",100,88,71},
        {"1003","rose",88,66,0}
    };
    */
    freopen("studen1.in","r",stdin);
    freopen("studen1.out","w",stdout);
    for (int i=0;i<3;i++)
    {    
        cin>>stu1[i].ID>>stu1[i].name>>stu1[i].maths1>>stu1[i].chinese1>>stu1[i].english1;
        stu1[i].count1=stu1[i].chinese1+stu1[i].english1+stu1[i].maths1; 
    }
    sort(stu1,stu1+3,cmpstu);//由小到大排序 
    for (int i=0;i<3;i++)
    {
        cout<<stu1[i].ID<<" "<<stu1[i].name<<" "<<stu1[i].count1<<endl;     
    }
    fclose(stdout);
    fclose(stdin);    
}

4  单词排序

C++ STL(algorithm)_#include

 

void stl4()
{
    //单词排序
    freopen("WordSort.in","r",stdin);
    string words[100];
    int n,num=0;
    cin>>n;    
    for (int i=0;i<n;i++)
    {
        cin>>words[num];           
        bool has=false;
        for (int i=0;i<num;i++)
        {
            /*            
            str1.compare(str2);
            compare为比较字符串 如果相等则输出为0,不等则输出为-1。
            */
            if(words[i].compare(words[num])==0) 
            {
                has=true;
                break;
            }
        }
        if (!has)
        {
            num++;
        }
       
    }     
    sort(words,words+num);
    for (int i=0; i<num; i++)
    {
        cout << words[i] << endl;
    }    
    fclose(stdin);
}
void stl4_1()
{
    //单词排序
    freopen("WordSort.in","r",stdin);
    int n;
    string a[100];
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    sort(a,a+n);
    for(int i=0;i<n;i++)
    {
        if(a[i]==a[i+1])
        {
            continue;
        }
        cout<<a[i]<<endl;
    }
}

5 志愿者选拔

C++ STL(algorithm)_#include_02

 

struct data
{
  int num,sc;
} a[5005];
bool cmp1(data a,data b)
{
  if((a.sc>b.sc)||(a.sc==b.sc&&a.num<b.num))
    return 1;
  else return 0;
}
void stl5()
{
    //志愿者选拔 
    int n,m,r=0;
    freopen("voluntary4.in","r",stdin);
    cin>>n>>m;
    for(int i=0; i<n; i++)
    {
        cin>>a[i].num>>a[i].sc;
    }
    sort(a,a+n,cmp1);
    int s=a[int(m*1.5)].sc;
    for(int i=0; i<n; i++)
    {
        if(a[i].sc>=s)
        {
            r++;
        }    
    }
    cout<<s<<' '<<r<<endl;
    for(int i=0; i<r; i++)
    {
        cout<<a[i].num<<' '<<a[i].sc<<endl;
    }    
    fclose(stdin);    
}

6 奖学金

C++ STL(algorithm)_#include_03

 

#define N 300
struct node2
{
  int num;
  int Ch;
  int Ma;
  int En;
  int sum;
} a1[N];
int cmp2(node2 a,node2 b)
{
  if(a.sum==b.sum)
  {
    if(a.Ch==b.Ch)
      return a.num<b.num;
    return a.Ch>b.Ch;
  }
  return a.sum>b.sum;
}
void stl6()
{
    //奖学金
    freopen("scholar.in","r",stdin);
    int n;
    cin>>n;
    for(int i=0; i<n; i++)
    {
        a1[i].num=i;
        cin>>a1[i].Ch>>a1[i].Ma>>a1[i].En;
        a1[i].sum=a1[i].Ch+a1[i].Ma+a1[i].En;
    }        
    sort(a1,a1+n,cmp2);
    for(int i=0; i<5; i++)
    {
         cout<<a1[i].num<<" ";
         cout<<a1[i].sum<<endl;
    }
    
    fclose(stdin);
}

 lower_bound 和 upper_bound

#include <iostream>
#include <algorithm>
using namespace std;
/*
//在 [first, last) 区域内查找不小于 val 的元素
ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last,
                             const T& val);
//在 [first, last) 区域内查找第一个不符合 comp 规则的元素
ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last,
                             const T& val, Compare comp);
//查找[first, last)区域中第一个大于 val 的元素。
ForwardIterator upper_bound (ForwardIterator first, ForwardIterator last,
                             const T& val);
//查找[first, last)区域中第一个不符合 comp 规则的元素
ForwardIterator upper_bound (ForwardIterator first, ForwardIterator last,
                             const T& val, Compare comp);
*/
void stl11()
{
    //升序数组   
    int a[]={1,1,2,2,3,3,3,4,4,4};
    //lower_bound()用于寻找第一个大于等于val的位置
    int i=lower_bound(a,a+10,0)-a;//0
    cout<<i<<endl;
    i=lower_bound(a,a+10,1)-a;//0
    cout<<i<<endl;
    i=lower_bound(a,a+10,3)-a;//4
    cout<<i<<endl;
    i=lower_bound(a,a+10,5)-a;//10
    cout<<i<<endl;
    //upper_bound()用于寻找第一个大于val的位置
    int j=upper_bound(a,a+10,0)-a;//0
    cout<<j<<endl;
    j=upper_bound(a,a+10,1)-a;//2
    cout<<j<<endl;
    j=upper_bound(a,a+10,3)-a;//7
    cout<<j<<endl;
    j=upper_bound(a,a+10,4)-a;//10
    cout<<j<<endl;
    j=upper_bound(a,a+10,5)-a;//10
    cout<<j<<endl;
} 
void stl12()
{
    //降序数组使用 lower_bound upper_bound
    //lower_bound(first,last,val,greater<int>()),
    //若val在序列中,则返回val第一次出现的位置,
    //否则返回第一个插入val,不影响原序列顺序的位置。 
    int a[]={4,4,3,3,2,2,1,1};
    int i=lower_bound(a,a+8,0,greater<int>())-a; //8 
    cout<<i<<endl;
    i=lower_bound(a,a+8,4,greater<int>())-a;//0
    cout<<i<<endl;
    i=lower_bound(a,a+8,1,greater<int>())-a;//6
    cout<<i<<endl;
    i=lower_bound(a,a+8,3,greater<int>())-a;//2
    cout<<i<<endl;
    i=lower_bound(a,a+8,5,greater<int>())-a;//0
    cout<<i<<endl;
    //upper_bound(first,last,val,greater<int>())
    //val在序列中,返回第一个小于val的位置,
    //否则 返回第一个插入val不影响原序列顺序的位置 
    int j=upper_bound(a,a+8,0,greater<int>())-a;//8
    cout<<j<<endl;
    j=upper_bound(a,a+8,4,greater<int>())-a;//2
    cout<<j<<endl;
    j=upper_bound(a,a+8,1,greater<int>())-a;//8
    cout<<j<<endl;
    j=upper_bound(a,a+8,3,greater<int>())-a;//4
    cout<<j<<endl;    
    j=upper_bound(a,a+8,5,greater<int>())-a;//0
    cout<<j<<endl;    
    
}

 



标签:ForwardIterator,stu1,algorithm,val,STL,bound,C++,int,cout
From: https://blog.51cto.com/u_1017534/6508205

相关文章

  • C++ STL(algorithm)
    1字符和整数排序#include<iostream>#include<algorithm>usingnamespacestd;voidstl1(){inta[]={-1,9,-34,100,45,2,98,32};intlen=sizeof(a)/sizeof(int);sort(a,a+len);//由小到大排列sort(a,a+len,greater<int>());//由大到小排列}vo......
  • [pybind11]为c++项目写python API接口
    C++项目的pybind方法有哪些?有什么区别?以下是主要的python绑定cpp的方法:方法年份代表用户适用于CPython的C/C++扩展模块1991标准库PyBind11(推荐用于C++)2015Cython(推荐用于C)2007gevent、kivyHPy2019mypyc2017ctype2003oscryptocffi......
  • 关于如何使用C++进行编程(不使用数据库的情况下)
    问题描述对于一个长期使用Java连接数据库,实现javaweb编程的软工友友来说,突然在编程任务中不允许连接数据库,就有一点的蒙圈,没有办法,只能去查阅资料啦!问题解决不出意外的话,这次我们就需要使用文件操作来存储数据啦!(然后另外一种方法时json,显然,文件操作更加简单一点、也是更加熟悉......
  • C++异常处理
    需要异常处理的情况程序运行时常会碰到一些异常情况,例如:做除法的时候除数为0;用户输入年龄时输入了一个负数;用new运算符动态分配空间时,空间不够导致无法分配;访问数组元素时,下标越界;打开文件读取时,文件不存在。这些异常情况,如果不能发现并加以处理,很可能会导致程序崩溃。......
  • 【C++】Effective Modern C++ Key Notes
    [errataveryimportant](https://www.aristeia.com/BookErrata/emc++-errata.html)>Argument,ActualArgument>Parameter,FormalParameter##一类型推导C++98有一套类型推导的规则:用于函数模板的规则。C++11修改了其中的一些规则并增加了两套规则,一套用于auto,一套用于dec......
  • 现代C++学习指南-方向篇
    C++是一门有着四十年历史的语言,先后经历过四次版本大升级(诞生、98、11、17(20),14算小升级)。每次升级都是很多问题和解决方案的取舍。了解这些历史,能更好地帮助我们理清语言的发展脉络。所以接下来我将借它的发展历程,谈一谈我对它的理解,最后给出我认为比较合理的学习路线指南。C++0......
  • 设计 C++ 接口文件的小技巧之 PIMPL
    C++里面有一些惯用法(idioms),如RAII,PIMPL,copy-swap、CRTP、SFINAE等。今天要说的是PIMPL,即PointerToImplementation,指向实现的指针。问题描述在实际的项目中,经常需要定义和第三方/供应商的C++接口。假如有这样一个接口文件:MyInterface.h#include<string>#include<li......
  • 用于提速的一些C++ 编译器的编译选项
    C++Compilerflags在TIO中怎么用?在C++Compilerflags新建几行:-Ofast:这个编译器优化选项启用所有-O3级别的优化,并进一步启用一些可能会破坏标准精度的优化,如忽视IEEE或ISO规定的某些数学准则的优化。这可能会使得程序运行得更快,但也可能会降低精度,因此只有在你可以接......
  • C++面试八股文:了解位运算吗?
    C++面试八股文:了解位运算吗?某日二师兄参加XXX科技公司的C++工程师开发岗位第12面:面试官:了解位运算吗?二师兄:了解一些。(我很熟悉)面试官:请列举以下有哪些位运算?二师兄:按位与(&)、按位或(|)、按位异或(^),按位取反(~)、左移(<<)和右移(>>)。面试官:好的。那你知道位运算有什么优势吗?......
  • 《C++》继承
    继承classA:publicB子类:继承方式父类classPhone{public: Phone() { frame="框架"; screen="屏幕"; battery="电池"; }public: stringframe; stringscreen; stringbattery;};classBrand:publicPhone{public: Brand(strin......