首页 > 其他 > 详细

STL

时间:2019-08-15 16:56:28      阅读:99      评论:0      收藏:0      [点我收藏+]

1、set:

技术分享图片
#include<bits/stdc++.h>
#define pf(a) printf("%d ",a)
#define phn puts("")
#define F(i,a,b) for(rg int i=a;i<=b;++i)
#define rg register
#define il inline
#define LL long long
using namespace std;
int read();
multiset<int>s;
int n,a[100010];
int main(){
    n=read();F(i,1,n)a[i]=read();
    s.clear();
    F(i,1,n)s.insert(a[i]);
    for(multiset<int>::iterator p=s.begin();p!=s.end();++p){
        printf("%d",*p);
    }
    phn;//1235778
    multiset<int>::iterator p;
    if((p=s.find(3))!=s.end()){
        s.erase(p);
    }
    s.erase(7);
    for(multiset<int>::iterator p=s.begin();p!=s.end();++p){
        printf("%d",*p);
    }
    phn;
    //1258
}
il int read(){
    int s=0;char ch;
    while(ch=getchar(),!isdigit(ch));
    for(;isdigit(ch);s=s*10+(ch^48),ch=getchar());
    return s;
}
/*
g++ 4.cpp -g
./a.out
7
5 3 1 8 2 7 7
*/
View Code

iterator:迭代器(类似指针)。STL每个类型都有特定专用迭代器。

set:自动去重。

multiset:不去重,可重复。

2、sort函数参数

(1)start表示要排序数组的起始地址;
(2)end表示数组结束地址的下一位
(3)cmp用于规定排序的方法,可不填,默认升序。
sort(a+1,a+n+1):对1~n排序。
sort(a,a+n):对1~n-1排序。
3、nth_element:
作用:nth_element作用为求第n大的元素,并把它放在第n位置上,下标是从0開始计数的,也就是说求第0小的元素就是最小的数。
仅仅能确定a[k]是数列中第k大的数。
nth_element()函数不过将第n大的数排好了位置,并不返回值。
技术分享图片
#include<iostream>
#include<algorithm>
using namespace std; 
int main()
{
    int a[]={1,3,4,5,2,6,8,7,9};
    int i;
    cout<<"数列例如以下:"<<endl;
    for(i=0;i<9;i++)
       cout<<a[i]<<" ";
    nth_element(a,a+5,a+9);
    cout<<endl<<"输出第五大的数: "<<a[4]<<endl; //注意下标是从0開始计数的 
    return 0;
}
View Code

 

STL

原文:https://www.cnblogs.com/seamtn/p/11358198.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!