1 / 7

C++ Specialized Library Facilities: Tuples, Bitsets, Regular Expressions, Random Number Generation

An overview of specialized library facilities in C++, including tuples, bitsets, regular expressions, and random number generation.

myarber
Download Presentation

C++ Specialized Library Facilities: Tuples, Bitsets, Regular Expressions, Random Number Generation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. #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

  3. 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

  4. #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

  5. #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

  6. #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

  7. #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

More Related