谈STL的重要应用与实现

1. priority_queue

无理由在广搜和解决贪心问题用处很大,首先作为广搜的挑选最优状态如Astar,然后是当做堆给最短路和最小生成树做优化,用的是贪心思想。

定义优先级,和正常的反过来。
bool operator < (const number1 &a)const {
  return x > a.x; // 从小到大 ,x 小的 优先级别高
}

优先队列的实现就是一个堆,队尾进入一个新元素就和父节点比较上升,出对就调整堆。

上一道纯贪心和优先队列完美结合的例题 HDU 4544

#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;

#define MAXN 100006
#define LL long long
int Blood[MAXN];
bool cmp(int a, int b) {
    return a > b;
}
struct Node {
    int D, cost;
    bool operator <(const Node &tmp) const {
        return cost > tmp.cost;
    }
} arrow[MAXN];
int N, M;
priority_queue<Node> Q;
bool cmp_Node(Node a, Node b) {
    return a.D > b.D;
}
LL solve() {
    LL Min_cost = 0;
    int pos = 0;
    for (int i = 0; i < N; i++) {
        while (pos < M && Blood[i] <= arrow[pos].D)
            Q.push(arrow[pos++]);
        if (Q.empty()) {
            return -1;
        }
        Min_cost += Q.top().cost;
        Q.pop();
    }
    return Min_cost;
}
int main() {
//    freopen("data_tmp.txt", "r", stdin);
    int i;
    while (~scanf("%d%d", &N, &M)) {
        while (!Q.empty())
            Q.pop();
        for (i = 0; i < N; i++)
            scanf("%d", &Blood[i]);
        for (i = 0; i < M; i++)
            scanf("%d", &arrow[i].D);
        for (i = 0; i < M; i++)
            scanf("%d", &arrow[i].cost);
        sort(Blood, Blood + N, cmp);
        sort(arrow, arrow + M, cmp_Node);
        if (arrow[0].D < Blood[0]) {
            puts("No");
            continue;
        }
        LL ans = solve();
        if (ans == -1)
            puts("No");
        else
            printf("%I64d
", ans);
    }
    return 0;
}

2. set 与 hash_set

应用,首先是集合操作,交并补对称差,和查找.count() ,指向键值>=k的第一个元素.lower_bound() ,指向键值>k的第一个元素.upper_bound(),it-- 就得到<=val 的最大值。

其次是对于动态插入删除有序的题,或者维护状态(最远曼哈顿距离),可以用set快速找到应该插入的位置,如insert和lower_bound 都会返回位置的迭代器。

上一道很有代表性的set应用 (hash_set用法和set一致,很多情况下hash_set效率更高,且hash表空间分配上使用了一个近似于倍增的素数表,最开始取第一个素数,当空间不足时就使用下一个素数)

内部实现为一颗红黑树。

//给出一些能引起周围一定区域内炸弹爆炸的炸弹,按给出顺序引爆
//输出每次爆炸会引起几个炸弹的爆炸
//unique()去重操作,返回去重后尾地址
//lower_bound(first,last,val)求first到last区间内大于等于val第一个值

#include<queue>
#include<set>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
const int maxn = 100015;
int myhash[maxn];
struct node {
    int x, y, d;
} mine[maxn];
struct node2 {
    int y, id;
    node2(int a, int b) {
        y = a;
        id = b;
    }
    bool operator<(const node2 &tmp) const {
        return y < tmp.y;
    }
};
multiset<node2> p[maxn];
multiset<node2>::iterator LL, RR, it;
queue<int> q;
bool vis[maxn];

int main() {
    int n;
    while (scanf("%d", &n) != EOF, n) {
        for (int i = 0; i < n; i++) {
            scanf("%d%d%d", &mine[i].x, &mine[i].y, &mine[i].d);
            myhash[i] = mine[i].x; //在 x 方向离散化
        }
        sort(myhash, myhash + n);
        int cnt = unique(myhash, myhash + n) - myhash;
        for (int i = 0; i < cnt; i++)
            p[i].clear();
        memset(vis, 0, sizeof(vis));
        for (int i = 0; i < n; i++) {
            int pos = lower_bound(myhash, myhash + cnt, mine[i].x) - myhash;
            p[pos].insert(node2(mine[i].y, i));  // i 是原序号
        }
        int id;
        scanf("%d", &id);
        while (!q.empty())
            q.pop();
        int ans = 0;
        q.push(id);
        vis[id] = 1;
        while (!q.empty()) {
            int now_id = q.front(); //点燃第一个
            q.pop();
            ans++;
            int first_pos = lower_bound(myhash, myhash + cnt,
                    mine[now_id].x - mine[now_id].d) - myhash;
            int last_pos = upper_bound(myhash, myhash + cnt,
                    mine[now_id].x + mine[now_id].d) - myhash;
            //首先由横坐标确定大致的 x 寻找范围
            for (int pos = first_pos; pos < last_pos; pos++) {
                int dy = mine[now_id].d - abs(mine[now_id].x - myhash[pos]);
                //lower_bound 与upper_bound配合确定上下界
                LL = p[pos].lower_bound(node2(mine[now_id].y - dy, 0));
                RR = p[pos].upper_bound(node2(mine[now_id].y + dy, 0));
                for (it = LL; it != RR; it++) {
                    if (vis[it->id] == 0) {
                        vis[it->id] = 1;
                        q.push(it->id);
                    }
                }
                p[pos].erase(LL, RR); //删除已经爆炸的点 区间删除
            }
        }
        printf("%d
", ans);
    }
    return 0;
}

3. map

首先是映射,如规定10000000000对应1,而不能开那么大的bool数组吧,那就用map就可以。

其次它有自排序功能,所以对于处理字典序很有用。

map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的。

//给出一系列要忽略的单词,这些单词以外的单词都看作关键字。给出一些标题,按这些关键字的字典序给标题排序
#include <iostream>
#include <cstdio>
#include <map>
#include <set>
#include <cctype>
#include <algorithm>
using namespace std;
const int maxn = 220;

int main() {
    string k, st;
    int a = 0;
    set<string> o;
    multimap<string, string> r;
    while (cin >> k && k != "::")
        o.insert(k);
    getchar();
    while (getline (cin, st)) {
        for (int i = 0; i < st.size(); i++)
            st[i] = tolower(st[i]);
        for (int i = 0; i < st.size(); i++) {
            if (!isalpha(st[i])) continue;
            string t;
            int rec = i;
            while (i < st.size() && isalpha(st[i]))
                t += st[i++];
            if (!o.count(t)) {
                for (int j = 0; j < t.size(); j++)
                    t[j] = toupper(t[j]);
                string temp = st;
                temp.replace(rec, t.size(), t);
                r.insert(make_pair(t, temp));
            }
        }
    }
    for (multimap<string, string>::iterator i = r.begin(); i != r.end(); i++)
        cout << i -> second << endl;
    return 0;
}

4.heap

头文件 #include <algorithm>

建立堆 make_heap(_First, _Last, _Comp)

默认是建立最大堆的。对int类型,可以在第三个参数传入greater<int>()得到最小堆。

在堆中添加数据 push_heap (_First, _Last) 要先在最后加入数据,再调用push_heap()在原堆上调整

在堆中删除数据

pop_heap(_First, _Last) 要先调用pop_heap()清除第一个并调整成堆,再删除数据

堆排序 sort_heap(_First, _Last) 稳定排序

#include <algorithm>
using namespace std;

int num[20];
void debug() {
    for (int i = 0; i < 11; i++)
        printf("%d ", num[i]);
    puts("");
}
int main() {
    memset(num, -1, sizeof(num));
    for (int i = 0; i < 10; i++)
        num[i] = rand() % 100;
    debug();
    make_heap(num, num + 10);
    debug();
    num[10] = 100;
    push_heap(num, num + 11);
    debug();
    pop_heap(num, num + 11);
    num[10] = -1;
    debug();
    return 0;
}

执行结果

原数组 41 67 34 0 69 24 78 58 62 64 -1
建堆后 78 69 41 62 67 24 34 58 0 64 -1
添加100调整后 100 78 41 62 69 24 34 58 0 64 67
清除堆顶调整后 78 69 41 62 67 24 34 58 0 64 -1

POJ 2051

//============================================================================
// Name        : Test.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <algorithm>
using namespace std;

struct Node {
    int id, p, time;
    bool operator <(const Node &tmp) const {
        return (time == tmp.time) ? id > tmp.id : time > tmp.time;
    }
} node[4000];

int main() {
    char opr[10];
    int cnt = 0;
    while (scanf("%s", opr) && opr[0] != '#') {
        scanf("%d%d", &node[cnt].id, &node[cnt].p);
        node[cnt].time = node[cnt].p;
        cnt++;
    }
    make_heap(node, node + cnt);
    int k;
    scanf("%d", &k);
    while (k--) {
        printf("%d
", node[0].id);
        pop_heap(node, node + cnt);
        node[cnt - 1].time += node[cnt - 1].p;
        push_heap(node, node + cnt);
    }
    return 0;
}

5.string

有关函数

string类的构造函数:
string(const char *s);    //用c字符串s初始化
string(int n,char c);     //用n个字符c初始化
此外,string类还支持默认构造函数和复制构造函数,如string s1;string s2="hello";都是正确的写法。当构造的string太长而无法表达时会抛出length_error异常

string类的字符操作:
const char &operator[](int n)const;
const char &at(int n)const;
char &operator[](int n);
char &at(int n);
operator[]和at()均返回当前字符串中第n个字符的位置,但at函数提供范围检查,当越界时会抛出out_of_range异常,下标运算符[]不提供检查访问。
const char *data()const;//返回一个非null终止的c字符数组
const char *c_str()const;//返回一个以null终止的c字符串
int copy(char *s, int n, int pos = 0) const;//把当前串中以pos开始的n个字符拷贝到以s为起始位置的字符数组中,返回实际拷贝的数目

string的特性描述:
int capacity()const;    //返回当前容量(即string中不必增加内存即可存放的元素个数)
int max_size()const;    //返回string对象中可存放的最大字符串的长度
int size()const;        //返回当前字符串的大小
int length()const;       //返回当前字符串的长度
bool empty()const;        //当前字符串是否为空
void resize(int len,char c);//把字符串当前大小置为len,并用字符c填充不足的部分

string类的输入输出操作:
string类重载运算符operator>>用于输入,同样重载运算符operator<<用于输出操作。
函数getline(istream &in,string &s);用于从输入流in中读取字符串到s中,以换行符'
'分开。

string的赋值:
string &operator=(const string &s);//把字符串s赋给当前字符串
string &assign(const char *s);//用c类型字符串s赋值
string &assign(const char *s,int n);//用c字符串s开始的n个字符赋值
string &assign(const string &s);//把字符串s赋给当前字符串
string &assign(int n,char c);//用n个字符c赋值给当前字符串
string &assign(const string &s,int start,int n);//把字符串s中从start开始的n个字符赋给当前字符串
string &assign(const_iterator first,const_itertor last);//把first和last迭代器之间的部分赋给字符串

string的连接:
string &operator+=(const string &s);//把字符串s连接到当前字符串的结尾 
string &append(const char *s);            //把c类型字符串s连接到当前字符串结尾
string &append(const char *s,int n);//把c类型字符串s的前n个字符连接到当前字符串结尾
string &append(const string &s);    //同operator+=()
string &append(const string &s,int pos,int n);//把字符串s中从pos开始的n个字符连接到当前字符串的结尾
string &append(int n,char c);        //在当前字符串结尾添加n个字符c
string &append(const_iterator first,const_iterator last);//把迭代器first和last之间的部分连接到当前字符串的结尾 

string的比较:
bool operator==(const string &s1,const string &s2)const;//比较两个字符串是否相等
运算符">","<",">=","<=","!="均被重载用于字符串的比较;
int compare(const string &s) const;//比较当前字符串和s的大小
int compare(int pos, int n,const string &s)const;//比较当前字符串从pos开始的n个字符组成的字符串与s的大小
int compare(int pos, int n,const string &s,int pos2,int n2)const;//比较当前字符串从pos开始的n个字符组成的字符串与s中pos2开始的n2个字符组成的字符串的大小
int compare(const char *s) const;
int compare(int pos, int n,const char *s) const;
int compare(int pos, int n,const char *s, int pos2) const;
compare函数在>时返回1,<时返回-1,==时返回0  

string的子串:
string substr(int pos = 0,int n = npos) const;//返回pos开始的n个字符组成的字符串


string的交换:
void swap(string &s2);    //交换当前字符串与s2的值

string类的查找函数:

int find(char c, int pos = 0) const;//从pos开始查找字符c在当前字符串的位置
int find(const char *s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
int find(const char *s, int pos, int n) const;//从pos开始查找字符串s中前n个字符在当前串中的位置
int find(const string &s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
//查找成功时返回所在位置,失败返回string::npos的值

int rfind(char c, int pos = npos) const;//从pos开始从后向前查找字符c在当前串中的位置
int rfind(const char *s, int pos = npos) const;
int rfind(const char *s, int pos, int n = npos) const;
int rfind(const string &s,int pos = npos) const;
//从pos开始从后向前查找字符串s中前n个字符组成的字符串在当前串中的位置,成功返回所在位置,失败时返回string::npos的值

int find_first_of(char c, int pos = 0) const;//从pos开始查找字符c第一次出现的位置
int find_first_of(const char *s, int pos = 0) const;
int find_first_of(const char *s, int pos, int n) const;
int find_first_of(const string &s,int pos = 0) const;
//从pos开始查找当前串中第一个在s的前n个字符组成的数组里的字符的位置。查找失败返回string::npos

int find_first_not_of(char c, int pos = 0) const;
int find_first_not_of(const char *s, int pos = 0) const;
int find_first_not_of(const char *s, int pos,int n) const;
int find_first_not_of(const string &s,int pos = 0) const;
//从当前串中查找第一个不在串s中的字符出现的位置,失败返回string::npos

int find_last_of(char c, int pos = npos) const;
int find_last_of(const char *s, int pos = npos) const;
int find_last_of(const char *s, int pos, int n = npos) const;
int find_last_of(const string &s,int pos = npos) const;

int find_last_not_of(char c, int pos = npos) const;
int find_last_not_of(const char *s, int pos = npos) const;
int find_last_not_of(const char *s, int pos, int n) const;
int find_last_not_of(const string &s,int pos = npos) const;
//find_last_of和find_last_not_of与find_first_of和find_first_not_of相似,只不过是从后向前查找

string类的替换函数:

string &replace(int p0, int n0,const char *s);//删除从p0开始的n0个字符,然后在p0处插入串s
string &replace(int p0, int n0,const char *s, int n);//删除p0开始的n0个字符,然后在p0处插入字符串s的前n个字符
string &replace(int p0, int n0,const string &s);//删除从p0开始的n0个字符,然后在p0处插入串s
string &replace(int p0, int n0,const string &s, int pos, int n);//删除p0开始的n0个字符,然后在p0处插入串s中从pos开始的n个字符
string &replace(int p0, int n0,int n, char c);//删除p0开始的n0个字符,然后在p0处插入n个字符c
string &replace(iterator first0, iterator last0,const char *s);//把[first0,last0)之间的部分替换为字符串s
string &replace(iterator first0, iterator last0,const char *s, int n);//把[first0,last0)之间的部分替换为s的前n个字符
string &replace(iterator first0, iterator last0,const string &s);//把[first0,last0)之间的部分替换为串s
string &replace(iterator first0, iterator last0,int n, char c);//把[first0,last0)之间的部分替换为n个字符c
string &replace(iterator first0, iterator last0,const_iterator first, const_iterator last);//把[first0,last0)之间的部分替换成[first,last)之间的字符串

string类的插入函数:

string &insert(int p0, const char *s);
string &insert(int p0, const char *s, int n);
string &insert(int p0,const string &s);
string &insert(int p0,const string &s, int pos, int n);
//前4个函数在p0位置插入字符串s中pos开始的前n个字符
string &insert(int p0, int n, char c);//此函数在p0处插入n个字符c
iterator insert(iterator it, char c);//在it处插入字符c,返回插入后迭代器的位置
void insert(iterator it, const_iterator first, const_iterator last);//在it处插入[first,last)之间的字符
void insert(iterator it, int n, char c);//在it处插入n个字符c

string类的删除函数

iterator erase(iterator first, iterator last);//删除[first,last)之间的所有字符,返回删除后迭代器的位置
iterator erase(iterator it);//删除it指向的字符,返回删除后迭代器的位置
string &erase(int pos = 0, int n = npos);//删除pos开始的n个字符,返回修改后的字符串

string类的迭代器处理:

string类提供了向前和向后遍历的迭代器iterator,迭代器提供了访问各个字符的语法,类似于指针操作,迭代器不检查范围。
用string::iterator或string::const_iterator声明迭代器变量,const_iterator不允许改变迭代的内容。常用迭代器函数有:
const_iterator begin()const;
iterator begin();                //返回string的起始位置
const_iterator end()const;
iterator end();                    //返回string的最后一个字符后面的位置
const_iterator rbegin()const;
iterator rbegin();                //返回string的最后一个字符的位置
const_iterator rend()const;
iterator rend();                    //返回string第一个字符位置的前面
rbegin和rend用于从后向前的迭代访问,通过设置迭代器string::reverse_iterator,string::const_reverse_iterator实现

字符串流处理:

通过定义ostringstream和istringstream变量实现,<sstream>头文件中
例如:
    string input("hello,this is a test");
    istringstream is(input);
    string s1,s2,s3,s4;
    is>>s1>>s2>>s3>>s4;//s1="hello,this",s2="is",s3="a",s4="test"
    ostringstream os;
    os<<s1<<s2<<s3<<s4;
    cout<<os.str();

6.stable_sort

sort优点一大堆,一个缺点就是它不是一种稳定的排序。要追求稳定性,只好用stable_sort了。

在各种排序算法中,合并排序是稳定的,但一般的合并排序需要额外的O(N)的存储空间,而这个条件不是一定能够满足的。所以在stable_sort内部,首先判断是否有足够的额外空间,有的话就使用普通合并函数,总的时间复杂性和快速排序一个数量级,都是O(N*logN)。

如果没有额外空间,使用了一个merge_without_buffer的关键函数进行就地合并,这个合并过程不需要额外的存储空间(递归的堆栈除外),但时间复杂度变成O(N*logN),这种情况下,总的stable_sort时间复杂度是O(N*logN*logN)。

 

原文地址:https://www.cnblogs.com/updateofsimon/p/3443900.html