C++11中std::initializer_list的使用

initializer_list是一种标准库类型,用于表示某种特定类型的值的数组。和vector一样,initializer_list也是一种模板类型,定义initializer_list对象时,必须说明列表中所含元素的类型。和vector不一样的是,initializer_list对象中的元素永远是常量值,我们无法改变initializer_list对象中元素的值。

initializer_list可以作用于可变数量的实参:有时我们无法提前预知应该向函数传递几个实参。为了编写能处理不同数量实参的函数,C++11新标准提供了两种主要的方法:如果所有的实参类型相同,可以传递一个名为initializer_list的标准库类型;如果实参的类型不同,我们可以编写一种特殊的函数,也就是所谓的可变参数模板。

作用于initializer_list对象的begin和end操作类似于vector对应的成员。begin()成员提供一个指向列表首元素的指针,end()成员提供一个指向列表尾元素的指针。

含有initializer_list形参的函数也可以同时拥有其他形参。

类模板initializer_list用于访问初始化列表(initialization list),列表元素的数据类型为const T.编译器从花括号(brace)封闭的、元素由逗号分隔开的初始化列表自动构造initializer_list模板类, 例如:

auto il = {10,20, 30}; // the type of il is an initializer_list<int>
类模板initializer_list引用(refer to)但不包含这个这个列表的元素。复制这个initializer_list对象将引用到同一个基础列表而不是引用到基础列表的新的拷贝。std::initializer_list并不是一个容器,不要用它传递期望长期存储的值。构造函数的形参如果为一个initializer_list模板类,则这种特殊的构造函数称为初始化器列表构造函数(initializer_list constructor),例如:

struct myclass {
  myclass (int,int);
  myclass (initializer_list<int>);
  /* definitions ... */
};
myclass foo {10,20};  // calls initializer_list constructor
myclass bar (10,20);  // calls first constructor 

仅可以从braced-init-list推导出initializer_list<T>。这一过程中,编译器自动查询哪些构造函数可以用initializer_list<T>作为参数,并据此确定initializer_list的模板参数T的类型,从而对braced-init-list做类型转换。

C++11标准明确规定不能由模板参数推导出对应实参std::initializer_list的类型。

C++还有一种特殊的形参类型(即省略符),可以用它传递可变数量的实参。

省略符形参是为了便于C++程序访问某些特殊的C代码而设置的,这些代码使用了名为varargs的C标准库功能。通常,省略符形参不应用于其他目的。省略符形参只能出现在形参列表的最后一个位置。

  std::initializer_list:This type is used to access the values in a C++ initialization list, which is a list of elements of type const T.

下面是从其它文章中copy的std::initializer_list测试代码,详细内容介绍可以参考对应的reference:

#include "initializer_list.hpp"
#include <initializer_list>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>

///
// reference: http://www.cplusplus.com/reference/initializer_list/initializer_list/
struct myclass {
	std::string str;
	myclass(std::initializer_list<int> args) {
		std::stringstream ss;
		std::initializer_list<int>::iterator it;  // same as: const int* it
		for (it = args.begin(); it != args.end(); ++it) ss << ' ' << *it;
		str = ss.str();
	}
};

template<class T>
struct simple_container {
	T * data;
	unsigned n;
	simple_container(std::initializer_list<int> args) {
		data = new T[args.size()];
		n = 0;
		for (T x : args) { data[n++] = x; }
	}
	T* begin() { return data; }
	T* end() { return data + n; }
};

template<class T>
void print_list(std::initializer_list<T> il)
{
	for (const T* it = std::begin(il); it != std::end(il); ++it) std::cout << ' ' << *it;
	std::cout << '\n';
}

int test_initializer_list_1()
{
{
	std::initializer_list<int> mylist;
	mylist = { 10, 20, 30 };
	std::cout << "mylist contains:";
	for (int x : mylist) std::cout << ' ' << x;
	std::cout << '\n';
}

{
	myclass myobject{ 10, 20, 30 };
	std::cout << "myobject contains:" << myobject.str << '\n';
}

{
	simple_container<int> myobject{ 10, 20, 30 };
	std::cout << "myobject contains:";
	for (int x : myobject) std::cout << ' ' << x;
	std::cout << '\n';
}

{
	print_list({ 10, 20, 30 });
}

{
	// 编译器从花括号(brace)封闭的、元素由逗号分隔开的初始化列表自动构造initializer_list模板类
	auto il = { 10, 20, 30 };
	for (auto i : il)
		std::cout << i << "    ";
	std::cout << std::endl;
}

	return 0;
}


// reference: http://en.cppreference.com/w/cpp/utility/initializer_list
template <class T>
struct S {
	std::vector<T> v;
	S(std::initializer_list<T> l) : v(l) {
		std::cout << "constructed with a " << l.size() << "-element list\n";
	}
	void append(std::initializer_list<T> l) {
		v.insert(v.end(), l.begin(), l.end());
	}
	std::pair<const T*, std::size_t> c_arr() const {
		return{ &v[0], v.size() };  // copy list-initialization in return statement
		// this is NOT a use of std::initializer_list
	}
};

template <typename T>
void templated_fn(T) {}

int test_initializer_list_2()
{
	S<int> s = { 1, 2, 3, 4, 5 }; // copy list-initialization
	s.append({ 6, 7, 8 });      // list-initialization in function call

	std::cout << "The vector size is now " << s.c_arr().second << " ints:\n";

	for (auto n : s.v)
		std::cout << n << ' ';
	std::cout << '\n';

	std::cout << "Range-for over brace-init-list: \n";

	for (int x : {-1, -2, -3}) // the rule for auto makes this ranged-for work
		std::cout << x << ' ';
	std::cout << '\n';

	auto al = { 10, 11, 12 };   // special rule for auto

	std::cout << "The list bound to auto has size() = " << al.size() << '\n';

	//    templated_fn({1, 2, 3}); // compiler error! "{1, 2, 3}" is not an expression,
	// it has no type, and so T cannot be deduced
	templated_fn<std::initializer_list<int>>({ 1, 2, 3 }); // OK
	templated_fn<std::vector<int>>({ 1, 2, 3 });           // also OK

	return 0;
}

GitHub:  https://github.com/fengbingchun/Messy_Test

  • 14
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
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
回答: c++std::initializer_list是一个类模板,用于创建一个初始化器列表,它可以用来初始化标准容器或特定的自定义类对象。\[2\]它的底层机制是指针空间,类似于一个存放了一堆元素的列表。\[2\]你可以使用std::initializer_list来遍历列表的元素,通过使用begin()和end()函数来获取指向首元素和末尾元素后一位置的指针,然后使用循环来遍历列表的元素。\[1\]例如,你可以使用for循环来遍历一个std::initializer_list<int>对象,并使用*it来访问每个元素。\[2\]另外,你也可以使用size()函数来获取初始化器列表元素的数量。\[3\] #### 引用[.reference_title] - *1* *3* [C++std::initializer_list详解](https://blog.csdn.net/weixin_43165135/article/details/127927352)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [现代C++std::initializer_list的特性分析](https://blog.csdn.net/a574780196/article/details/122493579)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值