STL——C & C++ 函数 & vector容器 & 优先队列

C++

#include< algorithm>

1. nique()

unique()函数是一个去重函数,STL中unique的函数 unique的功能是去除相邻的重复元素(只保留一个),还有一个容易忽视的特性是它并不真正把重复的元素删除。因为unique去除的是相邻的重复元素,所以一般用之前都会要排一下序

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int a[]= {1,2,2,3,3,4,4,8,8,5};
    int *m=unique(a,a+10);
    int k=m-a;
    for(int i=0; i<k; i++)
    {
        cout<<a[i]<<endl;
    }
    return 0;
}

2. count()

返回某个值出现的次数

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int a[]= {1,2,2,3,3,4,4,8,8,5,2};
    cout<<count(a, a+11, 2)<<endl;
    return 0;
}

3. find()

查找函数

#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    int m[]= {35,5,5,9,3,7,10};
    int n=7;
    if (find(m,m+n,10) == m+n-1)
        cout << "yes" << endl;
    else
        cout << "no" << endl;
    cout<<find(m,m+n,10)-m<<endl;//在数组中第一次出现的位置的数组下标
}

4. reverse(s.begin(),s.end())

反转字符串

#include<iostream>
#include<algorithm>
using namespace std;
int main ()
{
        string s;
        s="123456";
        reverse(s.begin(),s.end());
        cout<<s;
        return 0;
}

6. swap()</

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值