不得不说16章的STL初学者噩梦 看到算法那直接省略跳过了
16.10.1

#include <iostream>
#include <string>
using namespace std;
bool Petest(const string &);
int main(void)
{cout << "otto是否是回文: " << (Petest("otto") ? "是" : "不是") << endl;return 0;
}
bool Petest(const string & s)
{for(int i = 0, j = s.size() - 1; i < s.size() / 2; i++, j--){if(s[i] == s[j])continue;return false;}return true;
}

16.10.2

#include <iostream>
#include <cctype>
using namespace std;
bool Petest(const char *);
int main(void)
{cout << "Madam, I'm Adam是否是回文: " << (Petest("Madam, I'm Adam") ? "是" : "不是") << endl;return 0;
}
bool Petest(const char * s)
{char temp[30];int i, j, l;for(i = 0, j = 0; s[i]; i++){if(isalpha(s[i])){temp[j] = tolower(s[i]);j++;}}for(i = 0, l = j - 1; i < j / 2; i++, l--){if(temp[i] == temp[l])continue;return false;}return true;
}

16.10.3
先康康16.3分析

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cctype>
using namespace std;
const int NUM = 26;
const string wordlist[NUM] =
{"apiary", "beetle", "cereal", "danger", "ensign", "florid", "garage","health", "insult", "jackal", "keeper", "loaner", "manage", "nonce","onset", "plaid", "quilt", "remote", "stolid", "train", "useful","valid", "whence", "xenon", "yearn", "zippy"
};
int main(void)
{srand(time(nullptr));char play;//此变量判断是否要继续游戏cout << "Will you play a word game? <y/n> ";cin >> play;play = tolower(play);//将输入转换为小写while(play == 'y')//是否开始or继续游戏{string target = wordlist[rand() % NUM];//随机抽取一个单词int length = target.length();string attempt(length, '-');//创建一个与需猜测单词相同长度的string对象 并将该对象用'-'初始化string badchars;//猜错的字母放进此对象中int guesses = 6;//一共有6次错误机会cout << "Guess my secret word. It has " << length<< " letters, and you guess\n"<< "one letter at a time. Yout get " << guesses<< " wrong guesses.\n";//显示一些基本信息cout << "Yout word: " << attempt << endl;//显示我们猜对了几个字母while(guesses > 0 && attempt != target)//还有错误机会并且单词还没猜完继续循环{char letter;//猜测的字母cout << "Guess a letter: ";cin >> letter;if(badchars.find(letter) != string::npos || attempt.find(letter) != string::npos){//如果在猜错单词对象中找到此字母or在才对单词对象中找到此字母cout << "You already guessed that. Try again.\n";continue;//打印猜过了的提示信息回到循环开始}int loc = target.find(letter);//看看此字母是否在我们所猜的单词中if(loc == string::npos)//如果不在{cout << "Oh, bad guess!\n";guesses--;badchars += letter;}else//如果在{cout << "Good guess!\n";attempt[loc] = letter;//让我们猜测的存储对象的指定索引成为对的字母loc = target.find(letter, loc + 1);//此时我们继续从第一次找到的索引加一的位置看看是否还有该字母while(loc != string::npos){//如果还有同样字母继续添加继续找attempt[loc] = letter;loc = target.find(letter, loc + 1);}}cout << "Your word: " << attempt << endl;//更新一下单词显示if(attempt != target)//如果还没猜完{if(badchars.length() > 0)//有猜错的字母cout << "Bad choices: " << badchars << endl;cout << guesses << " bad guesses left\n";}}if(guesses > 0)//如果猜测机会还有cout << "That's right!\n";elsecout << "Sorry, the word is " << target << ".\n";cout << "Will you play a word game? <y/n> ";cin >> play;play = tolower(play);}cout << "Bye\n";return 0;
}

main.cpp

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cctype>
#include <fstream>
#include <vector>
using namespace std;
int main(void)
{cout << "请输入文件名字: ";string s;getline(cin, s);ifstream out(s);if(!out.is_open())return 0;vector<string> arr;int NUM = 0;while(!out.eof()){getline(out, s);arr.push_back(s);NUM++;}out.clear();srand(time(nullptr));char play;//此变量判断是否要继续游戏cout << "Will you play a word game? <y/n> ";cin >> play;play = tolower(play);//将输入转换为小写while(play == 'y')//是否开始or继续游戏{string target = arr[rand() % NUM];//随机抽取一个单词int length = target.length();string attempt(length, '-');//创建一个与需猜测单词相同长度的string对象 并将该对象用'-'初始化string badchars;//猜错的字母放进此对象中int guesses = 6;//一共有6次错误机会cout << "Guess my secret word. It has " << length<< " letters, and you guess\n"<< "one letter at a time. Yout get " << guesses<< " wrong guesses.\n";//显示一些基本信息cout << "Yout word: " << attempt << endl;//显示我们猜对了几个字母while(guesses > 0 && attempt != target)//还有错误机会并且单词还没猜完继续循环{char letter;//猜测的字母cout << "Guess a letter: ";cin >> letter;if(badchars.find(letter) != string::npos || attempt.find(letter) != string::npos){//如果在猜错单词对象中找到此字母or在才对单词对象中找到此字母cout << "You already guessed that. Try again.\n";continue;//打印猜过了的提示信息回到循环开始}int loc = target.find(letter);//看看此字母是否在我们所猜的单词中if(loc == string::npos)//如果不在{cout << "Oh, bad guess!\n";guesses--;badchars += letter;}else//如果在{cout << "Good guess!\n";attempt[loc] = letter;//让我们猜测的存储对象的指定索引成为对的字母loc = target.find(letter, loc + 1);//此时我们继续从第一次找到的索引加一的位置看看是否还有该字母while(loc != string::npos){//如果还有同样字母继续添加继续找attempt[loc] = letter;loc = target.find(letter, loc + 1);}}cout << "Your word: " << attempt << endl;//更新一下单词显示if(attempt != target)//如果还没猜完{if(badchars.length() > 0)//有猜错的字母cout << "Bad choices: " << badchars << endl;cout << guesses << " bad guesses left\n";}}if(guesses > 0)//如果猜测机会还有cout << "That's right!\n";elsecout << "Sorry, the word is " << target << ".\n";cout << "Will you play a word game? <y/n> ";cin >> play;play = tolower(play);}cout << "Bye\n";return 0;
}

这章学的不是很明白 上网查了查需要哪些函数
16.10.4

#include <iostream>
#include <algorithm>
using namespace std;
int reduce(long *, int);
int main(void)
{long ar[5] = {6, 4, 9, 6, 80};int n = reduce(ar, 5);for(int i = 0; i < n; i++)cout << ar[i] << endl;return 0;
}
int reduce(long * ar, int n)
{sort(ar, ar + n);long * p = unique(ar, ar + n);return (int)(p - ar);
}

16.10.5

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
template <typename T>
int reduce(T *, int);
int main(void)
{long ar[5] = {6, 4, 9, 6, 80};int n = reduce(ar, 5);for(int i = 0; i < n; i++)cout << ar[i] << endl;string s[3] = {"nmsl", "nssb", "nmsl"};n = reduce(s, 3);for(int i = 0; i < n; i++)cout << s[i] << endl;return 0;
}
template <typename T>
int reduce(T * ar, int n)
{sort(ar, ar + n);T * p = unique(ar, ar + n);return (int)(p - ar);
}

16.10.6
这道题只要我们把自定义的Queue类改成STL的queue类就行了基本什么都不用改

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <queue>
using namespace std;
class Customer
{private:long arrive;int processtime;
public:Customer(void) { arrive = processtime = 0; }void set(long when){processtime = std::rand() % 3 + 1;arrive = when;}   long when(void) const { return arrive; }int ptime(void) const { return processtime; }
};
typedef Customer Item;
const int MIN_PER_HR = 60;
bool newcustomer(double);
int main(void)
{srand(time(nullptr));cout << "Case Study: Bank of Heather Automatic Teller\n";queue<Item> line;cout << "Enter the number of simulation hours: ";int hours;cin >> hours;long cyclelimit = MIN_PER_HR * hours;cout << "Enter the average number of customers per hour: ";double perhour;cin >> perhour;double min_per_cust;min_per_cust = MIN_PER_HR / perhour;Item temp;long turnaways = 0;long customers = 0;long served = 0;long sum_line = 0;int wait_time = 0;long line_wait = 0;for (int cycle = 0; cycle < cyclelimit; cycle++){if (newcustomer(min_per_cust)){if (line.size() == qs)turnaways++;else{customers++;temp.set(cycle);line.push(temp);}}if (wait_time <= 0 && !line.empty())    {line.pop();wait_time = temp.ptime();line_wait += cycle - temp.when();served++;           }                       if (wait_time > 0)wait_time--;sum_line += line.size(); }if (customers > 0){cout << "customers accepted: " << customers << endl;cout << "  customers served: " << served << endl;       cout << "         turnaways: " << turnaways << endl;        cout << "average queue size: ";     cout.precision(2);cout.setf(ios_base::fixed, ios_base::floatfield);cout << (double) sum_line / cyclelimit << endl;cout << " average wait time: "<< (double) line_wait / served << " minutes\n"; }elsecout << "No customers!\n";cout << "Done!\n";return 0;
}
bool newcustomer(double x)
{return (rand() * x / RAND_MAX < 1);
}

16.10.7

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> lotto(int, int);
void show(int);
int main(void)
{vector<int> x;x = lotto(51, 6);for_each(x.begin(), x.end(), show);return 0;
}
vector<int> lotto(int s, int n)
{vector<int> sum;for(int i = 1; i <= s; i++){sum.push_back(i);}random_shuffle(sum.begin(), sum.end());vector<int> temp;for(int i = 0; i < n; i++){temp.push_back(sum[i]);}return temp;
}
void show(int n)
{cout << n << endl;
}

16.10.8

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main(void)
{vector<string> Mat;cout << "Mat:" << endl;string name;for(int i = 0; i < 3; i++)//可以让他自定义输多少个 这里为了简化只输3个{cin >> name;Mat.push_back(name);}sort(Mat.begin(), Mat.end());for(int i = 0; i < 3; i++){cout << Mat[i] << endl;}vector<string> Pat;cout << "Pat:" << endl;for(int i = 0; i < 3; i++){cin >> name;Pat.push_back(name);}sort(Pat.begin(), Pat.end());for(int i = 0; i < 3; i++){cout << Pat[i] << endl;}vector<string> reslut;reslut.resize(Mat.size() + Pat.size());merge(Mat.begin(), Mat.end(), Pat.begin(), Pat.end(), reslut.begin());sort(reslut.begin(), reslut.end());unique(reslut.begin(), reslut.end());for(auto x = reslut.begin(); x != reslut.end() - 1; x++){cout << *x << endl;}return 0;
}

16.10.9

#include <iostream>
#include <vector>
#include <list>
#include <cstdlib>
#include <algorithm>
#include <ctime>
using namespace std;
const int sum = 1000000;
int main(void)
{vector<int> vi0;vector<int> vi;list<int> li;srand(time(nullptr));for(int i = 0; i < sum; i++){int temp = rand();vi0.push_back(temp);vi.push_back(temp);li.push_back(temp);}clock_t start = clock();sort(vi.begin(), vi.end());clock_t end = clock();cout << "排序vi的时间为: " << (double)(end - start) / CLOCKS_PER_SEC << endl;start = clock();li.sort();end = clock();cout << "排序li的时间为: " << (double)(end - start) / CLOCKS_PER_SEC << endl;copy(vi0.begin(), vi0.end(), li.begin());start = clock();copy(li.begin(), li.end(), vi.begin());sort(vi.begin(), vi.end());copy(vi.begin(), vi.end(), li.begin());end = clock();cout << "处理了一系列的复制排序的时间为: " << (double)(end - start) / CLOCKS_PER_SEC << endl;return 0;
}

16.10.10
这一章学的有些差 此题来自https://blog.csdn.net/weixin_41882882/article/details/82257916STL真是头给搞大了

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include<memory>using namespace std;struct Review {std::string title;int rating;double price;
};bool operator<(const shared_ptr<Review> & r1, const shared_ptr<Review> & r2);
bool worseThan(const shared_ptr<Review> & r1, const shared_ptr<Review> & r2);
bool FillReview(Review & rr);
void ShowReview(const shared_ptr<Review> & rr);
void showmenu();
bool sorting1(const shared_ptr<Review> & r1, const shared_ptr<Review> & r2);int main()
{vector <shared_ptr<Review>> books;Review temp;while (FillReview(temp)) {shared_ptr<Review> pd_temp(new Review(temp));books.push_back(pd_temp);}if (books.size() > 0){cout<< "choose a way to show data.";int choice;showmenu();cin >> choice;while (choice <= 7 && choice > 0){switch (choice){case 1:for_each(books.begin(), books.end(), ShowReview);break;case 2:sort(books.begin(), books.end());for_each(books.begin(), books.end(), ShowReview);break;case 3:sort(books.begin(), books.end(), worseThan);for_each(books.begin(), books.end(), ShowReview);break;case 4:sort(books.begin(), books.end(), worseThan);reverse(books.begin(), books.end());for_each(books.begin(), books.end(), ShowReview);break;case 5:sort(books.begin(), books.end(), sorting1);for_each(books.begin(), books.end(), ShowReview);break;case 6:sort(books.begin(), books.end(), sorting1);reverse(books.begin(), books.end());for_each(books.begin(), books.end(), ShowReview);break;default:cout << "wrong number.";continue;}showmenu();cin >> choice;}}elsecout << "No entries. ";cout << "Bye.\n";// cin.get();return 0;
}bool operator<(const shared_ptr<Review> & r1, const shared_ptr<Review> & r2)
{if (r1->title < r2->title)return true;else if (r1->title == r2->title && r1->rating < r2->rating)return true;elsereturn false;
}bool worseThan(const shared_ptr<Review> & r1, const shared_ptr<Review> & r2)
{if (r1->rating < r2->rating)return true;elsereturn false;
}bool FillReview(Review & rr)
{std::cout << "Enter book title (quit to quit): ";getline(cin, rr.title);if (rr.title == "quit")return false;cout << "Enter book price:  ";cin >> rr.price;if (!cin)return false;std::cout << "Enter book rating: ";std::cin >> rr.rating;if (!cin)return false;// get rid of rest of input linewhile (std::cin.get() != '\n')continue;return true;
}void ShowReview(const shared_ptr<Review> & rr)
{cout << "name\trating\tprice\n";cout << rr->title << "\t" << rr->rating << "\t" << rr->price << endl;
}bool sorting1(const shared_ptr<Review> & r1, const shared_ptr<Review> & r2)
{if (r1->price < r2->price)return true;if (r1->price == r2->price && r1->rating < r2->rating)return true;elsereturn false;
}void showmenu()
{cout << "Please enter 1,2,3,4,5,6 or 7\n"<< "1) by original order \t 2) by alphabet order  \n"<< "3) by rating up      \t 4) by rating down     \n"<< "5) by pricing up     \t 6) by pricing down    \n"  << "7) quit  \n";
}

C++ Primer Plus第十六章练习相关推荐

  1. C++ Primer Plus(第六版)第十六章课后习题

    C++ Primer Plus(第六版)第十六章课后习题 16.10.1 #include <iostream> #include <string> using namespa ...

  2. C++ Primer plus学习笔记-第十六章:string类和标准模板库

    第十六章:string类和标准模板库 前言:这一章已经相当靠近全书的后面部分了:这一章我们会深入探讨一些技术上的细节,比如string的具体构造函数,比如适用于string类的几个函数,比如我们还会介 ...

  3. 第十六章、Raid及mdadm命令

    第十六章.Raid及mdadm命令 10_01_Raid及mdadm命令之一 (17 - $) 各种接口的速率: IDE: 133Mbps SATA: 300Mbps, 600Mbps, 6Gbps ...

  4. 第十六章 推荐系统-机器学习老师板书-斯坦福吴恩达教授

    第十六章 推荐系统 16.1 问题规划 16.2 基于内容的推荐系统 16.3 协同过滤 16.4 协同过滤算法 16.5 向量化:低秩矩阵分解 16.6 实现细节:均值规范化 16.1 问题规划 1 ...

  5. 鸟哥的Linux私房菜(基础篇)- 第二十六章、Linux 核心编译与管理

    第二十六章.Linux核心编译与管理 最近升级日期:2009/09/18 我们说的 Linux 其实指的就是核心 (kernel) 而已.这个核心控制你主机的所有硬件并提供系统所有的功能,所以说,他重 ...

  6. 鸟哥的Linux私房菜(基础篇)- 第十六章、例行性工作排程 (crontab)

    第十六章.例行性工作排程 (crontab) 最近升级日期:2009/09/11 学习了基础篇也一阵子了,你会发现到为什么系统常常会主动的进行一些任务?这些任务到底是谁在配置工作的?如果你想要让自己设 ...

  7. pdfstamper生成pdf无法显示汉字_正点原子STM32F4/F7水星开发板资料连载第四十六章 汉字显示实验...

    1)实验平台:正点原子水星 STM32F4/F7 开发板 2)摘自<STM32F7 开发指南(HAL 库版)>关注官方微信号公众号,获取更多资料:正点原子 3)全套实验源码+手册+视频下载 ...

  8. 判断按键值_「正点原子NANO STM32开发板资料连载」第十六章电容触摸按键实验...

    1)实验平台:ALIENTEK NANO STM32F411 V1开发板2)摘自<正点原子STM32F4 开发指南(HAL 库版>关注官方微信号公众号,获取更多资料:正点原子 第十六章电容 ...

  9. 嵌入式实时操作系统ucos-ii_「正点原子NANO STM32开发板资料连载」第三十六章 UCOSII 实验 1任务调度...

    1)实验平台:alientek NANO STM32F411 V1开发板2)摘自<正点原子STM32F4 开发指南(HAL 库版>关注官方微信号公众号,获取更多资料:正点原子 第三十六章 ...

最新文章

  1. 用python实现TCP协议传输功能(服务端代码)
  2. POJ1821 Fence
  3. Jmeter 新手教程
  4. docker镜像是否包含操作系统
  5. Mycat监控_监控平台安装Mycat-web_作为配置中心注册发现用---MyCat分布式数据库集群架构工作笔记0037
  6. According to the overall view of the patent
  7. 【网络信息安全】Web 安全
  8. python查看mongo库容量_mongo查看数据库空间大小
  9. Node.js Web开发框架
  10. SSM框架中的Mapper.xml文件中的增、删、改、查等操作
  11. 通过CuteFTP用VBScript使用SFTP,实现Win与Linux的文件传输
  12. dnf虚拟机安装的解决方法
  13. python数据分析学习和建模的个人笔记(一)
  14. linux ssh互信配置
  15. 小布语音下载安装_小布语音助手
  16. 断章·贵鬼篇·白羊妇语
  17. micropython 固件编译关于 spi psram opi qspi 的设置
  18. 静态IP和动态IP有什么区别?
  19. Kotlin 非对称加密RSA
  20. linux进程调度算法,关于嵌入式Linux系统实时进程调度算法系统详解

热门文章

  1. 矩阵可逆的充要条件及证明
  2. 关于如何在终端设置有颜色的字体
  3. u8服务器能用无线网连吗,u8客户端如何连接服务器配置
  4. 北科技计算机考研本科来源,考研经验分享:28岁,我考上了北科大
  5. 2022年浙江导游资格考试模拟题及答案(多选题)
  6. Android 手机识别
  7. 小程序日期时间选择器
  8. 【华人学者风采】任南琪 哈尔滨工业大学
  9. 双足机器人ZMP预观控制算法及代码实现
  10. 全民学python,现连小学生都开始内卷起来了!