首页 > 代码库 > C++primer 10.3.1节练习
C++primer 10.3.1节练习
练习10.11
1 #include<iostream> 2 #include<string> 3 #include <iostream> 4 #include <string> 5 #include <vector> 6 #include <algorithm> 7 #include <list> 8 using namespace std; 9 10 void elmDups(vector<string> &vec); 11 12 bool isShorter(string str, string str1); 13 14 int main() 15 { 16 vector<string> str; 17 string str1; 18 while (cin >> str1) 19 { 20 str.push_back(str1); 21 } 22 elmDups(str); 23 stable_sort(str.begin(), str.end(), isShorter); 24 for (auto c : str) 25 cout << c << endl; 26 system("pause"); 27 return 0; 28 } 29 30 void elmDups(vector<string>& vec) 31 { 32 sort(vec.begin(), vec.end()); 33 auto it = unique(vec.begin(), vec.end()); 34 vec.erase(it, vec.end()); 35 } 36 37 bool isShorter(string str, string str1) 38 { 39 return str < str1 ? true : false; 40 }
练习10.12
1 #include <iostream> 2 #include <string> 3 #include "factmain.h" 4 #include <fstream> 5 #include <vector> 6 #include <algorithm> 7 using namespace std; 8 9 struct Sales_data { 10 friend ostream &print(ostream &os, const Sales_data &item); 11 friend istream &read(istream &is, Sales_data &item); 12 private: 13 string bookNo; 14 unsigned units_sold; 15 double price; 16 double revenue; 17 double avg_price() const; 18 public: 19 Sales_data(const string &s, const unsigned &u, const double &p) : bookNo(s), units_sold(u), price(p) {} 20 Sales_data() : Sales_data("12345", 3, 12) {} 21 Sales_data(string s) : Sales_data(s, 2, 4) {} 22 //Sales_data(const string &s, const unsigned &u) : bookNo(s), units_sold(u) {} 23 Sales_data(istream &is) : Sales_data() { read(is, *this); } 24 string isbn() const { return bookNo; } 25 unsigned &rUnit() { return units_sold; } 26 double &rPrice() { return price; } 27 double &rRevenue() { return revenue; } 28 Sales_data &combine(const Sales_data&); 29 30 }; 31 32 Sales_data add(const Sales_data &lhs, const Sales_data &rhs); 33 ostream &print(ostream &os, const Sales_data &item); 34 istream &read(istream &is, Sales_data &item); 35 bool compareIsbn(Sales_data data1, Sales_data data2); 36 37 38 int main() //main函数的部分需要改变 39 { 40 vector<Sales_data> Data; 41 Sales_data data; 42 int i = 10; 43 while (i-- != 0) 44 { 45 read(cin, data); 46 Data.push_back(data); 47 } 48 sort(Data.begin(), Data.end(), compareIsbn); 49 for (auto c : Data) 50 { 51 print(cout, c); 52 cout << endl; 53 } 54 55 system("pause"); 56 return 0; 57 } 58 59 Sales_data add(const Sales_data & lhs, const Sales_data & rhs) //非成员函数add 60 { 61 Sales_data sum = lhs; 62 sum.combine(rhs); 63 return sum; 64 // TODO: 在此处插入 return 语句 65 } 66 67 istream & read(istream & is, Sales_data & item) 68 { 69 is >> item.bookNo >> item.units_sold >> item.price; 70 item.revenue = item.units_sold * item.price; 71 return is; 72 // TODO: 在此处插入 return 语句 73 } 74 75 bool compareIsbn(Sales_data data1, Sales_data data2) 76 { 77 return data1.isbn() < data2.isbn() ? true : false; 78 } 79 80 ostream & print(ostream & os, const Sales_data & item) 81 { 82 os << item.bookNo << " " << item.units_sold << " " << item.price << " " << item.revenue; 83 return os; 84 // TODO: 在此处插入 return 语句 85 } 86 87 Sales_data & Sales_data::combine(const Sales_data &rhs) 88 { 89 units_sold += rhs.units_sold; 90 revenue += rhs.revenue; 91 return *this; 92 } 93 94 inline double Sales_data::avg_price() const 95 { 96 return units_sold ? revenue/units_sold : 0; 97 }
练习10.13
1 #include<iostream> 2 #include<string> 3 #include <iostream> 4 #include <string> 5 #include <vector> 6 #include <algorithm> 7 #include <list> 8 using namespace std; 9 10 void elmDups(vector<string> &vec); 11 12 bool isShorter(string str, string str1); 13 14 bool divide(string s); 15 16 int main() 17 { 18 vector<string> words{ "the", "quick", "red", "fox", "jumps", "over","the", "slow","red","turtle" }; 19 elmDups(words); 20 auto it = partition(words.begin(), words.end(), divide); 21 for (auto iter = words.begin(); iter != it; ++iter) 22 cout << *iter << endl; 23 system("pause"); 24 return 0; 25 } 26 27 void elmDups(vector<string>& vec) 28 { 29 sort(vec.begin(), vec.end()); 30 auto it = unique(vec.begin(), vec.end()); 31 vec.erase(it, vec.end()); 32 } 33 34 bool isShorter(string str, string str1) 35 { 36 return str < str1 ? true : false; 37 } 38 39 bool divide(string s) 40 { 41 return s.size() > 4 ? true : false; 42 }
C++primer 10.3.1节练习
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。