自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+

AZRAEL_DEATH

WA是一笔财富

  • 博客(35)
  • 资源 (8)
  • 收藏
  • 关注

原创 BZOJ3144【HZNOI2013】切糕 <最小割>

切糕 经历千辛万苦小A得到了一块切糕,切糕的形状是长方体,小A打算拦腰将切糕切成两半分给小B。出于美观考虑,小A希望切面能尽量光滑且和谐。于是她找到你,希望你能帮她找出最好的切割方案。 出于简便考虑,我们将切糕视作一个长P,宽Q,高R的长方体点阵。我们将位于第z层中第x行,第y列上的点称为(x,y,z),它有一个非负的不和谐值v(x,y,z)。一个合法的切面满足以下两个条件:

2017-09-19 17:51:42 501

原创 BZOJ1008【HNOI2008】越狱 <补集转换>

【HNOI2008】越狱 <补集转换> Time Limit: 1 Sec Memory Limit: 162 MB Description 监狱有连续编号为1…N的N个房间,每个房间关押一个犯人,有M种宗教,每个犯人可能信仰其中一种。如果相邻房间的犯人的宗教相同,就可能发生越狱,求有多少种状态可能发生越狱。 Input 输入两个整数M,N.1<=M<

2017-09-19 09:26:58 428

原创 BZOJ1066【SCOI2007】蜥蜴 <网络流>

【SCOI2007】蜥蜴 Description 在一个r行c列的网格地图中有一些高度不同的石柱,一些石柱上站着一些蜥蜴,你的任务是让尽量多的蜥蜴逃到边界外。 每行每列中相邻石柱的距离为1,蜥蜴的跳跃距离是d,即蜥蜴可以跳到平面距离不超过d的任何一个石柱上。石柱都不稳定,每次当蜥蜴跳跃时,所离开的石柱高度减1(如果仍然落在地图内部,则到达的石柱高度不变),如果该石柱原来高度为1,则蜥

2017-09-18 23:14:12 437

原创 BZOJ1189【HNOI2007】紧急疏散evacuate <二分答案+网络流>

【HNOI2007】紧急疏散evacuate Time Limit: 10 Sec Memory Limit: 128 MB Description 发生了火警,所有人员需要紧急疏散!假设每个房间是一个N M的矩形区域。每个格子如果是’.’,那么表示这是一块空地;如果是’X’,那么表示这是一面墙,如果是’D’,那么表示这是一扇门,人们可以从这儿撤出房间。已知门一定在房

2017-09-18 23:04:35 508

原创 HDU4348 To The Moon <带修主席树>

【HDU4348】To The Moon Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem Description To The Moon is a independent game released in November 2011, it

2017-09-17 22:10:58 401

原创 LG1073【NOIp2009】最优贸易 <Tarjan+DP>

最优贸易 C 国有 n 个大城市和 m 条道路,每条道路连接这 n 个城市中的某两个城市。任意两个城市之间最多只有一条道路直接相连。这 m 条道路中有一部分为单向通行的道路,一部分为双向通行的道路,双向通行的道路在统计条数时也计为 1 条。C 国幅员辽阔,各地的资源分布情况各不相同,这就导致了同一种商品在不同城市的价格不一定相同。但是,同一种商品在同一个城市的买入价和卖出价始终是相同的。

2017-09-17 21:59:40 494

原创 BZOJ1179【APOI2009】ATM <Tarjan>

ATM Time Limit: 15 Sec Memory Limit: 162 MB Description Siruseri城中的道路都是单向的。不同的道路由路口连接。按照法律规定,在每个路口都设立了一个Siruseri银行的ATM取款机。令人奇怪的是,Siruseri的酒吧都设在路口,虽然并不是每个路口都设有酒吧。 Banditiji计划实施Siruser

2017-09-17 21:43:50 330

原创 20170911~0917周总结

20170911~0917周总结

2017-09-17 21:02:28 212

原创 POJ1170 Shopping Offers <五维DP>

Shopping Offers In a shop each kind of product has a price. For example, the price of a flower is 2 ICU (Informatics Currency Units) and the price of a vase is 5 ICU. In order to attract more cust

2017-08-13 11:47:02 287

原创 POJ1182 【NOI2001】 食物链 <种类并查集>

食物链 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形。A吃B, B吃C,C吃A。 现有N个动物,以1-N编号。每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种。 有人用两种说法对这N个动物所构成的食物链关系进行描述: 第一种说法是”1 X Y”,表示X和Y是同类。 第二种说法是”2 X Y”,表示X吃Y。 此人对N

2017-08-12 21:00:20 237

原创 HDU5340 Three Palindromes <Manacher>

Three Palindromes Can we divided a given string S into three nonempty palindromes? Input First line contains a single integer T≤20 which denotes the number of test cases. For each test

2017-08-11 21:36:12 336

原创 POJ2155 Matrix <树套树/二维树状数组>

Matrix Time Limit: 3000MS Memory Limit: 65536K Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row and j-th column. Initially w

2017-08-11 21:19:19 317

原创 BZOJ3110 K大数查询 <树套树>

K大数查询 有N个位置,M个操作。操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c 如果是2 a b c形式,表示询问从第a个位置到第b个位置,第C大的数是多少。 Input 第一行N,M 接下来M行,每行形如1 a b c或2 a b c Output 输出每个询问的结果 Sample In

2017-08-11 21:14:21 257

原创 BZOJ2588 Count on a tree <DFS序+LCA+值域主席树>

Count on a tree 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权。其中lastans是上一个询问的答案,初始为0,即第一个询问的u是明文。 Input 第一行两个整数N,M。 第二行有N个整数,其中第i个整数表示点i的权值。 后面N-1行每行两个整数(x,y

2017-08-11 17:18:42 338

原创 POJ1201 Intervals <差分约束系统>

Intervals You are given n closed, integer intervals [ai, bi] and n integers c1, …, cn. Write a program that: reads the number of intervals, their end points and integers c1, …, cn from the

2017-08-11 17:04:37 238

原创 BZOJ1798 【AHOI2009】 seq维护序列 <线段树>

维护序列 题目描述 老师交给小可可一个维护数列的任务,现在小可可希望你来帮他完成。 有长为N的数列,不妨设为a1,a2,…,aN 。有如下三种操作形式: (1)把数列中的一段数全部乘一个值; (2)把数列中的一段数全部加一个值; (3)询问数列中的一段数的和,由于答案可能很大,你只需输出这个数模P的值。 输入输出格式 输入格式: 第一行两个整数N和P(1≤P≤1

2017-08-11 16:21:39 334

原创 BZOJ2002 Bounce 弹飞绵羊 <分块>

Bounce 弹飞绵羊 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏。游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装置设定初始弹力系数ki,当绵羊达到第i个装置时,它会往后弹ki步,达到第i+ki个装置,若不存在第i+ki个装置,则绵羊被弹飞。绵羊想知道当它从第i个装置起步时,被弹几次后会被弹飞。为了使得游戏

2017-08-11 16:08:33 324

原创 BZOJ3673 可持久化并查集 by zky <可持久化数组+主席树>

可持久化并查集 by zky n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b 询问a,b是否属于同一集合,是则输出1否则输出0 Sample Input 5 6 1 1 2 3 1 2 2 0 3 1 2 2 1 3 1 2 Sampl

2017-08-11 15:22:36 373

原创 BZOJ2434【NOI2011】阿狸的打字机 <AC自动机+Fail树+树状数组>

BZOJ2434 阿狸的打字机 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机。打字机上只有28个按键,分别印有26个小写英文字母和’B’、’P’两个字母。 经阿狸研究发现,这个打字机是这样工作的: ·输入小写字母,打字机的一个凹槽中会加入这个字母(这个字母加在凹槽的最后)。 ·按一下印有’B’的按键,打字机凹槽中最后一个字母会消失。 ·按一下印有’P’

2017-08-11 11:39:27 415

原创 20170710总结

20170710总结

2017-07-11 14:03:41 279

原创 可持久化线段树/主席树 基础原理和例题

可持久化线段树/主席树 基础原理和例题

2017-07-08 18:11:48 585

原创 20170707总结

20170707总结

2017-07-08 10:16:45 246

原创 20170706总结

20170706总结

2017-07-08 10:16:06 236

原创 20170705总结

20170705总结

2017-07-08 10:15:36 207

原创 20170703总结

20170703总结

2017-07-04 22:18:18 256

原创 20170704总结

20170704总结

2017-07-04 22:17:14 253

原创 20170701总结

20170701总结

2017-07-02 10:35:45 240

原创 20170630总结

20170630总结

2017-07-02 10:35:19 302

原创 20170629总结

20170629总结

2017-06-30 13:01:21 271

原创 20170628总结

20170628总结

2017-06-28 23:03:58 243

原创 20170627总结

20170627总结 今天进入真正的图论,复习了DFS序、LCA和最小生成树。LCA的两种方法,倍增写得很熟,RMQ则不那么熟练,学长提及了玄学的ST表,于是又把这个构造O(n)查询O(1)的神奇结构复习了一遍。我记得RMQ除了线段树,树状数组和ST表以外,还有一种叫莫队的东西。我九上的时候接触过,现在都忘完了,期待过几天讲数据结构的时候能遇到。关于最小生成树,虽然知道kruskal和prim两种

2017-06-27 22:48:41 225

原创 AC自动机模板及基础例题小结

AC自动机(Aho-Corasick Automation)

2017-06-27 13:30:47 899

原创 20170626总结

20170626总结今天是图论的第一节课。上午图论入门,讲了位运算和搜索。位运算虽然简单,但用处巨大,特别是在搜索和DP中。复习了一下lowbit的原理,新学了求1个数的平行算法(虽然可以打表,但是了解一下算数算法保险一些)。我自己学状压dp的时候学过子集枚举,现在重温以便,发现还不够熟悉,希望过几天讲dp的时候能在熟练一下。搜索作为一项基本功,还是很重要的。DFS和BFS就不说了,后面讲的双

2017-06-27 13:02:04 210

原创 20170623总结

20170623总结上午继续听dogther大神的课,复习了贪心、二分答案、三分答案。我一直没有系统地刷过贪心的题,今天大神将贪心的各类题都过了一遍,都懂了,只是不知道写码的时候会不会出现问题。贪心的例题里k叉的哈夫曼树以前没见过,看来和二叉的哈夫曼树还有点区别。此外,还有两道字典树上的贪心,思想都懂了,可能写码会有些细节上的问题。三分答案的精度一直是个问题,我只A了一道模板题,连经典的铁人两

2017-06-27 13:01:07 237

原创 20170622总结

20170622总结上午听dogther大神的课,重新学配vim,并复习了二分大神讲得很清楚,将所有基础知识点都复习了一遍,只可惜讲对拍的时候演示得太快,没学会大神在cpp程序内调用对拍的方法,只能苦逼地写脚本/(ㄒoㄒ)/~~听课之余复习了一下前天的ACautomation,又写了一道题(简单题,解密码,解完就是标准模板),发现貌似setFail有很多地方可以缩码,只是现下掌握不算熟

2017-06-27 13:00:15 236

APIO2018 T2T3 题解 陈俊锟 吕欣 张瑞喆

第二题 重复下列算法: 每次选出半径最大的圆 删除和它有交的所有圆 这里有交指圆盘公共区域不为 0 求每个圆是哪一次被删除的 第三题 先从简单的题开始 给出一张无向图 求有多少 (

2018-07-26

APIO2018T1 New Home 题解 吕欣

题目大意 在一个数轴上: 给定 N 个商店,每个商店有一个开业时间、关门时间、坐标和销售物品的种类 同时有 M 个询问,每个询问给你一个时间 t[i] 和地点 d[i],试求在 t[i] 时刻,一个住在 d[i] 的人,为了买某种商品最远需要跑多长距离。

2018-07-26

JavaScript学习指南

《JavaScript学习指南(第2版)》系统地介绍了JavaScript的基本语法、基本对象、调试工具与排错技术、事件处理机制、浏览器对象模型/文档对象模型(BOM/DOM)等方面的知识,并通过一个复杂的示例深入探讨了Ajax应用。《JavaScript学习指南(第2版)》提供了许多简单易懂、主题鲜明的示例,介绍了大量最佳实践和良好编程习惯,对提高代码可读性、可维护性均有很高的价值,并且对很多跨浏览器兼容问题进行了详细说明,追踪了新规范的发展。, 《JavaScript学习指南(第2版)》适合于希望通过JavaScript为自己的网页/网站添加活力的读者,不管你是否有编程经验,通过阅读《JavaScript学习指南(第2版)》都能够很快地掌握这一技术。在阅读《JavaScript学习指南(第2版)》之前,最好对CSS、HTML/XHTML有所了解。 点击链接进入新版 :, JavaScript学习指南(第2版修订版)

2017-10-14

Lua中文教程

目录 版权声明 ........................................................................................................................................ i 译序 ................................................................................................................................................ i 目录 .............................................................................................................................................. iii 第一篇 语言 ................................................................................................................................. 1 第 0 章 序言 ................................................................................................................................. 1 0.1 序言 .................................................................................................................................... 1 0.2 Lua的使用者....................................................................................................................... 2 0.3 Lua的相关资源 ................................................................................................................... 3 0.4 本书的体例 ........................................................................................................................ 3 0.5 关于本书 ............................................................................................................................ 3 0.6 感谢 .................................................................................................................................... 4 第 1 章 起点 ................................................................................................................................. 5 1.1 Chunks................................................................................................................................. 5 1.2 全局变量 ............................................................................................................................ 7 1.3 词法约定 ............................................................................................................................ 7 1.4 命令行方式 ........................................................................................................................ 7 第 2 章 类型和值 ......................................................................................................................... 9 2.1 Nil........................................................................................................................................ 9 2.2 Booleans .............................................................................................................................. 9 2.3 Numbers............................................................................................................................. 10 2.4 Strings................................................................................................................................ 10 2.5 Functions ........................................................................................................................... 12 2.6 Userdata and Threads ........................................................................................................ 12 第 3 章 表达式 ........................................................................................................................... 13 3.1 算术运算符 ...................................................................................................................... 13 3.2 关系运算符 ...................................................................................................................... 13 3.3 逻辑运算符 ...................................................................................................................... 13 3.4 连接运算符 ...................................................................................................................... 14 3.5 优先级 .............................................................................................................................. 15 3.6 表的构造 ........................................................................................................................... 15 第 4 章 基本语法 ....................................................................................................................... 18 4.1 赋值语句 .......................................................................................................................... 18 4.2 局部变量与代码块(block) ......................................................................................... 19 4.3 控制结构语句 .................................................................................................................. 20 Programming in Lua iv Copyright ® 2005, Translation Team, www.luachina.net 4.4 break和return语句............................................................................................................. 23 第 5 章 函数 ............................................................................................................................... 24 5.1 返回多个结果值 .............................................................................................................. 25 5.2 可变参数 ........................................................................................................................... 27 5.3 命名参数 .......................................................................................................................... 28 第 6 章 再论函数 ....................................................................................................................... 30 6.1 闭包 .................................................................................................................................. 32 6.2 非全局函数 ...................................................................................................................... 34 6.3 正确的尾调用(Proper Tail Calls) ............................................................................... 36 第 7 章 迭代器与泛型for........................................................................................................... 40 7.1 迭代器与闭包 .................................................................................................................. 40 7.2 范性for的语义.................................................................................................................. 42 7.3 无状态的迭代器 .............................................................................................................. 43 7.4 多状态的迭代器 .............................................................................................................. 44 7.5 真正的迭代器 .................................................................................................................. 45 第 8 章 编译·运行·调试 ....................................................................................................... 47 8.1 require函数........................................................................................................................ 49 8.2 C Packages......................................................................................................................... 50 8.3 错误 .................................................................................................................................. 51 8.4 异常和错误处理 .............................................................................................................. 52 8.5 错误信息和回跟踪(Tracebacks) ................................................................................ 53 第 9 章 协同程序 ....................................................................................................................... 56 9.1 协同的基础 ...................................................................................................................... 56 9.2 管道和过滤器 .................................................................................................................. 58 9.3 用作迭代器的协同 .......................................................................................................... 61 9.4 非抢占式多线程 .............................................................................................................. 63 第 10 章 完整示例 ..................................................................................................................... 68 10.1 Lua作为数据描述语言使用 ........................................................................................... 68 10.2 马尔可夫链算法 ............................................................................................................ 71 第二篇 tables与objects............................................................................................................... 75 第 11 章 数据结构 ..................................................................................................................... 76 11.1 数组 ................................................................................................................................ 76 11.2 阵和多维数组 ................................................................................................................ 77 11.3 链表 ................................................................................................................................ 78 11.4 队列和双端队列 ............................................................................................................ 78 11.5 集合和包 ........................................................................................................................ 80 11.6 字符串缓冲 .................................................................................................................... 80 第 12 章 数据文件与持久化 ..................................................................................................... 84 12.1 序列化 ............................................................................................................................ 86 Programming in Lua v Copyright ® 2005, Translation Team, www.luachina.net 第 13 章 Metatables and Metamethods ...................................................................................... 92 13.1 算术运算的Metamethods............................................................................................... 92 13.2 关系运算的Metamethods............................................................................................... 95 13.3 库定义的Metamethods................................................................................................... 96 13.4 表相关的Metamethods................................................................................................... 97 第 14 章 环境 ........................................................................................................................... 103 14.1 使用动态名字访问全局变量 ...................................................................................... 103 14.2 声明全局变量 ............................................................................................................... 104 14.3 非全局的环境 .............................................................................................................. 106 第 15 章 Packages .................................................................................................................... 109 15.1 基本方法 ...................................................................................................................... 109 15.2 私有成员(Privacy) ...................................................................................................111 15.3 包与文件 .......................................................................................................................112 15.4 使用全局表 ...................................................................................................................113 15.5 其他一些技巧(Other Facilities)...............................................................................115 第 16 章 面向对象程序设计 ....................................................................................................118 16.1 类 ...................................................................................................................................119 16.2 继承 .............................................................................................................................. 121 16.3 多重继承 ...................................................................................................................... 122 16.4 私有性(privacy) ...................................................................................................... 125 16.5 Single-Method的对象实现方法 ................................................................................... 127 第 17 章 Weak表 ...................................................................................................................... 128 17.1 记忆函数 ...................................................................................................................... 130 17.2 关联对象属性 .............................................................................................................. 131 17.3 重述带有默认值的表 .................................................................................................. 132 第三篇 标准库 ......................................................................................................................... 134 第 18 章 数学库 ....................................................................................................................... 135 第 19 章 Table库 ...................................................................................................................... 136 19.1 数组大小 ....................................................................................................................... 136 19.2 插入/删除 ..................................................................................................................... 137 19.3 排序 .............................................................................................................................. 137 第 20 章 String库 ..................................................................................................................... 140 20.1 模式匹配函数 .............................................................................................................. 141 20.2 模式 .............................................................................................................................. 143 20.3 捕获(Captures) ........................................................................................................ 146 20.4 转换的技巧(Tricks of the Trade)............................................................................ 151 第 21 章 IO库........................................................................................................................... 157 21.1 简单I/O模式................................................................................................................. 157 21.2 完全I/O 模式............................................................................................................... 160 Programming in Lua vi Copyright ® 2005, Translation Team, www.luachina.net 第 22 章 操作系统库 ............................................................................................................... 165 22.1 Date和Time ................................................................................................................... 165 22.2 其它的系统调用 .......................................................................................................... 167 第 23 章 Debug库..................................................................................................................... 169 23.1 自省(Introspective) ................................................................................................. 169 23.2 Hooks............................................................................................................................. 173 23.3 Profiles........................................................................................................................... 174 第四篇 C API ........................................................................................................................... 177 第 24 章 C API纵览 ................................................................................................................. 178 24.1 第一个示例程序 .......................................................................................................... 179 24.2 堆栈 .............................................................................................................................. 181 24.3 C API的错误处理 ......................................................................................................... 186 第 25 章 扩展你的程序 ........................................................................................................... 188 25.1 表操作 .......................................................................................................................... 189 25.2 调用Lua函数 ................................................................................................................ 193 25.3 通用的函数调用 .......................................................................................................... 195 第 26 章 调用C函数................................................................................................................. 198 26.1 C 函数........................................................................................................................... 198 26.2 C 函数库....................................................................................................................... 200 第 27 章 撰写C函数的技巧..................................................................................................... 203 27.1 数组操作 ...................................................................................................................... 203 27.2 字符串处理 .................................................................................................................. 204 27.3 在C函数中保存状态.................................................................................................... 207 第 28 章 User-Defined Types in C ........................................................................................... 212 28.1 Userdata......................................................................................................................... 212 28.2 Metatables...................................................................................................................... 215 28.3 访问面向对象的数据 .................................................................................................. 217 28.4 访问数组 ...................................................................................................................... 219 28.5 Light Userdata ............................................................................................................... 220 第 29 章 资源管理 ................................................................................................................... 222 29.1 目录迭代器 .................................................................................................................. 222 29.2 XML解析 ...................................................................................................................... 225

2017-10-14

骗分导论(OI)

0 前言 4 1 骗分——与复杂度的较量 6 2 参赛准备 7 2.1 心态——成就考试的前提 7 2.2 知己知彼, 百战不殆 8 2.2.1 单题的命制 8 2.2.2 测试数据的命制 10 2.2.3 套题的命制 11 2.3 应该学习的内容 12 2.4 找准位置, 水滴石穿 14 2.5 临阵磨枪, 不快也光 16 2.6 玩自己的养成游戏 17 3 复杂度常数优化 17 3.1 时间复杂度常数优化的意义 17 3.2 基本运算的速度 18 3.3 位运算的速度 20 3.4 数组运算的速度 23 3.5 实数运算的速度 24 1 博杰学习网, 官方顶级域名为 http://boj.pp.ru, 是本文作者的个人网站, 主要发表各学科竞赛( 主要是数 学、 信息学) 的信息动态、 竞赛试题解答、 学习资料、 个人研究成果, 趣味科学, 个人随笔文章, 班级事 务, 是石家庄二中首个学科竞赛资料库、 河北省信息学代表队资料库, 欢迎访问。 旗下网站有数之理论坛 http://boj.5d6d.com/, 属于数学、 信息学趣题、 文章、 资料交流的场所, 其中大量语 言精辟、 分析透彻的教程文章和趣味十足、 深入本质的趣题值得读者一看。 2 什么是信息学竞赛? 笔者认为, 信息学竞赛就是考察在有电脑的情况下“ 如何使用计算机代替人脑的工 作, 使得人的工作效率更高”, 就是考察应用计算机这个高效工具解决实际问题的能力。 3 本文所说的“ 骗分” 不是作弊, 而是尽可能多得分的应试技巧, 请不要误解。 NOI2009 河北省代表队论文 石家庄二中 李博杰 骗分导论·信息学竞赛 2 3.6 程序书写习惯 25 3.7 小算法 大优化 30 3.8 空间复杂度优化 33 3.9 降低编程复杂度 34 4 数学分析与猜想 41 4.1 数学与信息学 41 4.2 计算数学基础 45 4.2.1 素数判断算法 45 4.2.2 欧几里德算法 47 4.2.3 函数类算法 48 4.3 组合计数 50 4.4 计算几何算法 54 4.5 博弈论中的数学 61 4.5.1 纳什均衡 61 4.5.2 数学分析的应用 63 4.5.3 SG 定理与组合游戏 67 4.6 概率 68 4.7 组合构造 70 5 非完美算法 73 5.1 贪心法 74 5.2 随机法 78 5.3 试验法 78 5.4 调整法 78 5.5 模拟法 79 6 搜索算法 79 6.1 可行性剪枝 79 6.2 最优性剪枝 87 6.3 局部贪心、 动态规划 91 6.4 启发式搜索 92 7 骗分攻略 92 7.1 数学题——观察 92 7.2 小数据——交表 96 7.3 部分分——卡时 96 7.4 提交答案题——枚举 96 7.5 全面地考虑问题 98 7.6 设计获胜策略 103 8 调试程序 108 8.1 从静态查错开始 108 8.1.1 编译错误 108 8.1.2 运行错误 109 8.1.3 变量冲突 110 8.1.4 张冠李戴 115 8.1.5 正反对调 115 8.1.6 就差一点 116 NOI2009 河北省代表队论文 石家庄二中 李博杰 骗分导论·信息学竞赛 3 8.1.7 逻辑混乱 117 8.1.8 边界溢出 117 8.1.9 格式错误 117 8.1.10 忽略特例 118 8.1.11 分类失误 118 8.1.12 算法错误 118 8.2 善用输出语句 118 8.3 设计测试数据 122 8.4 考试有风险, 答题需谨慎 122 9 实战演习 124 9.1 2008 试题回顾与分析 124 9.1.1 2008 主要赛事题目与分类 124 9.1.2 题目方法与分类比例 126 9.1.3 竞赛规则 126 9.2 NOIP 模拟赛( 一) 127 9.3 NOIP 模拟赛( 二) 141 9.4 NOIP 模拟赛( 三) 154 9.5 NOIP 模拟赛( 四) 169 9.6 NOIP2008 183 9.6.1 笨小猴 184 9.6.2 火柴棒等式 186 9.6.3 传纸条 194 9.6.4 双栈排序 201 9.6.5 NOIP 小结 209 9.7 NOI2008 209 9.7.1 假面舞会 209 9.7.2 设计路线 211 9.7.3 志愿者招募 212 9.7.4 奥运物流 213 9.7.5 糖果雨 214 9.7.6 赛程安排 217 9.8 WC2009 219 9.8.1 最短路问题 219 9.8.2 语音识别 220 9.8.3 优化设计 223 9.9 CTSC2008 225 9.9.1 三角形的教学楼 225 9.9.2 祭祀 227 9.9.3 奥运抽奖 229 9.9.4 图腾 231 9.9.5 网络管理 232 9.9.6 唯美村落 234 9.10 IOI2008 236 10 骗分的实质 236 NOI2009 河北省代表队论文 石家庄二中 李博杰 骗分导论·信息学竞赛 4 10.1 Keep it simple and stupid 236 10.2 告别骗分 237 参考文献 238 感谢 238 后记——程序人生 238

2017-10-14

背包九讲_2.0

Contents 1 01背包问题 2 1.1 题目 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.2 基本思路 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.3 优化空间复杂度 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.4 初始化的细节问题 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.5 一个常数优化 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.6 小结 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 2 完全背包问题 4 2.1 题目 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 2.2 基本思路 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 2.3 一个简单有效的优化 . . . . . . . . . . . . . . . . . . . . . . . . . . 5 2.4 转化为01背包问题求解 . . . . . . . . . . . . . . . . . . . . . . . . . 5 2.5 O(V N)的算法 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 2.6 小结 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 3 多重背包问题 6 3.1 题目 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 3.2 基本算法 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 3.3 转化为01背包问题 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 3.4 O(VN)的算法 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 3.5 小结 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 4 混合三种背包问题 8 4.1 问题 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 4.2 01背包与完全背包的混合 . . . . . . . . . . . . . . . . . . . . . . . . 8 4.3 再加上多重背包 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 4.4 小结 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1 5 二维费用的背包问题 9 5.1 问题 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 5.2 算法 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 5.3 物品总个数的限制 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 5.4 复整数域上的背包问题 . . . . . . . . . . . . . . . . . . . . . . . . . 9 5.5 小结 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 6 分组的背包问题 10 6.1 问题 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 6.2 算法 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 6.3 小结 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 7 有依赖的背包问题 10 7.1 简化的问题 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 7.2 算法 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 7.3 较一般的问题 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 7.4 小结 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 8 泛化物品 11 8.1 定义 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 8.2 泛化物品的和 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 8.3 背包问题的泛化物品 . . . . . . . . . . . . . . . . . . . . . . . . . . 12 8.4 小结 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 9 背包问题问法的变化 13 9.1 输出方案 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 9.2 输出字典序最小的最优方案 . . . . . . . . . . . . . . . . . . . . . . 13 9.3 求方案总数 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 9.4 最优方案的总数 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 9.5 求次优解、第K优解 . . . . . . . . . . . . . . . . . . . . . . . . . . 14 9.6 小结 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

2017-10-14

c++标准库自学教程

Contents Preface to the Second Edition xxiii Acknowledgments for the Second Edition xxiv Preface to the First Edition xxv Acknowledgments for the First Edition xxvi 1 About This Book 1 1.1 Why This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.2 Before Reading This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.3 Style and Structure of the Book . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.4 How to Read This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.5 State of the Art . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.6 Example Code and Additional Information . . . . . . . . . . . . . . . . . . . . . 5 1.7 Feedback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 2 Introduction to C++ and the Standard Library 7 2.1 History of the C++ Standards . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 2.1.1 Common Questions about the C++11 Standard . . . . . . . . . . . . . . 8 2.1.2 Compatibility between C++98 and C++11 . . . . . . . . . . . . . . . . . 9 2.2 Complexity and Big-O Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 3 New Language Features 13 3.1 New C++11 Language Features . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 3.1.1 Important Minor Syntax Cleanups . . . . . . . . . . . . . . . . . . . . . 13 3.1.2 Automatic Type Deduction with auto . . . . . . . . . . . . . . . . . . . 14 3.1.3 Uniform Initialization and Initializer Lists . . . . . . . . . . . . . . . . . 15 3.1.4 Range-Based for Loops . . . . . . . . . . . . . . . . . . . . . . . . . . 17 3.1.5 Move Semantics and Rvalue References . . . . . . . . . . . . . . . . . . 19 ptg7913098 viii Contents 3.1.6 New String Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 3.1.7 Keyword noexcept . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 3.1.8 Keyword constexpr . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 3.1.9 New Template Features . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 3.1.10 Lambdas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 3.1.11 Keyword decltype . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 3.1.12 New Function Declaration Syntax . . . . . . . . . . . . . . . . . . . . . 32 3.1.13 Scoped Enumerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 3.1.14 New Fundamental Data Types . . . . . . . . . . . . . . . . . . . . . . . 33 3.2 Old “New” Language Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 3.2.1 Explicit Initialization for Fundamental Types . . . . . . . . . . . . . . . 37 3.2.2 Definition of main() . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 4 General Concepts 39 4.1 Namespace std . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 4.2 Header Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 4.3 Error and Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 4.3.1 Standard Exception Classes . . . . . . . . . . . . . . . . . . . . . . . . . 41 4.3.2 Members of Exception Classes . . . . . . . . . . . . . . . . . . . . . . . 44 4.3.3 Passing Exceptions with Class exception_ptr . . . . . . . . . . . . . . 52 4.3.4 Throwing Standard Exceptions . . . . . . . . . . . . . . . . . . . . . . . 53 4.3.5 Deriving from Standard Exception Classes . . . . . . . . . . . . . . . . . 54 4.4 Callable Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 4.5 Concurrency and Multithreading . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 4.6 Allocators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 5 Utilities 59 5.1 Pairs and Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 5.1.1 Pairs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 5.1.2 Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 5.1.3 I/O for Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 5.1.4 Conversions between tuples and pairs . . . . . . . . . . . . . . . . . . 75 5.2 Smart Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 5.2.1 Class shared_ptr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 5.2.2 Class weak_ptr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 5.2.3 Misusing Shared Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . 89 5.2.4 Shared and Weak Pointers in Detail . . . . . . . . . . . . . . . . . . . . . 92 5.2.5 Class unique_ptr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 ptg7913098 Contents ix 5.2.6 Class unique_ptr in Detail . . . . . . . . . . . . . . . . . . . . . . . . 110 5.2.7 Class auto_ptr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 5.2.8 Final Words on Smart Pointers . . . . . . . . . . . . . . . . . . . . . . . 114 5.3 Numeric Limits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 5.4 Type Traits and Type Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 5.4.1 Purpose of Type Traits . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 5.4.2 Type Traits in Detail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 5.4.3 Reference Wrappers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 5.4.4 Function Type Wrappers . . . . . . . . . . . . . . . . . . . . . . . . . . 133 5.5 Auxiliary Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 5.5.1 Processing the Minimum and Maximum . . . . . . . . . . . . . . . . . . 134 5.5.2 Swapping Two Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 5.5.3 Supplementary Comparison Operators . . . . . . . . . . . . . . . . . . . 138 5.6 Compile-Time Fractional Arithmetic with Class ratio<> . . . . . . . . . . . . . 140 5.7 Clocks and Timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 5.7.1 Overview of the Chrono Library . . . . . . . . . . . . . . . . . . . . . . 143 5.7.2 Durations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 5.7.3 Clocks and Timepoints . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 5.7.4 Date and Time Functions by C and POSIX . . . . . . . . . . . . . . . . . 157 5.7.5 Blocking with Timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 5.8 Header Files <cstddef>, <cstdlib>, and <cstring> . . . . . . . . . . . . . . 161 5.8.1 Definitions in <cstddef> . . . . . . . . . . . . . . . . . . . . . . . . . . 161 5.8.2 Definitions in <cstdlib> . . . . . . . . . . . . . . . . . . . . . . . . . . 162 5.8.3 Definitions in <cstring> . . . . . . . . . . . . . . . . . . . . . . . . . . 163 6 The Standard Template Library 165 6.1 STL Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 6.2 Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 6.2.1 Sequence Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 6.2.2 Associative Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 6.2.3 Unordered Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 6.2.4 Associative Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 6.2.5 Other Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 6.2.6 Container Adapters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 6.3 Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 6.3.1 Further Examples of Using Associative and Unordered Containers . . . . 193 6.3.2 Iterator Categories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198 ptg7913098 x Contents 6.4 Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 6.4.1 Ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 6.4.2 Handling Multiple Ranges . . . . . . . . . . . . . . . . . . . . . . . . . 207 6.5 Iterator Adapters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210 6.5.1 Insert Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210 6.5.2 Stream Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212 6.5.3 Reverse Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 6.5.4 Move Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 6.6 User-Defined Generic Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 6.7 Manipulating Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 6.7.1 “Removing” Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . 218 6.7.2 Manipulating Associative and Unordered Containers . . . . . . . . . . . 221 6.7.3 Algorithms versus Member Functions . . . . . . . . . . . . . . . . . . . 223 6.8 Functions as Algorithm Arguments . . . . . . . . . . . . . . . . . . . . . . . . . 224 6.8.1 Using Functions as Algorithm Arguments . . . . . . . . . . . . . . . . . 224 6.8.2 Predicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226 6.9 Using Lambdas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 6.10 Function Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 6.10.1 Definition of Function Objects . . . . . . . . . . . . . . . . . . . . . . . 233 6.10.2 Predefined Function Objects . . . . . . . . . . . . . . . . . . . . . . . . 239 6.10.3 Binders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 6.10.4 Function Objects and Binders versus Lambdas . . . . . . . . . . . . . . . 243 6.11 Container Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 6.11.1 Requirements for Container Elements . . . . . . . . . . . . . . . . . . . 244 6.11.2 Value Semantics or Reference Semantics . . . . . . . . . . . . . . . . . . 245 6.12 Errors and Exceptions inside the STL . . . . . . . . . . . . . . . . . . . . . . . . 245 6.12.1 Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 6.12.2 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 6.13 Extending the STL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 6.13.1 Integrating Additional Types . . . . . . . . . . . . . . . . . . . . . . . . 250 6.13.2 Deriving from STL Types . . . . . . . . . . . . . . . . . . . . . . . . . . 251 7 STL Containers 253 7.1 Common Container Abilities and Operations . . . . . . . . . . . . . . . . . . . . 254 7.1.1 Container Abilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254 7.1.2 Container Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254 7.1.3 Container Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260 ptg7913098 Contents xi 7.2 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 7.2.1 Abilities of Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 7.2.2 Array Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 7.2.3 Using arrays as C-Style Arrays . . . . . . . . . . . . . . . . . . . . . . 267 7.2.4 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268 7.2.5 Tuple Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268 7.2.6 Examples of Using Arrays . . . . . . . . . . . . . . . . . . . . . . . . . 268 7.3 Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270 7.3.1 Abilities of Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270 7.3.2 Vector Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 7.3.3 Using Vectors as C-Style Arrays . . . . . . . . . . . . . . . . . . . . . . 278 7.3.4 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278 7.3.5 Examples of Using Vectors . . . . . . . . . . . . . . . . . . . . . . . . . 279 7.3.6 Class vector<bool> . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281 7.4 Deques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283 7.4.1 Abilities of Deques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284 7.4.2 Deque Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 7.4.3 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288 7.4.4 Examples of Using Deques . . . . . . . . . . . . . . . . . . . . . . . . . 288 7.5 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290 7.5.1 Abilities of Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290 7.5.2 List Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291 7.5.3 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296 7.5.4 Examples of Using Lists . . . . . . . . . . . . . . . . . . . . . . . . . . 298 7.6 Forward Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300 7.6.1 Abilities of Forward Lists . . . . . . . . . . . . . . . . . . . . . . . . . . 300 7.6.2 Forward List Operations . . . . . . . . . . . . . . . . . . . . . . . . . . 302 7.6.3 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311 7.6.4 Examples of Using Forward Lists . . . . . . . . . . . . . . . . . . . . . . 312 7.7 Sets and Multisets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314 7.7.1 Abilities of Sets and Multisets . . . . . . . . . . . . . . . . . . . . . . . 315 7.7.2 Set and Multiset Operations . . . . . . . . . . . . . . . . . . . . . . . . . 316 7.7.3 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325 7.7.4 Examples of Using Sets and Multisets . . . . . . . . . . . . . . . . . . . 325 7.7.5 Example of Specifying the Sorting Criterion at Runtime . . . . . . . . . . 328 ptg7913098 xii Contents 7.8 Maps and Multimaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331 7.8.1 Abilities of Maps and Multimaps . . . . . . . . . . . . . . . . . . . . . . 332 7.8.2 Map and Multimap Operations . . . . . . . . . . . . . . . . . . . . . . . 333 7.8.3 Using Maps as Associative Arrays . . . . . . . . . . . . . . . . . . . . . 343 7.8.4 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345 7.8.5 Examples of Using Maps and Multimaps . . . . . . . . . . . . . . . . . . 345 7.8.6 Example with Maps, Strings, and Sorting Criterion at Runtime . . . . . . 351 7.9 Unordered Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355 7.9.1 Abilities of Unordered Containers . . . . . . . . . . . . . . . . . . . . . 357 7.9.2 Creating and Controlling Unordered Containers . . . . . . . . . . . . . . 359 7.9.3 Other Operations for Unordered Containers . . . . . . . . . . . . . . . . 367 7.9.4 The Bucket Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374 7.9.5 Using Unordered Maps as Associative Arrays . . . . . . . . . . . . . . . 374 7.9.6 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375 7.9.7 Examples of Using Unordered Containers . . . . . . . . . . . . . . . . . 375 7.10 Other STL Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385 7.10.1 Strings as STL Containers . . . . . . . . . . . . . . . . . . . . . . . . . 385 7.10.2 Ordinary C-Style Arrays as STL Containers . . . . . . . . . . . . . . . . 386 7.11 Implementing Reference Semantics . . . . . . . . . . . . . . . . . . . . . . . . . 388 7.12 When to Use Which Container . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392 8 STL Container Members in Detail 397 8.1 Type Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397 8.2 Create, Copy, and Destroy Operations . . . . . . . . . . . . . . . . . . . . . . . . 400 8.3 Nonmodifying Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403 8.3.1 Size Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403 8.3.2 Comparison Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . 404 8.3.3 Nonmodifying Operations for Associative and Unordered Containers . . . 404 8.4 Assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406 8.5 Direct Element Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408 8.6 Operations to Generate Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . 410 8.7 Inserting and Removing Elements . . . . . . . . . . . . . . . . . . . . . . . . . . 411 8.7.1 Inserting Single Elements . . . . . . . . . . . . . . . . . . . . . . . . . . 411 8.7.2 Inserting Multiple Elements . . . . . . . . . . . . . . . . . . . . . . . . . 416 8.7.3 Removing Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417 8.7.4 Resizing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420 ptg7913098 Contents xiii 8.8 Special Member Functions for Lists and Forward Lists . . . . . . . . . . . . . . . 420 8.8.1 Special Member Functions for Lists (and Forward Lists) . . . . . . . . . 420 8.8.2 Special Member Functions for Forward Lists Only . . . . . . . . . . . . 423 8.9 Container Policy Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427 8.9.1 Nonmodifying Policy Functions . . . . . . . . . . . . . . . . . . . . . . 427 8.9.2 Modifying Policy Functions . . . . . . . . . . . . . . . . . . . . . . . . 428 8.9.3 Bucket Interface for Unordered Containers . . . . . . . . . . . . . . . . . 429 8.10 Allocator Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430 8.10.1 Fundamental Allocator Members . . . . . . . . . . . . . . . . . . . . . . 430 8.10.2 Constructors with Optional Allocator Parameters . . . . . . . . . . . . . 430 9 STL Iterators 433 9.1 Header Files for Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433 9.2 Iterator Categories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433 9.2.1 Output Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433 9.2.2 Input Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435 9.2.3 Forward Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436 9.2.4 Bidirectional Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437 9.2.5 Random-Access Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . 438 9.2.6 The Increment and Decrement Problem of Vector Iterators . . . . . . . . 440 9.3 Auxiliary Iterator Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441 9.3.1 advance() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441 9.3.2 next() and prev() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443 9.3.3 distance() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445 9.3.4 iter_swap() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446 9.4 Iterator Adapters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448 9.4.1 Reverse Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448 9.4.2 Insert Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454 9.4.3 Stream Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460 9.4.4 Move Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466 9.5 Iterator Traits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466 9.5.1 Writing Generic Functions for Iterators . . . . . . . . . . . . . . . . . . . 468 9.6 Writing User-Defined Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471 ptg7913098 xiv Contents 10 STL Function Objects and Using Lambdas 475 10.1 The Concept of Function Objects . . . . . . . . . . . . . . . . . . . . . . . . . . 475 10.1.1 Function Objects as Sorting Criteria . . . . . . . . . . . . . . . . . . . . 476 10.1.2 Function Objects with Internal State . . . . . . . . . . . . . . . . . . . . 478 10.1.3 The Return Value of for_each() . . . . . . . . . . . . . . . . . . . . . 482 10.1.4 Predicates versus Function Objects . . . . . . . . . . . . . . . . . . . . . 483 10.2 Predefined Function Objects and Binders . . . . . . . . . . . . . . . . . . . . . . 486 10.2.1 Predefined Function Objects . . . . . . . . . . . . . . . . . . . . . . . . 486 10.2.2 Function Adapters and Binders . . . . . . . . . . . . . . . . . . . . . . . 487 10.2.3 User-Defined Function Objects for Function Adapters . . . . . . . . . . . 495 10.2.4 Deprecated Function Adapters . . . . . . . . . . . . . . . . . . . . . . . 497 10.3 Using Lambdas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499 10.3.1 Lambdas versus Binders . . . . . . . . . . . . . . . . . . . . . . . . . . 499 10.3.2 Lambdas versus Stateful Function Objects . . . . . . . . . . . . . . . . . 500 10.3.3 Lambdas Calling Global and Member Functions . . . . . . . . . . . . . . 502 10.3.4 Lambdas as Hash Function, Sorting, or Equivalence Criterion . . . . . . . 504 11 STL Algorithms 505 11.1 Algorithm Header Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505 11.2 Algorithm Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505 11.2.1 A Brief Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506 11.2.2 Classification of Algorithms . . . . . . . . . . . . . . . . . . . . . . . . 506 11.3 Auxiliary Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517 11.4 The for_each() Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519 11.5 Nonmodifying Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524 11.5.1 Counting Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524 11.5.2 Minimum and Maximum . . . . . . . . . . . . . . . . . . . . . . . . . . 525 11.5.3 Searching Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 528 11.5.4 Comparing Ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542 11.5.5 Predicates for Ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . 550 11.6 Modifying Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557 11.6.1 Copying Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557 11.6.2 Moving Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 561 11.6.3 Transforming and Combining Elements . . . . . . . . . . . . . . . . . . 563 11.6.4 Swapping Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 566 11.6.5 Assigning New Values . . . . . . . . . . . . . . . . . . . . . . . . . . . 568 11.6.6 Replacing Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 571 ptg7913098 Contents xv 11.7 Removing Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575 11.7.1 Removing Certain Values . . . . . . . . . . . . . . . . . . . . . . . . . . 575 11.7.2 Removing Duplicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . 578 11.8 Mutating Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583 11.8.1 Reversing the Order of Elements . . . . . . . . . . . . . . . . . . . . . . 583 11.8.2 Rotating Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 584 11.8.3 Permuting Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587 11.8.4 Shuffling Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 589 11.8.5 Moving Elements to the Front . . . . . . . . . . . . . . . . . . . . . . . 592 11.8.6 Partition into Two Subranges . . . . . . . . . . . . . . . . . . . . . . . . 594 11.9 Sorting Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 596 11.9.1 Sorting All Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . 596 11.9.2 Partial Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599 11.9.3 Sorting According to the nth Element . . . . . . . . . . . . . . . . . . . 602 11.9.4 Heap Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 604 11.10 Sorted-Range Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 608 11.10.1 Searching Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 608 11.10.2 Merging Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 614 11.11 Numeric Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 623 11.11.1 Processing Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 623 11.11.2 Converting Relative and Absolute Values . . . . . . . . . . . . . . . . . . 627 12 Special Containers 631 12.1 Stacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 632 12.1.1 The Core Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 633 12.1.2 Example of Using Stacks . . . . . . . . . . . . . . . . . . . . . . . . . . 633 12.1.3 A User-Defined Stack Class . . . . . . . . . . . . . . . . . . . . . . . . . 635 12.1.4 Class stack<> in Detail . . . . . . . . . . . . . . . . . . . . . . . . . . 637 12.2 Queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 638 12.2.1 The Core Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 639 12.2.2 Example of Using Queues . . . . . . . . . . . . . . . . . . . . . . . . . 640 12.2.3 A User-Defined Queue Class . . . . . . . . . . . . . . . . . . . . . . . . 641 12.2.4 Class queue<> in Detail . . . . . . . . . . . . . . . . . . . . . . . . . . 641 12.3 Priority Queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 641 12.3.1 The Core Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 643 12.3.2 Example of Using Priority Queues . . . . . . . . . . . . . . . . . . . . . 643 12.3.3 Class priority_queue<> in Detail . . . . . . . . . . . . . . . . . . . . 644 ptg7913098 xvi Contents 12.4 Container Adapters in Detail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 645 12.4.1 Type Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 645 12.4.2 Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 646 12.4.3 Supplementary Constructors for Priority Queues . . . . . . . . . . . . . . 646 12.4.4 Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 647 12.5 Bitsets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 650 12.5.1 Examples of Using Bitsets . . . . . . . . . . . . . . . . . . . . . . . . . 651 12.5.2 Class bitset in Detail . . . . . . . . . . . . . . . . . . . . . . . . . . . 653 13 Strings 655 13.1 Purpose of the String Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 656 13.1.1 A First Example: Extracting a Temporary Filename . . . . . . . . . . . . 656 13.1.2 A Second Example: Extracting Words and Printing Them Backward . . . 660 13.2 Description of the String Classes . . . . . . . . . . . . . . . . . . . . . . . . . . 663 13.2.1 String Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 663 13.2.2 Operation Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 666 13.2.3 Constructors and Destructor . . . . . . . . . . . . . . . . . . . . . . . . 667 13.2.4 Strings and C-Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . 668 13.2.5 Size and Capacity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 669 13.2.6 Element Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 671 13.2.7 Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 672 13.2.8 Modifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 673 13.2.9 Substrings and String Concatenation . . . . . . . . . . . . . . . . . . . . 676 13.2.10 Input/Output Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . 677 13.2.11 Searching and Finding . . . . . . . . . . . . . . . . . . . . . . . . . . . 678 13.2.12 The Value npos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 680 13.2.13 Numeric Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 681 13.2.14 Iterator Support for Strings . . . . . . . . . . . . . . . . . . . . . . . . . 684 13.2.15 Internationalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 689 13.2.16 Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 692 13.2.17 Strings and Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 692 13.3 String Class in Detail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 693 13.3.1 Type Definitions and Static Values . . . . . . . . . . . . . . . . . . . . . 693 13.3.2 Create, Copy, and Destroy Operations . . . . . . . . . . . . . . . . . . . 694 13.3.3 Operations for Size and Capacity . . . . . . . . . . . . . . . . . . . . . . 696 13.3.4 Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 697 13.3.5 Character Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 699 13.3.6 Generating C-Strings and Character Arrays . . . . . . . . . . . . . . . . 700 ptg7913098 Contents xvii 13.3.7 Modifying Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 700 13.3.8 Searching and Finding . . . . . . . . . . . . . . . . . . . . . . . . . . . 708 13.3.9 Substrings and String Concatenation . . . . . . . . . . . . . . . . . . . . 711 13.3.10 Input/Output Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 712 13.3.11 Numeric Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 713 13.3.12 Generating Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 714 13.3.13 Allocator Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715 14 Regular Expressions 717 14.1 The Regex Match and Search Interface . . . . . . . . . . . . . . . . . . . . . . . 717 14.2 Dealing with Subexpressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 720 14.3 Regex Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 726 14.4 Regex Token Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 727 14.5 Replacing Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . 730 14.6 Regex Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 732 14.7 Regex Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 735 14.8 The Regex ECMAScript Grammar . . . . . . . . . . . . . . . . . . . . . . . . . 738 14.9 Other Grammars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 739 14.10 Basic Regex Signatures in Detail . . . . . . . . . . . . . . . . . . . . . . . . . . 740 15 Input/Output Using Stream Classes 743 15.1 Common Background of I/O Streams . . . . . . . . . . . . . . . . . . . . . . . . 744 15.1.1 Stream Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 744 15.1.2 Stream Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 744 15.1.3 Global Stream Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . 745 15.1.4 Stream Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 745 15.1.5 Manipulators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 746 15.1.6 A Simple Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 746 15.2 Fundamental Stream Classes and Objects . . . . . . . . . . . . . . . . . . . . . . 748 15.2.1 Classes and Class Hierarchy . . . . . . . . . . . . . . . . . . . . . . . . 748 15.2.2 Global Stream Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . 751 15.2.3 Header Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 752 15.3 Standard Stream Operators << and >> . . . . . . . . . . . . . . . . . . . . . . . . 753 15.3.1 Output Operator << . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 753 15.3.2 Input Operator >> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 754 15.3.3 Input/Output of Special Types . . . . . . . . . . . . . . . . . . . . . . . 755 ptg7913098 xviii Contents 15.4 State of Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 758 15.4.1 Constants for the State of Streams . . . . . . . . . . . . . . . . . . . . . 758 15.4.2 Member Functions Accessing the State of Streams . . . . . . . . . . . . . 759 15.4.3 Stream State and Boolean Conditions . . . . . . . . . . . . . . . . . . . 760 15.4.4 Stream State and Exceptions . . . . . . . . . . . . . . . . . . . . . . . . 762 15.5 Standard Input/Output Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 767 15.5.1 Member Functions for Input . . . . . . . . . . . . . . . . . . . . . . . . 768 15.5.2 Member Functions for Output . . . . . . . . . . . . . . . . . . . . . . . 771 15.5.3 Example Uses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 772 15.5.4 sentry Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 772 15.6 Manipulators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 774 15.6.1 Overview of All Manipulators . . . . . . . . . . . . . . . . . . . . . . . 774 15.6.2 How Manipulators Work . . . . . . . . . . . . . . . . . . . . . . . . . . 776 15.6.3 User-Defined Manipulators . . . . . . . . . . . . . . . . . . . . . . . . . 777 15.7 Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 779 15.7.1 Format Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 779 15.7.2 Input/Output Format of Boolean Values . . . . . . . . . . . . . . . . . . 781 15.7.3 Field Width, Fill Character, and Adjustment . . . . . . . . . . . . . . . . 781 15.7.4 Positive Sign and Uppercase Letters . . . . . . . . . . . . . . . . . . . . 784 15.7.5 Numeric Base . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 785 15.7.6 Floating-Point Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . 787 15.7.7 General Formatting Definitions . . . . . . . . . . . . . . . . . . . . . . . 789 15.8 Internationalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 790 15.9 File Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 791 15.9.1 File Stream Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 791 15.9.2 Rvalue and Move Semantics for File Streams . . . . . . . . . . . . . . . 795 15.9.3 File Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 796 15.9.4 Random Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 799 15.9.5 Using File Descriptors . . . . . . . . . . . . . . . . . . . . . . . . . . . 801 15.10 Stream Classes for Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 802 15.10.1 String Stream Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 802 15.10.2 Move Semantics for String Streams . . . . . . . . . . . . . . . . . . . . . 806 15.10.3 char* Stream Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 807 15.11 Input/Output Operators for User-Defined Types . . . . . . . . . . . . . . . . . . . 810 15.11.1 Implementing Output Operators . . . . . . . . . . . . . . . . . . . . . . 810 15.11.2 Implementing Input Operators . . . . . . . . . . . . . . . . . . . . . . . 812 15.11.3 Input/Output Using Auxiliary Functions . . . . . . . . . . . . . . . . . . 814 ptg7913098 Contents xix 15.11.4 User-Defined Format Flags . . . . . . . . . . . . . . . . . . . . . . . . . 815 15.11.5 Conventions for User-Defined Input/Output Operators . . . . . . . . . . . 818 15.12 Connecting Input and Output Streams . . . . . . . . . . . . . . . . . . . . . . . . 819 15.12.1 Loose Coupling Using tie() . . . . . . . . . . . . . . . . . . . . . . . . 819 15.12.2 Tight Coupling Using Stream Buffers . . . . . . . . . . . . . . . . . . . 820 15.12.3 Redirecting Standard Streams . . . . . . . . . . . . . . . . . . . . . . . . 822 15.12.4 Streams for Reading and Writing . . . . . . . . . . . . . . . . . . . . . . 824 15.13 The Stream Buffer Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 826 15.13.1 The Stream Buffer Interfaces . . . . . . . . . . . . . . . . . . . . . . . . 826 15.13.2 Stream Buffer Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . 828 15.13.3 User-Defined Stream Buffers . . . . . . . . . . . . . . . . . . . . . . . . 832 15.14 Performance Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 844 15.14.1 Synchronization with C’s Standard Streams . . . . . . . . . . . . . . . . 845 15.14.2 Buffering in Stream Buffers . . . . . . . . . . . . . . . . . . . . . . . . . 845 15.14.3 Using Stream Buffers Directly . . . . . . . . . . . . . . . . . . . . . . . 846 16 Internationalization 849 16.1 Character Encodings and Character Sets . . . . . . . . . . . . . . . . . . . . . . . 850 16.1.1 Multibyte and Wide-Character Text . . . . . . . . . . . . . . . . . . . . . 850 16.1.2 Different Character Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . 851 16.1.3 Dealing with Character Sets in C++ . . . . . . . . . . . . . . . . . . . . 852 16.1.4 Character Traits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 853 16.1.5 Internationalization of Special Characters . . . . . . . . . . . . . . . . . 857 16.2 The Concept of Locales . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 857 16.2.1 Using Locales . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 858 16.2.2 Locale Facets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 864 16.3 Locales in Detail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 866 16.4 Facets in Detail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 869 16.4.1 Numeric Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 870 16.4.2 Monetary Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . 874 16.4.3 Time and Date Formatting . . . . . . . . . . . . . . . . . . . . . . . . . 884 16.4.4 Character Classification and Conversion . . . . . . . . . . . . . . . . . . 891 16.4.5 String Collation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 904 16.4.6 Internationalized Messages . . . . . . . . . . . . . . . . . . . . . . . . . 905 ptg7913098 xx Contents 17 Numerics 907 17.1 Random Numbers and Distributions . . . . . . . . . . . . . . . . . . . . . . . . . 907 17.1.1 A First Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 908 17.1.2 Engines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 912 17.1.3 Engines in Detail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 915 17.1.4 Distributions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 917 17.1.5 Distributions in Detail . . . . . . . . . . . . . . . . . . . . . . . . . . . . 921 17.2 Complex Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 925 17.2.1 Class complex<> in General . . . . . . . . . . . . . . . . . . . . . . . . 925 17.2.2 Examples Using Class complex<> . . . . . . . . . . . . . . . . . . . . . 926 17.2.3 Operations for Complex Numbers . . . . . . . . . . . . . . . . . . . . . 928 17.2.4 Class complex<> in Detail . . . . . . . . . . . . . . . . . . . . . . . . . 935 17.3 Global Numeric Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 941 17.4 Valarrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 943 18 Concurrency 945 18.1 The High-Level Interface: async() and Futures . . . . . . . . . . . . . . . . . . 946 18.1.1 A First Example Using async() and Futures . . . . . . . . . . . . . . . 946 18.1.2 An Example of Waiting for Two Tasks . . . . . . . . . . . . . . . . . . . 955 18.1.3 Shared Futures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 960 18.2 The Low-Level Interface: Threads and Promises . . . . . . . . . . . . . . . . . . 964 18.2.1 Class std::thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 964 18.2.2 Promises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 969 18.2.3 Class packaged_task<> . . . . . . . . . . . . . . . . . . . . . . . . . . 972 18.3 Starting a Thread in Detail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 973 18.3.1 async() in Detail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 974 18.3.2 Futures in Detail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 975 18.3.3 Shared Futures in Detail . . . . . . . . . . . . . . . . . . . . . . . . . . . 976 18.3.4 Class std::promise in Detail . . . . . . . . . . . . . . . . . . . . . . . 977 18.3.5 Class std::packaged_task in Detail . . . . . . . . . . . . . . . . . . . 977 18.3.6 Class std::thread in Detail . . . . . . . . . . . . . . . . . . . . . . . . 979 18.3.7 Namespace this_thread . . . . . . . . . . . . . . . . . . . . . . . . . 981 18.4 Synchronizing Threads, or the Problem of Concurrency . . . . . . . . . . . . . . 982 18.4.1 Beware of Concurrency! . . . . . . . . . . . . . . . . . . . . . . . . . . 982 18.4.2 The Reason for the Problem of Concurrent Data Access . . . . . . . . . . 983 18.4.3 What Exactly Can Go Wrong (the Extent of the Problem) . . . . . . . . . 983 18.4.4 The Features to Solve the Problems . . . . . . . . . . . . . . . . . . . . . 987 ptg7913098 Contents xxi 18.5 Mutexes and Locks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 989 18.5.1 Using Mutexes and Locks . . . . . . . . . . . . . . . . . . . . . . . . . . 989 18.5.2 Mutexes and Locks in Detail . . . . . . . . . . . . . . . . . . . . . . . . 998 18.5.3 Calling Once for Multiple Threads . . . . . . . . . . . . . . . . . . . . . 1000 18.6 Condition Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1003 18.6.1 Purpose of Condition Variables . . . . . . . . . . . . . . . . . . . . . . . 1003 18.6.2 A First Complete Example for Condition Variables . . . . . . . . . . . . 1004 18.6.3 Using Condition Variables to Implement a Queue for Multiple Threads . . 1006 18.6.4 Condition Variables in Detail . . . . . . . . . . . . . . . . . . . . . . . . 1009 18.7 Atomics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1012 18.7.1 Example of Using Atomics . . . . . . . . . . . . . . . . . . . . . . . . . 1012 18.7.2 Atomics and Their High-Level Interface in Detail . . . . . . . . . . . . . 1016 18.7.3 The C-Style Interface of Atomics . . . . . . . . . . . . . . . . . . . . . . 1019 18.7.4 The Low-Level Interface of Atomics . . . . . . . . . . . . . . . . . . . . 1019 19 Allocators 1023 19.1 Using Allocators as an Application Programmer . . . . . . . . . . . . . . . . . . 1023 19.2 A User-Defined Allocator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1024 19.3 Using Allocators as a Library Programmer . . . . . . . . . . . . . . . . . . . . . 1026 Bibliography 1031 Newsgroups and Forums . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1031 Books and Web Sites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1032 Index 1037

2017-10-14

数据结构Advanced-Data-Structures

数据结构原本,大一统,外文书原版 Data structures Contents Articles Introduction 1 Data structure 1 Linked data structure 3 Succinct data structure 6 Implicit data structure 8 Compressed data structure 9 Search data structure 10 Persistent data structure 11 Concurrent data structure 18 Abstract data types 21 Abstract data type 21 List 29 Stack 32 Queue 61 Deque 63 Priority queue 66 Map 70 Bidirectional map 73 Multimap 74 Set 75 Tree 80 Arrays 85 Array data structure 85 Row-major order 91 Dope vector 93 Iliffe vector 94 Dynamic array 95 Hashed array tree 98 Gap buffer 99 Circular buffer 101 Sparse array 111 Bit array 112 Bitboard 117 Parallel array 121 Lookup table 123 Lists 129 Linked list 129 XOR linked list 145 Unrolled linked list 147 VList 149 Skip list 151 Self-organizing list 157 Binary trees 162 Binary tree 162 Binary search tree 170 Self-balancing binary search tree 180 Tree rotation 182 Weight-balanced tree 185 Threaded binary tree 186 AVL tree 191 Red-black tree 195 AA tree 210 Scapegoat tree 215 Splay tree 219 T-tree 234 Rope 237 Top Trees 242 Tango Trees 246 Van Emde Boas tree 268 Cartesian tree 272 Treap 277 B-trees 281 B-tree 281 B+ tree 292 Dancing tree 297 2-3 tree 298 2-3-4 tree 299 Queaps 301 Fusion tree 305 Bx-tree 309 Heaps 312 Heap 312 Binary heap 315 Binomial heap 321 Fibonacci heap 326 2-3 heap 331 Pairing heap 331 Beap 334 Leftist tree 335 Skew heap 338 Soft heap 341 d-ary heap 343 Tries 346 Trie 346 Radix tree 353 Suffix tree 358 Suffix array 363 Compressed suffix array 367 FM-index 368 Generalised suffix tree 371 B-trie 372 Judy array 372 Directed acyclic word graph 374 Multiway trees 376 Ternary search tree 376 And–or tree 379 (a,b)-tree 380 Link/cut tree 381 SPQR tree 381 Spaghetti stack 384 Disjoint-set data structure 385 Space-partitioning trees 389 Space partitioning 389 Binary space partitioning 390 Segment tree 395 Interval tree 399 Range tree 404 Bin 406 k-d tree 408 Implicit k-d tree 416 min/max kd-tree 419 Adaptive k-d tree 420 Quadtree 421 Octree 427 Linear octrees 429 Z-order 429 UB-tree 434 R-tree 435 R+ tree 441 R* tree 442 Hilbert R-tree 445 X-tree 452 Metric tree 452 vP-tree 453 BK-tree 454 Hashes 455 Hash table 455 Hash function 468 Open addressing 476 Lazy deletion 479 Linear probing 479 Quadratic probing 480 Double hashing 484 Cuckoo hashing 486 Coalesced hashing 491 Perfect hash function 494 Universal hashing 496 Linear hashing 501 Extendible hashing 502 2-choice hashing 508 Pearson hashing 508 Fowler–Noll–Vo hash function 509 Bitstate hashing 511 Bloom filter 512 Locality preserving hashing 523 Morton number 524 Zobrist hashing 529 Rolling hash 530 Hash list 531 Hash tree 532 Prefix hash tree 534 Hash trie 535 Hash array mapped trie 535 Distributed hash table 536 Consistent hashing 542 Stable hashing 544 Koorde 544 Graphs 547 Graph 547 Adjacency list 549 Adjacency matrix 551 And-inverter graph 554 Binary decision diagram 556 Binary moment diagram 560 Zero-suppressed decision diagram 562 Propositional directed acyclic graph 563 Graph-structured stack 564 Scene graph 565 Appendix 570 Big O notation 570 Amortized analysis 581 Locality of reference 582 Standard Template Library 585 References Article Sources and Contributors 596 Image Sources, Licenses and Contributors 605 Article Licenses License 610

2017-10-14

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除