Beware of big rollbacks of mass inserts的理解?

mysqldbd 2011-04-27 03:53:16
Beware of big rollbacks of mass inserts: InnoDB uses the insert buffer to save disk I/O in inserts, but no such mechanism is used in a corresponding rollback. A disk-bound rollback can take 30 times as long to perform as the corresponding insert. Killing the database process does not help because the rollback starts again on server startup. The only way to get rid of a runaway rollback is to increase the buffer pool so that the rollback becomes CPU-bound and runs fast, or to use a special procedure. See Section 13.6.6.2, “Forcing InnoDB Recovery”.

问下 A disk-bound rollback can take 30 times as long to perform as the corresponding insert。中的30 times 是啥意思?难道有30次的rollback?该怎么理解?
...全文
113 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
曳尾之鱼 2011-05-02
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 mysqldbd 的回复:]

引用 5 楼 esperantor 的回复:
引用 4 楼 mysqldbd 的回复:

引用 3 楼 rucypli 的回复:
受限于磁盘的回滚操作要比对应的插入操作多花费三十倍的时间?


我也觉得不会是30倍吧,要是多5倍的时间,我还能理解,多30倍,不知道是何原因呢?



InnoDB uses the insert buffer to save disk I/O……
[/Quote]

可能innodb还不够完善吧

嗯,Oralce有回滚段
iihero_ 2011-05-02
  • 打赏
  • 举报
回复
就是30倍啊,一个使用buffer,一个不使用buffer, 差别肯定很明显。
mysqldbd 2011-04-29
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 aleng18 的回复:]
免费的就是木有啊,
5.5的应该快了很多吧,
[/Quote]

我看的就是社区版5.5的官方文档
aleng18 2011-04-29
  • 打赏
  • 举报
回复
免费的就是木有啊,
5.5的应该快了很多吧,
mysqldbd 2011-04-29
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 esperantor 的回复:]
引用 4 楼 mysqldbd 的回复:

引用 3 楼 rucypli 的回复:
受限于磁盘的回滚操作要比对应的插入操作多花费三十倍的时间?


我也觉得不会是30倍吧,要是多5倍的时间,我还能理解,多30倍,不知道是何原因呢?



InnoDB uses the insert buffer to save disk I/O in inserts, but no such ……
[/Quote]

后面半句,明白意思了,但是为什么innodb没有rollback的缓冲区内存呢,oracle好像就有啊!
曳尾之鱼 2011-04-29
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 mysqldbd 的回复:]

引用 3 楼 rucypli 的回复:
受限于磁盘的回滚操作要比对应的插入操作多花费三十倍的时间?


我也觉得不会是30倍吧,要是多5倍的时间,我还能理解,多30倍,不知道是何原因呢?
[/Quote]


InnoDB uses the insert buffer to save disk I/O in inserts, but no such mechanism is used in a corresponding rollback

相应的inserts没有insert buffer,需要进行disk I/0交互
mysqldbd 2011-04-29
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 rucypli 的回复:]
受限于磁盘的回滚操作要比对应的插入操作多花费三十倍的时间?
[/Quote]

我也觉得不会是30倍吧,要是多5倍的时间,我还能理解,多30倍,不知道是何原因呢?
rucypli 2011-04-27
  • 打赏
  • 举报
回复
受限于磁盘的回滚操作要比对应的插入操作多花费三十倍的时间?
ACMAIN_CHM 2011-04-27
  • 打赏
  • 举报
回复
[Quote= #1楼 Esperantor(曳尾之鱼) ]30倍吧[/Quote]

曳尾之鱼 2011-04-27
  • 打赏
  • 举报
回复

30 times

30倍吧
The C++ Standard Library A Tutorial and Reference (2nd Edition)+cppstdlib-code.zip C++标准库(第二版)英文版.pdf 非扫描版+源代码 Prefaceto the SecondEdition xxiii Acknowledgments for the SecondEdition xxiv Prefaceto the FirstEdition xxv Acknowledgments for the FirstEdition xxvi 1 About This Book 1 1.1 Why This Book. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.2 Before ReadingThis Book. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.3 Styleand Structure of the Book . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.4 How to ReadThis Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.5 Stateof the Art . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.6 Example Codeand AdditionalInformation . . . . . . . . . . . . . . . . . . . . . 5 1.7 Feedback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 2 Introduction to C++ and the StandardLibrary 7 2.1 Historyof the C++ Standards . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 2.1.1 Common Questionsabout 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 LanguageFeatures 13 3.1 New C++11 Language Features . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 3.1.1 Important MinorSyntax Cleanups . . . . . . . . . . . . . . . . . . . . . 13 3.1.2 AutomaticType Deductionwith auto . . . . . . . . . . . . . . . . . . . 14 3.1.3 UniformInitialization and Initializer Lists . . . . . . . . . . . . . . . . . 15 3.1.4 Range-Basedfor Loops . . . . . . . . . . . . . . . . . . . . . . . . . . 17 3.1.5 MoveSemanticsand Rvalue References . . . . . . . . . . . . . . . . . . 19 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 FundamentalData Types . . . . . . . . . . . . . . . . . . . . . . . 33 3.2 Old “New” Language Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 3.2.1 ExplicitInitialization for FundamentalTypes . . . . . . . . . . . . . . . 37 3.2.2 Definitionof main() . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 4 GeneralConcepts 39 4.1 Namespace std . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 4.2 Header Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 4.3 Errorand ExceptionHandling . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 4.3.1 Standard ExceptionClasses. . . . . . . . . . . . . . . . . . . . . . . . . 41 4.3.2 Members of ExceptionClasses. . . . . . . . . . . . . . . . . . . . . . . 44 4.3.3 PassingExceptions with Classexception_ptr . . . . . . . . . . . . . . 52 4.3.4 Throwing Standard Exceptions . . . . . . . . . . . . . . . . . . . . . . . 53 4.3.5 Deriving from Standard ExceptionClasses. . . . . . . . . . . . . . . . . 54 4.4 CallableObjects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 4.5 Concurrencyand 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 tuple sandpairs . . . . . . . . . . . . . . . . . . 75 5.2 Smart Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 5.2.1 Classshared_ptr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 5.2.2 Classweak_ptr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 5.2.3 Misusing Shared Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . 89 5.2.4 Shared and WeakPointersin Detail. . . . . . . . . . . . . . . . . . . . . 92 5.2.5 Classunique_ptr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 Contents ix 5.2.6 Classunique_ptrin Detail . . . . . . . . . . . . . . . . . . . . . . . . 110 5.2.7 Classauto_ptr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 5.2.8 FinalWordsonSmart Pointers . . . . . . . . . . . . . . . . . . . . . . . 114 5.3 Numeric Limits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 5.4 Type Traitsand Type Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 5.4.1 Purposeof Type Traits . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 5.4.2 Type Traitsin Detail. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 5.4.3 ReferenceWrappers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 5.4.4 Function Type Wrappers . . . . . . . . . . . . . . . . . . . . . . . . . . 133 5.5 Auxiliary Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 5.5.1 Processing the Minimumand Maximum. . . . . . . . . . . . . . . . . . 134 5.5.2 Swapping Two Va l u e s . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 5.5.3 SupplementaryComparison Operators . . . . . . . . . . . . . . . . . . . 138 5.6 Compile-Time FractionalArithmeticwith Classratio . . . . . . . . . . . . . 140 5.7 Clocks and Timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 5.7.1 Overview of the ChronoLibrary . . . . . . . . . . . . . . . . . . . . . . 143 5.7.2 Durations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 5.7.3 Clocks and Timepoints . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 5.7.4 Date and TimeFunctions byC and POSIX . . . . . . . . . . . . . . . . . 157 5.7.5 Blocking with Timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 5.8 Header Files , ,and . . . . . . . . . . . . . . 161 5.8.1 Definitionsin . . . . . . . . . . . . . . . . . . . . . . . . . . 161 5.8.2 Definitionsin . . . . . . . . . . . . . . . . . . . . . . . . . . 162 5.8.3 Definitionsin . . . . . . . . . . . . . . . . . . . . . . . . . . 163 6 The StandardTe m p l a t e 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 UnorderedContainers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 6.2.4 Associative Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 6.2.5 OtherContainers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 6.2.6 Container Adapters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 6.3 Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 6.3.1 Further Examples of UsingAssociative and UnorderedContainers . . . . 193 6.3.2 Iterator Categories. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198 x Contents 6.4 Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 6.4.1 Ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 6.4.2 Handling MultipleRanges . . . . . . . . . . . . . . . . . . . . . . . . . 207 6.5 Iterator Adapters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210 6.5.1 Insert Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210 6.5.2 Stream Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212 6.5.3 ReverseIterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 6.5.4 MoveIterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 6.6 User-DefinedGenericFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 6.7 ManipulatingAlgorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 6.7.1 “Removing”Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . 218 6.7.2 ManipulatingAssociative and UnorderedContainers . . . . . . . . . . . 221 6.7.3 Algorithms versus MemberFunctions . . . . . . . . . . . . . . . . . . . 223 6.8 Functions as AlgorithmArguments . . . . . . . . . . . . . . . . . . . . . . . . . 224 6.8.1 UsingFunctions as AlgorithmArguments . . . . . . . . . . . . . . . . . 224 6.8.2 Predicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226 6.9 UsingLambdas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 6.10 Function Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 6.10.1 Definitionof Function Objects . . . . . . . . . . . . . . . . . . . . . . . 233 6.10.2 PredefinedFunction Objects . . . . . . . . . . . . . . . . . . . . . . . . 239 6.10.3 Binders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 6.10.4 Function Objectsand Bindersversus Lambdas . . . . . . . . . . . . . . . 243 6.11 Container Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 6.11.1 Requirements for Container Elements . . . . . . . . . . . . . . . . . . . 244 6.11.2 Va l u eSemanticsor ReferenceSemantics. . . . . . . . . . . . . . . . . . 245 6.12 Errors and Exceptions inside the STL . . . . . . . . . . . . . . . . . . . . . . . . 245 6.12.1 ErrorHandling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 6.12.2 ExceptionHandling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 6.13 Extendingthe STL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 6.13.1 Integrating AdditionalTypes . . . . . . . . . . . . . . . . . . . . . . . . 250 6.13.2 Deriving from STL Types . . . . . . . . . . . . . . . . . . . . . . . . . . 251 7 STL Containers 253 7.1 Common Container Abilitiesand Operations . . . . . . . . . . . . . . . . . . . . 254 7.1.1 Container Abilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254 7.1.2 Container Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254 7.1.3 Container Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260 Contents xi 7.2 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 7.2.1 Abilitiesof Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 7.2.2 ArrayOperations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 7.2.3 Usingarray s as C-StyleArrays . . . . . . . . . . . . . . . . . . . . . . 267 7.2.4 ExceptionHandling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268 7.2.5 TupleInterface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268 7.2.6 Examples of UsingArrays . . . . . . . . . . . . . . . . . . . . . . . . . 268 7.3 Ve c t o r s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270 7.3.1 Abilitiesof Ve c t o r s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270 7.3.2 Ve c t o r Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 7.3.3 UsingVe c t o r sas C-StyleArrays . . . . . . . . . . . . . . . . . . . . . . 278 7.3.4 ExceptionHandling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278 7.3.5 Examples of UsingVe c t o r s . . . . . . . . . . . . . . . . . . . . . . . . . 279 7.3.6 Classvector . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281 7.4 Deques. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283 7.4.1 Abilitiesof Deques. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284 7.4.2 Deque Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 7.4.3 ExceptionHandling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288 7.4.4 Examples of UsingDeques. . . . . . . . . . . . . . . . . . . . . . . . . 288 7.5 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290 7.5.1 Abilitiesof Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290 7.5.2 List Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291 7.5.3 ExceptionHandling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296 7.5.4 Examples of UsingLists . . . . . . . . . . . . . . . . . . . . . . . . . . 298 7.6 Forward Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300 7.6.1 Abilitiesof Forward Lists . . . . . . . . . . . . . . . . . . . . . . . . . . 300 7.6.2 Forward List Operations . . . . . . . . . . . . . . . . . . . . . . . . . . 302 7.6.3 ExceptionHandling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311 7.6.4 Examples of UsingForward Lists . . . . . . . . . . . . . . . . . . . . . . 312 7.7 Sets and Multisets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314 7.7.1 Abilitiesof Sets and Multisets . . . . . . . . . . . . . . . . . . . . . . . 315 7.7.2 Setand MultisetOperations. . . . . . . . . . . . . . . . . . . . . . . . . 316 7.7.3 ExceptionHandling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325 7.7.4 Examples of UsingSets and Multisets . . . . . . . . . . . . . . . . . . . 325 7.7.5 Example of Specifying the Sorting Criterion at Runtime . . . . . . . . . . 328 xii Contents 7.8 Mapsand Multimaps. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331 7.8.1 Abilitiesof Mapsand Multimaps. . . . . . . . . . . . . . . . . . . . . . 332 7.8.2 Map and Multimap Operations . . . . . . . . . . . . . . . . . . . . . . . 333 7.8.3 UsingMapsas Associative Arrays . . . . . . . . . . . . . . . . . . . . . 343 7.8.4 ExceptionHandling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345 7.8.5 Examples of UsingMapsand Multimaps. . . . . . . . . . . . . . . . . . 345 7.8.6 Example with Maps,Strings,and Sorting Criterion at Runtime . . . . . . 351 7.9 UnorderedContainers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355 7.9.1 Abilitiesof UnorderedContainers . . . . . . . . . . . . . . . . . . . . . 357 7.9.2 Creating and Controlling UnorderedContainers . . . . . . . . . . . . . . 359 7.9.3 OtherOperationsfor UnorderedContainers . . . . . . . . . . . . . . . . 367 7.9.4 The Bucket Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374 7.9.5 UsingUnorderedMapsas Associative Arrays . . . . . . . . . . . . . . . 374 7.9.6 ExceptionHandling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375 7.9.7 Examples of UsingUnorderedContainers . . . . . . . . . . . . . . . . . 375 7.10 OtherSTL Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385 7.10.1 Strings as STL Containers . . . . . . . . . . . . . . . . . . . . . . . . . 385 7.10.2 Ordinary C-StyleArrays as STL Containers . . . . . . . . . . . . . . . . 386 7.11 Implementing ReferenceSemantics . . . . . . . . . . . . . . . . . . . . . . . . . 388 7.12 Whento Use WhichContainer . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392 8 STL ContainerMembersin Detail 397 8.1 Type Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397 8.2 Create, Copy,and DestroyOperations . . . . . . . . . . . . . . . . . . . . . . . . 400 8.3 Nonmodifying Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403 8.3.1 Size Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403 8.3.2 Comparison Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . 404 8.3.3 Nonmodifying Operationsfor Associative and UnorderedContainers . . . 404 8.4 Assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406 8.5 Direct ElementAccess . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408 8.6 Operationsto Generate Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . 410 8.7 Inserting and RemovingElements . . . . . . . . . . . . . . . . . . . . . . . . . . 411 8.7.1 Inserting Single Elements . . . . . . . . . . . . . . . . . . . . . . . . . . 411 8.7.2 Inserting MultipleElements . . . . . . . . . . . . . . . . . . . . . . . . . 416 8.7.3 RemovingElements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417 8.7.4 Resizing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420 Contents xiii 8.8 Special MemberFunctions for Lists and Forward Lists . . . . . . . . . . . . . . . 420 8.8.1 Special MemberFunctions for Lists (and Forward Lists) . . . . . . . . . 420 8.8.2 Special MemberFunctions for Forward Lists Only . . . . . . . . . . . . 423 8.9 Container Policy Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427 8.9.1 Nonmodifying Policy Functions . . . . . . . . . . . . . . . . . . . . . . 427 8.9.2 ModifyingPolicy Functions . . . . . . . . . . . . . . . . . . . . . . . . 428 8.9.3 Bucket Interface for UnorderedContainers . . . . . . . . . . . . . . . . . 429 8.10 Allocator Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430 8.10.1 FundamentalAllocator Members . . . . . . . . . . . . . . . . . . . . . . 430 8.10.2 Constructorswith 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 BidirectionalIterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437 9.2.5 Random-Access Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . 438 9.2.6 The Incrementand DecrementProblem of Ve c t o r 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 ReverseIterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448 9.4.2 Insert Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454 9.4.3 Stream Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460 9.4.4 MoveIterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466 9.5 Iterator Traits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466 9.5.1 WritingGenericFunctions for Iterators . . . . . . . . . . . . . . . . . . . 468 9.6 WritingUser-DefinedIterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471 xiv Contents 10 STL Function Objectsand UsingLambdas 475 10.1 The Conceptof Function Objects . . . . . . . . . . . . . . . . . . . . . . . . . . 475 10.1.1 Function Objectsas Sorting Criteria . . . . . . . . . . . . . . . . . . . . 476 10.1.2 Function Objectswith Internal State . . . . . . . . . . . . . . . . . . . . 478 10.1.3 The Return Va l u eof for_each() . . . . . . . . . . . . . . . . . . . . . 482 10.1.4 Predicatesversus Function Objects. . . . . . . . . . . . . . . . . . . . . 483 10.2 PredefinedFunction Objectsand Binders . . . . . . . . . . . . . . . . . . . . . . 486 10.2.1 PredefinedFunction Objects . . . . . . . . . . . . . . . . . . . . . . . . 486 10.2.2 Function Adapters and Binders. . . . . . . . . . . . . . . . . . . . . . . 487 10.2.3 User-DefinedFunction Objectsfor Function Adapters . . . . . . . . . . . 495 10.2.4 Deprecated Function Adapters . . . . . . . . . . . . . . . . . . . . . . . 497 10.3 UsingLambdas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499 10.3.1 Lambdas versus Binders . . . . . . . . . . . . . . . . . . . . . . . . . . 499 10.3.2 Lambdas versus StatefulFunction Objects. . . . . . . . . . . . . . . . . 500 10.3.3 Lambdas Calling Global and MemberFunctions . . . . . . . . . . . . . . 502 10.3.4 Lambdas as HashFunction, Sorting,or Equivalence Criterion . . . . . . . 504 11 STL Algorithms 505 11.1 AlgorithmHeader Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505 11.2 AlgorithmOverview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505 11.2.1 A BriefIntroduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 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 Minimumand Maximum. . . . . . . . . . . . . . . . . . . . . . . . . . 525 11.5.3 SearchingElements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 528 11.5.4 Comparing Ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542 11.5.5 Predicatesfor Ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . 550 11.6 ModifyingAlgorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557 11.6.1 Copying Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557 11.6.2 MovingElements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 561 11.6.3 Transforming and Combining Elements . . . . . . . . . . . . . . . . . . 563 11.6.4 Swapping Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 566 11.6.5 AssigningNew Va l u e s . . . . . . . . . . . . . . . . . . . . . . . . . . . 568 11.6.6 ReplacingElements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 571 Contents xv 11.7 RemovingAlgorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575 11.7.1 RemovingCertain Va l u e s . . . . . . . . . . . . . . . . . . . . . . . . . . 575 11.7.2 RemovingDuplicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . 578 11.8 Mutating Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583 11.8.1 Reversingthe Orderof Elements . . . . . . . . . . . . . . . . . . . . . . 583 11.8.2 Rotating Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 584 11.8.3 PermutingElements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587 11.8.4 Shuffling Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 589 11.8.5 MovingElements to the Front . . . . . . . . . . . . . . . . . . . . . . . 592 11.8.6 Partition into Two Subranges . . . . . . . . . . . . . . . . . . . . . . . . 594 11.9 Sorting Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 596 11.9.1 Sorting AllElements . . . . . . . . . . . . . . . . . . . . . . . . . . . . 596 11.9.2 Partial Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599 11.9.3 Sorting Accordingto the n th Element . . . . . . . . . . . . . . . . . . . 602 11.9.4 HeapAlgorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 604 11.10 Sorted-Range Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 608 11.10.1SearchingElements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 608 11.10.2MergingElements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 614 11.11 Numeric Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 623 11.11.1Processing Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 623 11.11.2Converting Relativeand Absolute Va l u e s . . . . . . . . . . . . . . . . . . 627 12 SpecialContainers 631 12.1 Stacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 632 12.1.1 The Core Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 633 12.1.2 Example of UsingStacks . . . . . . . . . . . . . . . . . . . . . . . . . . 633 12.1.3 A User-DefinedStackClass. . . . . . . . . . . . . . . . . . . . . . . . . 635 12.1.4 Classstack in Detail . . . . . . . . . . . . . . . . . . . . . . . . . . 637 12.2 Queues. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 638 12.2.1 The Core Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 639 12.2.2 Example of UsingQueues . . . . . . . . . . . . . . . . . . . . . . . . . 640 12.2.3 A User-DefinedQueue Class . . . . . . . . . . . . . . . . . . . . . . . . 641 12.2.4 Classqueue in Detail . . . . . . . . . . . . . . . . . . . . . . . . . . 641 12.3 PriorityQueues. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 641 12.3.1 The Core Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 643 12.3.2 Example of UsingPriorityQueues. . . . . . . . . . . . . . . . . . . . . 643 12.3.3 Classpriority_queue in Detail . . . . . . . . . . . . . . . . . . . . 644 xvi Contents 12.4 Container Adapters in Detail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 645 12.4.1 Type Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 645 12.4.2 Constructors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 646 12.4.3 SupplementaryConstructorsfor PriorityQueues. . . . . . . . . . . . . . 646 12.4.4 Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 647 12.5 Bitsets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 650 12.5.1 Examples of UsingBitsets . . . . . . . . . . . . . . . . . . . . . . . . . 651 12.5.2 Classbitsetin Detail . . . . . . . . . . . . . . . . . . . . . . . . . . . 653 13 Strings 655 13.1 Purposeof the String Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 656 13.1.1 A First Example: Extractinga Temporary Filename . . . . . . . . . . . . 656 13.1.2 A Second Example: ExtractingWordsand PrintingThemBackward . . . 660 13.2 Description of the String Classes . . . . . . . . . . . . . . . . . . . . . . . . . . 663 13.2.1 String Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 663 13.2.2 OperationOverview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 666 13.2.3 Constructorsand Destructor . . . . . . . . . . . . . . . . . . . . . . . . 667 13.2.4 Strings and C-Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . 668 13.2.5 Size and Capacity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 669 13.2.6 ElementAccess . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 671 13.2.7 Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 672 13.2.8 Modifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 673 13.2.9 Substringsand String Concatenation . . . . . . . . . . . . . . . . . . . . 676 13.2.10Input/Output Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . 677 13.2.11Searchingand Finding . . . . . . . . . . . . . . . . . . . . . . . . . . . 678 13.2.12The Va l u enpos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 680 13.2.13Numeric Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 681 13.2.14Iterator Supportfor Strings . . . . . . . . . . . . . . . . . . . . . . . . . 684 13.2.15Internationalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 689 13.2.16Performance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 692 13.2.17Strings and Ve c t o r s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 692 13.3 String Classin Detail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 693 13.3.1 Type Definitionsand StaticVa l u e s . . . . . . . . . . . . . . . . . . . . . 693 13.3.2 Create, Copy,and DestroyOperations . . . . . . . . . . . . . . . . . . . 694 13.3.3 Operationsfor Size and Capacity . . . . . . . . . . . . . . . . . . . . . . 696 13.3.4 Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 697 13.3.5 Character Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 699 13.3.6 GeneratingC-Strings and Character Arrays . . . . . . . . . . . . . . . . 700 Contents xvii 13.3.7 ModifyingOperations. . . . . . . . . . . . . . . . . . . . . . . . . . . . 700 13.3.8 Searchingand Finding . . . . . . . . . . . . . . . . . . . . . . . . . . . 708 13.3.9 Substringsand String Concatenation . . . . . . . . . . . . . . . . . . . . 711 13.3.10Input/Output Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 712 13.3.11Numeric Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 713 13.3.12GeneratingIterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 714 13.3.13Allocator Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715 14 RegularExpressions 717 14.1 The Regex Matchand Search Interface . . . . . . . . . . . . . . . . . . . . . . . 717 14.2 Dealingwith Subexpressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 720 14.3 Regex Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 726 14.4 Regex Token Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 727 14.5 ReplacingRegularExpressions . . . . . . . . . . . . . . . . . . . . . . . . . . . 730 14.6 Regex Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 732 14.7 Regex Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 735 14.8 The Regex ECMAScriptGrammar . . . . . . . . . . . . . . . . . . . . . . . . . 738 14.9 OtherGrammars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 739 14.10 BasicRegex Signaturesin Detail . . . . . . . . . . . . . . . . . . . . . . . . . . 740 15 Input/Output UsingStreamClasses 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 FundamentalStream Classesand Objects. . . . . . . . . . . . . . . . . . . . . . 748 15.2.1 Classes and ClassHierarchy . . . . . . . . . . . . . . . . . . . . . . . . 748 15.2.2 Global Stream Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . 751 15.2.3 Header Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 752 15.3 Standard Stream Operators <> . . . . . . . . . . . . . . . . . . . . . . . . 753 15.3.1 Output Operator <> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 754 15.3.3 Input/Output of Special Types . . . . . . . . . . . . . . . . . . . . . . . 755 xviii Contents 15.4 Stateof Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 758 15.4.1 Constants for the Stateof Streams . . . . . . . . . . . . . . . . . . . . . 758 15.4.2 MemberFunctions Accessingthe Stateof Streams. . . . . . . . . . . . . 759 15.4.3 Stream Stateand BooleanConditions . . . . . . . . . . . . . . . . . . . 760 15.4.4 Stream Stateand Exceptions . . . . . . . . . . . . . . . . . . . . . . . . 762 15.5 Standard Input/Output Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 767 15.5.1 MemberFunctions for Input . . . . . . . . . . . . . . . . . . . . . . . . 768 15.5.2 MemberFunctions for Output . . . . . . . . . . . . . . . . . . . . . . . 771 15.5.3 Example Uses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 772 15.5.4 sentryObjects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 772 15.6 Manipulators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 774 15.6.1 Overview of AllManipulators . . . . . . . . . . . . . . . . . . . . . . . 774 15.6.2 How ManipulatorsWork . . . . . . . . . . . . . . . . . . . . . . . . . . 776 15.6.3 User-DefinedManipulators. . . . . . . . . . . . . . . . . . . . . . . . . 777 15.7 Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 779 15.7.1 Format Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 779 15.7.2 Input/Output Format of BooleanVa l u e s . . . . . . . . . . . . . . . . . . 781 15.7.3 FieldWidth, Fill Character,and Adjustment . . . . . . . . . . . . . . . . 781 15.7.4 PositiveSign and UppercaseLetters . . . . . . . . . . . . . . . . . . . . 784 15.7.5 Numeric Base . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 785 15.7.6 Floating-Point Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . 787 15.7.7 GeneralFormatting Definitions . . . . . . . . . . . . . . . . . . . . . . . 789 15.8 Internationalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 790 15.9 File Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 791 15.9.1 File Stream Classes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 791 15.9.2 Rvalue and MoveSemanticsfor File Streams . . . . . . . . . . . . . . . 795 15.9.3 File Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 796 15.9.4 RandomAccess . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 799 15.9.5 UsingFile Descriptors . . . . . . . . . . . . . . . . . . . . . . . . . . . 801 15.10 Stream Classesfor Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 802 15.10.1String Stream Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 802 15.10.2MoveSemanticsfor String Streams. . . . . . . . . . . . . . . . . . . . . 806 15.10.3char* Stream Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 807 15.11 Input/Output Operators for User-DefinedTypes . . . . . . . . . . . . . . . . . . . 810 15.11.1Implementing Output Operators . . . . . . . . . . . . . . . . . . . . . . 810 15.11.2Implementing Input Operators . . . . . . . . . . . . . . . . . . . . . . . 812 15.11.3Input/Output UsingAuxiliary Functions . . . . . . . . . . . . . . . . . . 814 Contents xix 15.11.4User-DefinedFormat Flags . . . . . . . . . . . . . . . . . . . . . . . . . 815 15.11.5Conventionsfor User-DefinedInput/Output Operators . . . . . . . . . . . 818 15.12 Connecting Input and Output Streams . . . . . . . . . . . . . . . . . . . . . . . . 819 15.12.1Loose Coupling Usingtie() . . . . . . . . . . . . . . . . . . . . . . . . 819 15.12.2TightCoupling UsingStream Buffers . . . . . . . . . . . . . . . . . . . 820 15.12.3Redirecting Standard Streams. . . . . . . . . . . . . . . . . . . . . . . . 822 15.12.4Streamsfor Readingand Writing. . . . . . . . . . . . . . . . . . . . . . 824 15.13 The Stream Buffer Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 826 15.13.1The Stream Buffer Interfaces . . . . . . . . . . . . . . . . . . . . . . . . 826 15.13.2Stream Buffer Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . 828 15.13.3User-DefinedStream Buffers . . . . . . . . . . . . . . . . . . . . . . . . 832 15.14 PerformanceIssues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 844 15.14.1Synchronization with C’sStandard Streams . . . . . . . . . . . . . . . . 845 15.14.2Buffering in Stream Buffers . . . . . . . . . . . . . . . . . . . . . . . . . 845 15.14.3UsingStream Buffers Directly . . . . . . . . . . . . . . . . . . . . . . . 846 16 Internationalization 849 16.1 Character Encodingsand Character Sets . . . . . . . . . . . . . . . . . . . . . . . 850 16.1.1 Multibyte and Wide-CharacterText . . . . . . . . . . . . . . . . . . . . . 850 16.1.2 Different Character Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . 851 16.1.3 Dealingwith Character Sets in C++ . . . . . . . . . . . . . . . . . . . . 852 16.1.4 Character Traits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 853 16.1.5 Internationalization of Special Characters . . . . . . . . . . . . . . . . . 857 16.2 The Conceptof Locales . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 857 16.2.1 UsingLocales. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 858 16.2.2 Locale Facets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 864 16.3 Localesin Detail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 866 16.4 Facets in Detail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 869 16.4.1 Numeric Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 870 16.4.2 Monetary Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . 874 16.4.3 Timeand Date Formatting . . . . . . . . . . . . . . . . . . . . . . . . . 884 16.4.4 Character Classification and Conversion . . . . . . . . . . . . . . . . . . 891 16.4.5 String Collation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 904 16.4.6 Internationalized Messages . . . . . . . . . . . . . . . . . . . . . . . . . 905 xx Contents 17 Numerics 907 17.1 RandomNumbers and Distributions. . . . . . . . . . . . . . . . . . . . . . . . . 907 17.1.1 A First Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 908 17.1.2 Engines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 912 17.1.3 Enginesin Detail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 915 17.1.4 Distributions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 917 17.1.5 Distributionsin Detail . . . . . . . . . . . . . . . . . . . . . . . . . . . . 921 17.2 Complex Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 925 17.2.1 Classcomplex in General . . . . . . . . . . . . . . . . . . . . . . . . 925 17.2.2 Examples UsingClasscomplex . . . . . . . . . . . . . . . . . . . . . 926 17.2.3 Operationsfor Complex Numbers . . . . . . . . . . . . . . . . . . . . . 928 17.2.4 Classcomplex in Detail . . . . . . . . . . . . . . . . . . . . . . . . . 935 17.3 Global Numeric Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 941 17.4 Va l a r r a y s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 943 18 Concurrency 945 18.1 The High-Level Interface: async() and Futures . . . . . . . . . . . . . . . . . . 946 18.1.1 A First Example Usingasync() and Futures . . . . . . . . . . . . . . . 946 18.1.2 AnExample of Waitingfor Two Tasks . . . . . . . . . . . . . . . . . . . 955 18.1.3 Shared Futures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 960 18.2 The Low-Level Interface: Threadsand Promises . . . . . . . . . . . . . . . . . . 964 18.2.1 Classstd::thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 964 18.2.2 Promises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 969 18.2.3 Classpackaged_task . . . . . . . . . . . . . . . . . . . . . . . . . . 972 18.3 Startinga Thread in Detail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 973 18.3.1 async() in Detail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 974 18.3.2 Futuresin Detail. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 975 18.3.3 Shared Futuresin Detail. . . . . . . . . . . . . . . . . . . . . . . . . . . 976 18.3.4 Classstd::promise in Detail . . . . . . . . . . . . . . . . . . . . . . . 977 18.3.5 Classstd::packaged_task in Detail . . . . . . . . . . . . . . . . . . . 977 18.3.6 Classstd::thread in Detail . . . . . . . . . . . . . . . . . . . . . . . . 979 18.3.7 Namespace this_thread . . . . . . . . . . . . . . . . . . . . . . . . . 981 18.4 Synchronizing Threads, or the Problem of Concurrency . . . . . . . . . . . . . . 982 18.4.1 Bewareof Concurrency! . . . . . . . . . . . . . . . . . . . . . . . . . . 982 18.4.2 The Reason for the Problem of Concurrent Data Access . . . . . . . . . . 983 18.4.3 WhatExactlyCan GoWrong (the Extent of the Problem) . . . . . . . . . 983 18.4.4 The Features to Solvethe Problems . . . . . . . . . . . . . . . . . . . . . 987 Contents xxi 18.5 Mutexesand Locks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 989 18.5.1 UsingMutexesand Locks . . . . . . . . . . . . . . . . . . . . . . . . . . 989 18.5.2 Mutexesand Locks in Detail . . . . . . . . . . . . . . . . . . . . . . . . 998 18.5.3 Calling Oncefor MultipleThreads . . . . . . . . . . . . . . . . . . . . . 1000 18.6 ConditionVa r i a b l e s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1003 18.6.1 Purposeof ConditionVa r i a b l e s . . . . . . . . . . . . . . . . . . . . . . . 1003 18.6.2 A First Complete Example for ConditionVa r i a b l e s . . . . . . . . . . . . 1004 18.6.3 UsingConditionVa r i a b l e s to Implement a Queue for MultipleThreads . . 1006 18.6.4 ConditionVa r i a b l e s in Detail . . . . . . . . . . . . . . . . . . . . . . . . 1009 18.7 Atomics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1012 18.7.1 Example of UsingAtomics . . . . . . . . . . . . . . . . . . . . . . . . . 1012 18.7.2 Atomicsand TheirHigh-Level Interface in Detail . . . . . . . . . . . . . 1016 18.7.3 The C-StyleInterface of Atomics . . . . . . . . . . . . . . . . . . . . . . 1019 18.7.4 The Low-Level Interface of Atomics . . . . . . . . . . . . . . . . . . . . 1019 19 Allocators 1023 19.1 UsingAllocatorsas an Application Programmer . . . . . . . . . . . . . . . . . . 1023 19.2 A User-DefinedAllocator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1024 19.3 UsingAllocatorsas a LibraryProgrammer . . . . . . . . . . . . . . . . . . . . . 1026 Bibliography 1031 Newsgroups and Forums . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1031 Books and Web Sites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1032 Index 1037 This page intentionally left blank

56,677

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧