当前位置: 首页 > news >正文

【C++ Primer Plus习题】16.10

大家好,这里是国中之林!
❥前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。有兴趣的可以点点进去看看←

问题:

在这里插入图片描述

解答:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <memory>
using namespace std;struct Review
{string title;int rating;int price;
};bool operator<(const shared_ptr<Review>& p1, const shared_ptr<Review>& p2);
bool FillReview(Review& rr);
void ShowReview(const shared_ptr<Review>& p);
bool worseThan(const shared_ptr<Review>& p1, const shared_ptr<Review>& p2);
bool expenThan(const shared_ptr<Review>& p1, const shared_ptr<Review>& p2);int main()
{vector<shared_ptr<Review>>books;Review temp;while (FillReview(temp)){shared_ptr<Review>pd(new Review(temp));books.push_back(pd);}if (books.size() > 0){cout << "Choose the way to sort: "<< "r:rate, s: rate r, p:price, d:price, d:price r, q:quit\n";char choice;while (cin >> choice && choice != 'q'){switch (choice){case 'r':sort(books.begin(), books.end(), worseThan);break;case 's':sort(books.rbegin(), books.rend(), worseThan);break;case 'p':sort(books.begin(), books.end(), expenThan);break;case 'd':sort(books.rbegin(), books.rend(), expenThan);break;default:break;}for_each(books.begin(), books.end(), ShowReview);cout<<"Please choose the way to sort: "<< "r:rate, s: rate r, p:price, d:price, d:price r, q:quit\n";}}return 0;
}bool operator<(const shared_ptr<Review>& p1, const shared_ptr<Review>& p2)
{if (p1->title < p2->title)return true;else if (p1->title == p2->title && p1->rating < p2->rating)return true;elsereturn false;
}
bool FillReview(Review& rr)
{cout << "Enter book title (quit to quit): ";getline(cin, rr.title);if (rr.title == "quit" || rr.title == "")return false;cout << "Enter book rating: ";cin >> rr.rating;if (!cin)return false;cout << "Enter book price: ";cin >> rr.price;if (!cin)return false;while (cin.get() != '\n'){continue;}return true;
}
void ShowReview(const shared_ptr<Review>& p)
{cout << p->rating << "\t" << p->title << "\t" << p->price << endl;
}
bool  worseThan(const shared_ptr<Review>& p1, const shared_ptr<Review>& p2)
{if (p1->rating < p2->rating)return true;elsereturn false;
}
bool expenThan(const shared_ptr<Review>& p1, const shared_ptr<Review>& p2)
{if (p1->price < p2->price)return true;elsereturn false;
}

运行结果:
在这里插入图片描述

考查点:

  • vector容器
  • 智能指针
  • sort()
  • 迭代器
  • for_each()

2024年9月20日21:49:47

http://www.lryc.cn/news/442281.html

相关文章:

  • Django框架全面指南
  • git 更新LingDongGui问题解决
  • Thymeleaf模版引擎
  • jpa适配mysql切换达梦可能的坑
  • 922. 按奇偶排序数组 II 双指针 力扣
  • Vue接入高德地图并实现基本的路线规划功能
  • linux网络编程4
  • Spring模块详解Ⅳ(Spring ORM和Spring Transaction)
  • 深度图可视化显示(kitti)
  • 【Kubernetes知识点】HPA如何控制不同的资源实现自动扩缩容?
  • adb devices不显示连接设备怎么解决
  • 经典sql题(一)求连续登录不少于三天用户
  • 2024java面试-软实力篇
  • 「OC」present和push操作区别以及混合推出的实现
  • 【高分系列卫星简介】
  • 八股文-多线程、并发
  • xtu oj 折纸
  • 传知代码-多示例AI模型实现病理图像分类
  • Java知识点小结3:内存回收
  • LeetCode746:使用花费最小爬楼梯
  • 列表、数组排序总结:Collections.sort()、list.sort()、list.stream().sorted()、Arrays.sort()
  • 【资料分析】刷题日记3
  • 基于SpringBoot+Vue的商场停车场管理系统
  • 4. 密码协议
  • 基于嵌入式的智能物流柜( 触摸屏/0.96寸oled屏)
  • VSCode创建C++项目和编译多文件
  • 7个提升网站分页体验的 CSS 和 JavaScript 代码片段
  • C++——用带有默认参数的函数实现,求两个整数或三个整数中的最大数。
  • 对商品分类系统的若干问题的思考
  • javascript中Number 类型 在实际开发中常用的一些操作方法