C++ Prime第十七章
练习17.1
tuple<int, double, long> t(10, 20, 30);
tuple<size_t, size_t, int> t2{10,20,30};
tuple<int, long, double> t3;
t3 = make_tuple(10, 20, 30);
练习17.2
tuple<string, vector<string>, pair<string, int>> t4;
练习17.3
tuple是一个快速而且随意的数据结构
练习17.4
typedef tuple<vector<Sales_data>::size_type,
vector<Sales_data>::const_iterator,
vector<Sales_data>::const_iterator> matches;
vector<matches> findBook(const vector<vector<Sales_data>>& files, const string& book)
{
vector<matches> 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_tuple(it - files.begin(), found.first, found.second));
}
return ret;
}
练习17.5
typedef pair<vector<string>::size_type, pair<vector<string>::const_iterator, vector<string>::const_iterator> > matches;
练习17.6
用struct或者class
练习17.7
tuple版本,简单易用
练习17.8
no 会
练习17.9
(a) 高位32位为0,低位为32代表的位模式
(b)前置0加上1010101对应的二进制,注意,这不是个二进制,这是个10进制,虽然它仅有1和0.
©bstr长度大于8,则截取8个,小于8,高位补0
练习17.10
int main()
{
bitset<32> b1(1U);
bitset<32> btmp1;
btmp1.set(0);
if (b1 == btmp1)
cout << "yes" << endl;
bitset<32> b2(2U);
bitset<32> btmp2;
btmp2.set(1);
if (b2 == btmp2)
cout << "yes" << endl;
bitset<32> b21(21U);
bitset<32> btmp21;
btmp21.set(0);
btmp21.set(2);
btmp21.set(4);
if (b21 == btmp21)
cout << "yes" << endl;
return 0;
}
练习17.11
class Obj {
bitset<10> ans;
};
只需要把bitset设置为100,即可记录100个问题的真假测验的解答
练习17.12
class Obj {
public:
bitset<10> ans;
};
Obj obj;
void check(int n, bool flag)
{
obj.ans.set(n, flag);
}
int main()
{
for (auto i = 0; i < 10; ++i)
if (i % 2)
check(i, true);
else
check(i, false);
Obj right;
right.ans.flip();
right.ans.reset(3);
right.ans.reset(7);
int sum = 0;
for (auto i = 0; i < 10; ++i)
sum += (obj.ans[i] & right.ans[i]);
cout << sum << endl;
return 0;
}
练习17.13
上面
练习17.14
不会.看不懂错误码的解释是啥意识.
练习17.15
int main()
{
string pattern("[^c]ei");
pattern = "[[:alpha:]]*" + pattern + "[[:alpha:]]*";
regex r(pattern, regex_constants::ECMAScript);
smatch results;
string test_str;
cout << "请输入测试单词: ";
while (cin >> test_str) {
if (regex_search(test_str, results, r))
cout << "符合要求" << endl;
else
cout << "不符合要求\n";
cout << "请输入下一个测试单词: ";
}
return 0;
}
练习17.16
只能匹配三个字符.
string pattern("[^c]ei");
//pattern = "[[:alpha:]]*" + pattern + "[[:alpha:]]*";
//regex r(pattern, regex_constants::ECMAScript);
regex r(pattern);
smatch results;
string test_str;
cout << "请输入测试单词: ";
while (cin >> test_str) {
if (regex_search(test_str, results, r))
cout << "符合要求" << endl;
else
cout << "不符合要求\n";
cout << "请输入下一个测试单词: ";
}
练习17.17
string pattern("[^c]ei");
pattern = "[[:alpha:]]*" + pattern + "[[:alpha:]]*";
regex r(pattern, regex_constants::ECMAScript | regex_constants::icase);
smatch results;
string test_str;
string file;
while (getline(cin, file)) {
for (sregex_iterator it(file.begin(), file.end(), r), end_it; it != end_it; ++it)
{
auto pos = it->prefix().length();
pos = pos > 40 ? pos - 40 : 0;
cout << it->prefix().str().substr(pos)
<< "\n\t\t>>> " << it->str() << " <<<\n"
<< it->suffix().str().substr(0, 40)
<< endl;
}
}
练习17.18
看不懂题.
练习17.19
因为即使m[4]不匹配,m[4].str()也会返回空串,而不会报错,这样直接使用时安全的.
练习17.20
bool valid(const smatch& m) {
if (m[1].matched)
return m[3].matched && (m[4].matched == 0 || m[4].str() == " ");
else
return !m[3].matched && m[4].str() == m[6].str();
}
int main()
{
string phone =
"(\\()?(\\d{3})(\\))?([- .])?(\\d{3})([-. ]?)(\\d{4})";
regex r(phone);
smatch m;
string s;
while (getline(cin, s)) {
for (sregex_iterator it(s.begin(), s.end(), r), end_it; it != end_it; ++it) {
if (valid(*it))
cout << "valid: " << it->str() << endl;
else
cout << "not valid: " << it->str() << endl;
}
}
return 0;
}
练习17.21
同上
练习17.22
string phone =
"(\\()?(\\d{3})(\\))?([-|.|\\s{0,}])?(\\d{3})([-|.|\\s{0,} ]?)(\\d{4})";
练习17.23
string id = "(\\d{5})([-]?)(\\d{4})?";
regex r(id);
smatch res;
string test_str;
if (regex_search(test_str, res, r))
if (valid_id(res))//如果没有后4位数字,那么也不应该有短横线
;//do something
练习17.24
string phone =
"(\\()?(\\d{3})(\\))?([-|.|\\s{0,}])?(\\d{3})([-|.|\\s{0,} ]?)(\\d{4})";
regex r(phone);
smatch m;
string s;
string fmt = "$2.$5.$7";
while (getline(cin, s)) {
//for (sregex_iterator it(s.begin(), s.end(), r), end_it; it != end_it; ++it) {
// if (valid(*it))
// cout << "valid: " << it->str() << endl;
// else
// cout << "not valid: " << it->str() << endl;
cout << regex_replace(s, r, fmt) << endl;
}
练习17.25
while (getline(cin, s)) {
if(regex_search(s,m,r))
cout << m.format(fmt) << endl;
}
练习17.26
略
练习17.27
string id = "(\\d{5})([-]?)(\\d{4})?";
regex r(id);
smatch res;
string test_str;
string fmt = "$1-$3";
while (getline(cin, test_str)) {//有bug
cout << regex_replace(test_str, r, fmt) << endl;
}
练习17.28
unsigned int gen() {
static default_random_engine e(time(0));
static uniform_int_distribution<unsigned> u;
return u(e);
}
int main()
{
cout<<gen()<<endl;
return 0;
}
练习17.29
unsigned int gen(unsigned int n) {
static default_random_engine e(n);
static uniform_int_distribution<unsigned> u;
return u(e);
}
int main()
{
unsigned int n;
while(cin >> n)
cout<<gen(n)<<endl;
return 0;
}
练习17.30
unsigned int gen(unsigned int n,int min,int max) {
static default_random_engine e(n);
static uniform_int_distribution<unsigned> u(min,max);
return u(e);
}
int main()
{
cout << "输入 种子,最小值,最大值: ";
unsigned int n;
int min, max;
while (cin >> n >> min >> max)
{
cout << gen(n,min,max) << endl;
cout << "输入下一个种子,最小值,最大值: ";
}
return 0;
}
练习17.31
某一方现行,则永远先行.
练习17.32
作用域问题,会报错.
练习17.33
这题加个界面可以做个小游戏了,留着完成或者不完成.
练习17.34
会用
练习17.35
cout << uppercase << hex <<showbase<<hexfloat<< sqrt(2.0) << endl;
练习17.36
cout << fixed << setprecision(10) << showpoint;
cout << sqrt(2.0) << endl << sqrt(3.0) << endl << sqrt(4.0) << endl;
练习17.37
const string PATH = "word.txt";
int main()
{
ifstream fin(PATH);
char buf[30];
while (fin.getline(buf, 100))//某行超过29字符会崩溃
cout << fin.gcount() << endl;
return 0;
}
练习17.38
不会又读又写.
练习17.39
int main()
{
fstream finout(PATH,fstream::in|fstream::out|fstream::ate);
if (!finout)
{
cout << "error" << ends;
return EXIT_FAILURE;
}
auto end_mark = finout.tellg();
finout.seekg(0, fstream::beg);
size_t cnt = 0;
string line;
while (finout && finout.tellg() != end_mark && getline(finout, line)) {
cnt += line.size() + 1;
auto mark = finout.tellg();
finout.seekp(0, fstream::end);
finout << cnt;
if (mark != end_mark) finout << " ";
finout.seekg(mark);
}
finout.seekp(0, fstream::end);
finout << "\n";
return 0;
}