常用STL函数

1. string

*(以下函数均为str为示例字符串,用法为str. + 下面的函数)

  1. find(string a)

    ​ 类似kmp的功能,返回a在str中首字母出现的位置

  2. append(string s2,int st,int en)

    ​ 在str后方加入s2从st到en的部分

  3. operator + (用法为str= str + s1)

    ​ 将两个字符串简单拼接

  4. length()

    ​ 返回字符串的长度

  5. replace(string s1,string s2) / (string s1,int begin,int len)

    ​ 将原字符串中的s1全部替换为s2

  6. substr(int st,int len)

    ​ 截取指定位置区间内的字串

  7. getline(cin,str)

    ​ 读取整行,不读取回车和行末空格

2. queue

(以下函数均以q为示例队列,用法为q. + 下面的函数)

  1. push()

​ 将一个元素加入队列。若加入类型为结构体,用法为push((自定义结构体){参数})

  1. pop()

​ 将队首弹出队列

  1. front()

​ 返回队首元素

  1. empty()

​ 判断队列是否为空,是返回0,不是返回1

3. stack

(以下函数均以s为示例栈,用法为s. + 下面的函数)

  1. push()

​ 将一个元素加入栈。若加入元素的类型为结构体,用法为push((自定义结构体){参数})

  1. pop()

​ 将队首弹出栈

  1. top()

    返回栈顶元素

  2. empty()

​ 判断栈是否为空,是返回0,不是返回1

4.map

(以下函数均以m为示例映射)

  1. operator [ ] (m[ ])

    返回关键字对应的映射

  2. count() (用法为m.count(key))

    判断关键字是否出现过,是则返回1,否则返回0

* 对于要将结构体加入映射,必须重载operator <

具体重载如下:

struct MYSTRUCT
{
    int u,v;
    bool operator <(const MYSTRUCT a)
        return u<a.u?u:a.u;//其实如果只需要用count的话,这句话是什么无关紧要
}

5.priority_queue

(以下函数均以q为示例优先队列(堆),用法为q. + 下面的函数)

  1. push()

​ 将一个元素加入优先队列(堆)。若加入元素的类型为结构体,用法为push((自定义结构体){参数})

  1. pop()

​ 将队首弹出优先队列(堆)

  1. top()

    返回优先队列(堆)顶部元素

  2. empty()

​ 判断优先队列(堆)是否为空,是返回0,不是返回1

* 优先队列(堆)默认为大根堆,若要将其改为小根堆,在定义时需加入参数

具体代码如下:

priority_queue < int ,vector<int>, greater<int> > q;

* 万分感谢神犇Trust纠正错误!

若要用结构体,则还需要重载>

假设我们要用的结构体为Worm(这绝对不是蚯蚓的正解中节选的):

struct Worm
{
	long long cut,len;
};
bool operator > (const Worm a,const Worm b)
{
	return a.len-q*a.cut<b.len-q*b.cut;
}
priority_queue < Worm ,vector<Worm>, greater<Worm> > q0; 
But a man is not made for defeat.A man can be destroyed but not defeated.