《C++ Primer第五版》读书笔记(15)--Specialized Library Facilities

本章没啥语法上的新东西,不过random number与regular expressions对俺还是有吸引力滴。

17.1 The tuple Type
A tuple can have any number of members. Each distinct tuple type has a fixed number of members, but the number of members in one tuple type can differ from the number of members in another.
A tuple is most useful when we want to combine some data into a single object but do not want to bother to define a data structure to represent those data.

Defining and Initializing tuples
tuple<size_t,size_t, size_t> threeD; // all three members set to 0
tuple<string, vector<double>, int, list<int>> someVal("constants", {3.14, 2.718}, 42, {0,1,2,3,4,5});


Accessing the Members of a tuple
auto book = get<0>(item);  // returns the first member of item
auto cnt = get<1>(item);   // returns the second member of item
auto price = get<2>(item)/cnt; // returns the last member of item
get<2>(item) *= 0.8;  // apply 20% discount



Using a tuple to Return Multiple Values
A common use of tuple is to return multiple values from a function.


17.2 The bitset Type


The standard library defines the bitsetclass to make it easier to use bit operations and possible to deal with collections of bits that are larger than the longest integral type.


17.2.1 Defining and Initializing bitset
bitset<32>bitvec(1U); // 32 bits; low-order bit is 1, remaining bits are 0. The bits
starting at 0 are referred to as the low-order bits, and those ending at 31are
referred to as high-order bits.
 
Initializing a bitset from an unsigned Value
// bitvec1 is smaller than the initializer; high-order bits from the initializer are discarded
bitset<13> bitvec1 (0xbeef);  // bits are 1111011101111
// bitvec2 is larger than the initializer; high-order bits in bitvec2 are set to zero
bitset<20> bitvec2(0xbeef);  // bits are 00001011111011101111
// on machines with 64-bit long long 0ULL is 64 bits of 0, so ~0ULL  is 64 ones
bitset<128> bitvec3(~0ULL); // bits 0 ... 63 are one; 63 ... 127 are zero


Initializing a bitset from a string
bitset<32> bitvec4("1100"); // bits 2 and 3 are 1, all others are 0
17.2.2 Operations on bitset

17.3 Regular Expressions
A regular expression is a way of describing a sequence of characters. Regular expressions are a stunningly powerful computational device.
The regular-expression library provides a collection of classes and functions: The regex class manages regular expressions written in one of several common regular expression languages. The match classes hold information about a specific match.
These classes are used by the regex_search and regex_match functions. These functions take a regex object and a character sequence and detect whether the regular expression in that regex matches the given character sequence. The regex iterator types are iterator adaptors that use regex_search to iterate through an input sequence and return each matching subsequence. There is also a regex_replace function that lets us replace the matched part of a given input sequence with a specified alternative.


17.4 Random Numbers
The random-number library is a collection of random-number engines and distribution classes. A random-number engine returns a sequence of uniformly distributed integral values. The library defines several engines that have different performance characteristics. The default_random_engine is defined as the engine that should be suitable for most casual uses. The library also defines 20 distribution types. These distribution types use an engine to deliver random numbers of a specified type in a given range that are distributed according to a specified probability distribution.
Best Practices:C++programs should not use the library rand function. Instead, they should use the default_random_engine along with an appropriate distribution object.
default_random_engine e;  // generates random unsigned integers
for (size_t i = 0; i < 10; ++i)
// e() "calls" the object to produce the next random number
cout << e() << " ";



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值