70 likes | 89 Views
An overview of specialized library facilities in C++, including tuples, bitsets, regular expressions, and random number generation.
E N D
Overview of C++ Specialized Library Facilities • A (C++11) tuple is a template type that can have any number of members, possibly of varying types • A (C++11) bitset is creates a fixed sized container for bitwise data manipulation • The (C++11) Regular Expression Library allows for pattern matching, replacement, and search • (C++11) Random Number Generation now supports different kinds of engines and distributions
#include <tuple> tuple <size_t, size_t, size_t> 3D; tuple <string, vector<double>, int, list<int>> someTuple (“name”, {1.2, 3.4}, 4, {2,2, 3, 4 5, 6}); tuple <size_t, size_t, size_t> 3D (0, 0, 0); // cannot do this: // tuple <size_t, size_t, size_t> // 3D = (0, 0, 0); auto studentOne = make_tuple(1234, “Student Name”); Create a tuple with coordinates of 3D array Tuple can contain multiple types Tuple constructor is explicit, so direct initialization is required make_tuple can be used to create a tuple tuple
typedef tuple <int, string> student; vector<student> classList; classList.pushback(make_tuple (1234, “Jane Doe”)); classList.pushback(make_tuple (1000, “John Doe”)); bool b = studentOne < studentTwo; for (const auto &oneStu : classList) { cout << oneStu; } Create a tuple type Can be used in conjunction with other containers, such as vector Can also compare 2 tuples if operator< is defined for each of its members Or, if you write your own operator< Can write stream operators for tuples, put tuples in vectors, etc. ostream &operator<< (ostream& out, const student &s) { out << get<0>(s) << get<1>(s); return out; } tuple, continued
#include <bitset> bitset <16> block1(7U) upper bits lower bits 31 0 (0000000000000111) 13 - 0’s bitset <16> block2(“1110”) (0000000000001110) 12 - 0’s unsigned long ulong = block2.to_ulong(); block1.flip(0) block2 cout << block1 A bitset is similar to an array: has fixed unnamed elements starting at position 0 A bitset can be initialized An unsigned value is converted to bits, filling lower bits first Or, a string is converted to bits, again filling lower bits first Value of a bitset can be retrieved by converting back to unsigned long ostream operator<< will print bit values bitset
#include <regex> regex regex_match regex_search regex_replace sregex_iterator smatch ssub_match Regular Expressions are used to describe sequences of characters Create regular expression pattern Find perfect match Search for a pattern Change sequences matching pattern Iterator for a regular expression matching results Sub-expression result regex
#include <regex> #include <string> string test = “Hello World”; regex pattern(“(a|e|i|o|u)”); if (regex_search(test, pattern) { cout << “found ” << test;} if (regex_match(test, pattern) { cout << “matched ” << test;} else { cout << “no matches ”;} regex pattern2 (“([[:alpha:]\\s]*)(a|e|i|o|u)([[:alpha:]\\s]*))“, regex::icase); if (regex_match(test, pattern2) { cout << “matched “ << test; } Output (search for vowels) found Hello World Output (exact match for vowels) no matches Output (exact match for alphabetic) matched Hello World regex::icase [[:alpha:]] – alphabetic character (from a – z or A-Z) (a|e|i|o|u) – vowels regex, continued
#include <random> for (size_t i = 0; i<3; ++i) { default_random_engine e; e(); } for (size_t i = 0; i < 3; ++i) { default_random_engine e(time(0)+i); e(); } default_random_engine e(time(0)); for (size_t i = 0; i < 3; i++) { e(); } Random Number Engines • Random number generator • Important to seed the engine • Output - wrong • 1129438 • 1129438 • Output – may or may not be ok • 1000234 • 8791423 • Output – better approach • 4235720 • 3547526