C++STL

pair的用法:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <iostream>
#include <sstream>
#include <vector>
#define A first
#define B second
#define mp make_pair
using namespace std;
typedef pair<int ,pair<int, string> >  sta;
sta mksta(int a, int b, string c)
{
    return mp(a, mp(b, c));
}

我会先声明这么一些个东西;

然后就是pair了,这个容器很常用,他有一些特点,因此就会有一些妙用!

比如对于

typedef pair<int ,pair<int, string> >  sta;
这个容器,里面有三个元素,我向下面的vector里面加入一些sta,然后排序;

vector <sta> v;
//v.push_back();
sort(v.begin(),v.end(),cmp);
默认的顺序是第一关键字从小到大v[i].A,然后第二关键字从小到大A[i].B.A,然后第三关键字从小到大v[i].B.B。。

然后自己去想应用吧。。


unique的用法:

string str;  
    vector<string> words;  
    while(cin>>str){  
        words.push_back(str);  
    }  
    sort(words.begin(),words.end());  
    vector<string>::iterator end_unique =  
                            unique(words.begin(),words.end());  
    words.erase(end_unique,words.end()); 

先排序,才能用unique,

unique将相邻的重复的元素移到最后,返回一个iterator指向最后的重复元素

再用erase删除就达到了去除重复的目的


注:算法不直接修改容器的大小。如果需要添加或删除元素,则必须使用容器操作。


equal_range的用法:


找出一个区间 [i,j) 内的每个元素都等同于value(注意是前闭后开)
算法equal_range则是以pair的形式将两者都返回

pair<vector<int>::iterator,vector<int>::iterator> bounds;
bounds=equal_range(v.begin(),v.end(),a[i]);
j = bounds.second
i = bounds.first


在结构体里面声明比较函数:

struct node
{
    int v;
    int id;
    bool operator<(const node &a)const
    {
        return v<a.v;
    }

}book[4000];


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值