第十七章 标准库特殊设施
17.1
tuple<int, int, int> t{1, 2, 3}; tuple<int, int, int> t1(1, 2, 3);
17.2
tuple<string, vector<string>, pair<string, int>> t;
17.4
typedef tuple<vector<Sales_data>::size_type, vector<Sales_data>::const_iterator, vector<Sales_data>::const_iterator> tup; vector<tup> findBook(const vector<vector<Sales_data>> &files, const string &book) { vector<tup> vec; for (auto it = files.cebegin(); it != files.cend(); ++it) { auto found = equal_range(it->cbegin(), it->cend(), book, compareIsbn); if (found.first != found.second) vec.push_back(make_tuple(it - files.cbegin(), found.first, found.second)); } return vec; }
17.5
typedef pair<vector<Sales_data>::size_type, pair<vector<Sales_data>::const_iterator, vector<Sales_data>::const_iterator>> Pair; vector<Pair> findbook_pair(const vector<vector<Sales_data>> &files, const string &book) { vector<Pair> ret; for (auto it = files.cbegin(); it != files.cend(); ++it) { auto found = equal_range(it->cbegin(), it->cend(), book, compareIsbn); if (found.first != found.second) ret.push_back(make_pair(it - files.cbegin(), make_pair(found.first, found.second))); } return ret; }
17.6
struct matches_struct { vector<Sales_data>::size_type st; vector<Sales_data>::const_iterator first; vector<Sales_data>::const_iterator second; matches_struct(vector<Sales_data>::size_type s, vector<Sales_data>::const_iterator fi, vector<Sales_data>::const_iterator se): st(s), first(fi), second(se) {} }; vector<matches_struct> findbook_struct(const vector<vector<Sales_data>> &f, const string &book) { vector<matches_struct> ret; for (auto it = f.cbegin(); it != f.cend(); ++it) { auto found = equal_range(it->cbegin(), it->cend(), book, compareIsbn); if (found.first != found.second) ret.push_back(matches_struct(it - f.cbegin(), found.first, found.second)); } return ret; }
17.7
倾向于书中的版本,更简洁、灵活
17.8
创建的是使用Sales_data默认构造函数创建的对象
这样的结果就是书名为空,其他数据为0
17.9
(a):后六位为100000,前面58位全为0
(b):后七位为1010101,前面25位全为0
(c):若bstr包含的字符数多于8位,bv只包含bstr的前八位;否则,bv的前几位将被置零。当然,若出现非0/1的字符,会抛出异常
17.10
bitset<22> b; int a[] = {1, 2, 3, 5, 8, 13, 21}; for (auto &i : a) b.set(i); cout << b << endl;
17.11
template <unsigned N> class test { public: test() = default; test(unsigned long long &u): b(u) { } test(string &s): b(s) { } private: bitset<N> b; }; int main() { test<10> t1; test<10> t2; }
17.12
void set_pos(size_t pos, bool bl = true) //问题编号,真假解答的值 { b.set(pos, bl); }
17.13
#include <iostream> #include <string> #include <bitset> using namespace std; template <unsigned N> class test { public: test() = default; test(unsigned long long &u): b(u) { } test(string &s): b(s) { } void set_pos(size_t pos, bool bl = true) //问题编号,真假解答的值 { b.set(pos, bl); } int get_score(bitset<N> &b1) { auto bb = b ^ b1; bb.flip(); return bb.count(); } private: bitset<N> b; }; bitset<10> set_ans() { string s; cout << "请输入标准答案:"; cin >> s; bitset<10> ans(s); return ans; } int main() { bitset<10> ans = set_ans(); //正确答案 test<10> t; int cnt = 0, pos; cout << "请输入你认为正确的位置:"; while (cin >> pos && cnt++ <= 10) { t.set_pos(pos); } cout << "正确的答题数为:" << t.get_score(ans) << endl; return 0; }
17.14
#include <iostream> #include <string> #include <regex> using namespace std; int main() { try { regex r("[[:alnum:]+", regex::icase); } catch (regex_error e) { cout << e.what() << "\ncode: " << e.code() << endl; } return 0; }
17.15
#include <iostream> #include <string> #include <regex> using namespace std; int main() { regex r("[[:alpha:]]*[^c]ei[[:alpha:]]*", regex::icase); string s; cout << "Please input a word(enter 'q' to quit!): " << endl; while(cin >> s && s != "q") { if(std::regex_match(s, r)) cout << s << " is okay!" << endl; else cout << s << " is not okay!" <<endl; cout << "Please input a word(enter 'q' to quit!): " << endl; } return 0; }
17.16
只有一个单词为三个字符时才匹配
17.28
vector<unsigned> func() { vector<unsigned> vec; static default_random_engine e; static uniform_int_distribution<unsigned> u; for (auto i = 0; i != 10; ++i) vec.push_back(u(e)); return vec; }
17.29
vector<unsigned> func(unsigned seed) { vector<unsigned> vec; static default_random_engine e(seed); static uniform_int_distribution<unsigned> u; for (auto i = 0; i != 10; ++i) vec.push_back(u(e)); return vec; }
17.30
vector<unsigned> func(unsigned seed, unsigned mmin, unsigned mmax) { vector<unsigned> vec; static default_random_engine e(seed); static uniform_int_distribution<unsigned> u(mmin, mmax); for (auto i = 0; i != 10; ++i) vec.push_back(u(e)); return vec; }
17.31
每次循环中,分布返回的总是同一个值
17.32
到右大括号时,resp对象将被释放
17.33
#include <iostream> #include <string> #include <random> using namespace std; string trans1(const string &s1, const string &s2) { string s = s1 + s2; return s; } string trans2(const string &s1, const string &s2) { string s = s1; return s; } int main() { default_random_engine e; bernoulli_distribution b; bool bl = b(e); string ss; if (bl) ss = trans1("love", "kzw"); else ss = trans2("love", "kzw"); cout << ss << endl; return 0; }
17.34
#include <iostream> #include <string> #include <iomanip> using namespace std; int main() { int i = 20; double d = 10, d1 = 30.1415926; bool bl = true, bp = false; cout << boolalpha << bl << " " << bp << " " << noboolalpha << bl << endl; cout << showbase << 20 << " " << oct << 20 << " " << hex << 20 << endl << dec << noshowbase; cout << setbase(8) << 20 << " " << 20 << endl; cout << showpoint << d << noshowpoint << endl; cout << showpos << i << noshowpos << endl; cout << d1 << " " << scientific << d1 << " " << fixed << d1 << " " << d1 << endl; cout << setw(10) << d1 << " " << setprecision(7) << d1 << endl; return 0; }
17.35
cout << uppercase << hexfloat << sqrt(2.0) << endl; cout << nouppercase << defaultfloat;
17.36
double d0 = 3.14, d1 = 31.415, d2 = 520.1314, d3 = 11.11223344; cout << "d0: " << setw(12) << d0 << " next col" << endl << "d1: " << setw(12) << d1 << " next col" << endl << "d2: " << setw(12) << d2 << " next col" << endl << "d3: " << setw(12) << d3 << " next col" << endl;
17.37
当未遇到分隔符时,若已读取的字符数超过了size - 1时(下面的程序就是9),就会结束循环
但若遇到分隔符时,前面读取的字符数还未超过size - 1时,会打印该行(如空行),然后开始读取开始下一行(读取的字符数会清0)
小结:要想读取完整,那么字符数组和size都要设置的足够大,大到每一次读取可以遇到分隔符。
#include <iostream> #include <fstream> #include <string> #include <iomanip> #include <cmath> using namespace std; int main() { ifstream in("data.txt"); char str[100]; do{ in.getline(str, 10, '\n'); cout << str << endl; } while(in); return 0; }
17.38
#include <iostream> #include <fstream> #include <string> #include <iomanip> #include <cmath> using namespace std; int main() { ifstream in("data.txt"); char str[100]; do{ //谨记上一题的教训,size应设置得足够大,至少大于最长的单词的长度 in.getline(str, 30, ' '); //单词应该以空格分隔 cout << str << endl; } while(in); return 0; }
17.39
#include <iostream> #include <fstream> #include <string> #include <iomanip> #include <cstdlib> #include <cmath> using namespace std; int main() { fstream ios("data.txt", fstream::ate | fstream::in | fstream::out); if (!ios) { cerr << "Unable to open file!" << endl; return EXIT_FAILURE; } auto endMark = ios.tellg(); ios.seekg(0, fstream::beg); string line; size_t cnt = 0; while (ios && ios.tellg() != endMark && getline(ios, line)) { cnt += line.size() + 1; auto mark = ios.tellg(); // ifstream::pos_type mark = ios.tellg(); ios.seekp(0, fstream::end); ios << cnt; if (mark != endMark) ios << " "; ios.seekg(mark); } ios.seekp(0, fstream::end); ios << endl; return 0; }