当前位置:   article > 正文

c++ primer 第九章习题_c++ primer练习9.21

c++ primer练习9.21

练习9.1(a) 使用vector+sort  或 sort

(b) deque   (c) vector+sort  大量小型数据

练习9.2  list<deque<int>> x;

练习9.3 需要有两个指向同一容器的迭代器。begin不在end后面。

练习9.4

  1. bool findX(vector<int>::iterator begin, vector<int>::iterator end, int x) {
  2. while (begin != end) {
  3. if (*begin == x)
  4. return true;
  5. begin++;
  6. }
  7. return false;
  8. }

练习9.5

  1. vector<int>::iterator findX(vector<int>::iterator begin, vector<int>::iterator end, int x) {
  2. while (begin != end) {
  3. if (*begin == x)
  4. return begin;
  5. begin++;
  6. }
  7. return end;
  8. }

练习9.6 list的迭代器不支持<操作,改为!=。

练习9.7 vector<int>::value_type

练习9.8  list<string>::value_type   list<string>::reference

练习9.9  begin经过重载,对const类型返回const,非const返回非const。 cbegin总是返回常量迭代器。

练习9.10  普通类型 const  const  const

练习9.11 vector<int> a;  vector<int> a(b); vector<int> a={1,2,3,4,5}; 

vector<int> a = b; vector<int> a = {1,2,3,4,5}; vector<int> a(b.begin(), b.end());

练习9.12 接受一个容器则两个容器需要类型完全相同,接收两个迭代器的只需要元素类型相容。

练习9.13 vector<double> b(l.begin(), l.end()); vector<double> b(a.begin(), a.end());

练习9.14 

  1. list<const char*> l{"123", "456"};
  2. vector<string> vs(l.begin(), l.end());
  3. for(auto x : vs)
  4. cout << x << endl;
  5. return 0;

练习9.15

  1. bool vEqual(const vector<int>& v1, const vector<int>& v2) {
  2. if (v1.size() != v2.size())
  3. return false;
  4. for (int i = 0; i < v1.size(); i++)
  5. if (v1[i] != v2[i])
  6. return false;
  7. return true;
  8. }

练习9.16

  1. bool lvEqual(const list<int> &l, const vector<int>&v) {
  2. auto lIter = l.begin();
  3. auto vIter = v.begin();
  4. while (lIter != l.end() && vIter != v.end() && *lIter == *vIter) {
  5. lIter++;
  6. vIter++;
  7. }
  8. return lIter == l.end() && vIter == v.end();
  9. }

练习9.17 c1和c2类型相同且包含的元素有<操作。

练习9.18

  1. void inputAndPrint() {
  2. deque<string> d;
  3. string s;
  4. while (cin >> s)
  5. d.push_back(s);
  6. deque<string>::iterator diter = d.begin();
  7. while (diter != d.end())
  8. cout << *diter++ << endl;
  9. }

练习9.19

  1. void inputAndPrintList() {
  2. list<string> d;
  3. string s;
  4. while (cin >> s) {
  5. d.push_back(s);
  6. d.insert(d.end(), s);
  7. d.emplace(d.end(), s);
  8. }
  9. list<string>::iterator diter = d.begin();
  10. while (diter != d.end())
  11. cout << *diter++ << endl;
  12. }

练习9.20

  1. void copyFromList(list<int>& l) {
  2. list<int>::iterator iter = l.begin();
  3. deque<int> dOdd, dEven;
  4. while (iter != l.end()) {
  5. if (*iter % 2 == 0)
  6. dEven.insert(dEven.end(), *iter++);
  7. else
  8. dOdd.insert(dOdd.end(), *iter++);
  9. }
  10. for (auto it = dOdd.begin(); it != dOdd.end(); it++)
  11. cout << *it << endl;
  12. }

练习9.21 每次都把vector中的元素依次往后移动

练习9.22 永远不会等于,添加++iter

练习9.23 相等

练习9.25 不删除  返回elem2

练习9.29添加75个默认值, 抛弃第10个后面的值。

练习9.30 有默认构造函数或默认值

练习9.31 迭代器不能+2  加两次

练习9.32 不合法  iter的求值顺序不定

练习9.33 行为未定义

练习9.34 将奇数值复制一次

练习9.35 capacity是不分配内存的最大容量  size是目前元素个数。

练习9.36 不可能

练习9.37 list元素不是相邻的,不需要统一分配内存。  array元素数量固定。

练习9.38 每次两倍。

练习9.39 添加一些空串

练习9.40 512  乘2

练习9.41

  1. int main()
  2. {
  3. vector<char> v = {'1','2','3','4','5'};
  4. string s(v.begin(), v.end());
  5. cout << s <<endl;
  6. return 0;
  7. }

练习9.42 先用vector读取,然后转为string

练习9.43

  1. void change(string& s, string& old, string& news) {
  2. auto sit = s.begin();
  3. auto oit = old.begin();
  4. while(sit != s.end()) {
  5. if (*sit == *oit) {
  6. sit++;
  7. oit++;
  8. }else {
  9. sit++;
  10. oit = old.begin();
  11. }
  12. if (oit == old.end()) {
  13. sit = s.erase(sit-old.size(),sit);
  14. sit = s.insert(sit, news.begin(),news.end());
  15. sit = sit + news.size();
  16. oit = old.begin();
  17. }
  18. }
  19. }

练习9.44

  1. void change(string& s, string& old, string& news) {
  2. auto sit = s.begin();
  3. auto oit = old.begin();
  4. while(sit != s.end()) {
  5. if (*sit == *oit) {
  6. sit++;
  7. oit++;
  8. }else {
  9. sit++;
  10. oit = old.begin();
  11. }
  12. if (oit == old.end()) {
  13. int p = sit - s.begin();
  14. s.replace(p-old.size(), old.size(), news);
  15. sit = s.begin() + p - old.size() + news.size();
  16. }
  17. }
  18. }

练习9.46

  1. string insertAppend(string& s, string& pre, string& suf) {
  2. s.insert(0, pre);
  3. s.insert(s.size(), suf);
  4. return s;
  5. }

练习9.47

  1. void findFirst(string&s, string& num) {
  2. int pos = 0;
  3. while((pos = s.find_first_of(num, pos)) != string::npos) {
  4. cout << "number index: "<< pos << " is "<< s[pos] <<endl;
  5. pos++;
  6. }
  7. pos = 0;
  8. while((pos = s.find_first_not_of(num, pos)) != string::npos) {
  9. cout << "character index: "<< pos << " is "<< s[pos] <<endl;
  10. pos++;
  11. }
  12. }

练习9.48 string::npos
练习9.49

  1. void longest(string& s, string& c) {
  2. int pos = 0;
  3. int left = 0, ll = 0;
  4. int right = 0, rr = 0;
  5. while((pos = s.find_first_of(c,pos)) != string::npos) {
  6. if (pos == rr) {
  7. rr = pos+1;
  8. if (rr - ll > right - left) {
  9. right = rr;
  10. left = ll;
  11. }
  12. }else {
  13. ll = pos;
  14. rr = pos+1;
  15. }
  16. pos++;
  17. }
  18. cout << s.substr(left, right-left);
  19. }

练习9.50

  1. int sumInt(vector<string> v) {
  2. int sum = 0;
  3. for(auto x : v)
  4. sum += stoi(x);
  5. return sum;
  6. }
  7. double sumDouble(vector<string> v) {
  8. double sum = 0;
  9. for(auto x : v)
  10. sum += stod(x);
  11. return sum;
  12. }

练习9.51

  1. class date
  2. {
  3. unsigned year, month, day;
  4. public:
  5. friend void pirntDate(date& d);
  6. date(const string& ss) {
  7. int c = 0;
  8. if (ss.find(",") != string::npos) c = 0;
  9. else if (ss.find("/") != string::npos) c = 1;
  10. else c = 2;
  11. int left=0, right=0;
  12. switch(c) {
  13. case 0:
  14. if (ss.find("Jan") != string::npos) month = 1;
  15. if (ss.find("Feb") != string::npos) month = 2;
  16. if (ss.find("Mar") != string::npos) month = 3;
  17. if (ss.find("Apr") != string::npos) month = 4;
  18. if (ss.find("May") != string::npos) month = 5;
  19. if (ss.find("Jun") != string::npos) month = 6;
  20. if (ss.find("Jul") != string::npos) month = 7;
  21. if (ss.find("Aug") != string::npos) month = 8;
  22. if (ss.find("Sep") != string::npos) month = 9;
  23. if (ss.find("Oct") != string::npos) month = 10;
  24. if (ss.find("Nov") != string::npos) month = 11;
  25. if (ss.find("Dec") != string::npos) month = 12;
  26. left = ss.find(" ");
  27. right = ss.find(",");
  28. day = stoi(ss.substr(left+1,right-left-1));
  29. year = stoi(ss.substr(right+1));
  30. break;
  31. case 1:
  32. left = ss.find_first_of("/",0);
  33. right = ss.find_first_of("/",left+1);
  34. month = stoi(ss.substr(0,left));
  35. day = stoi(ss.substr(left+1, right-left-1));
  36. year = stoi(ss.substr(right+1,4));
  37. break;
  38. case 2:
  39. if (ss.find("Jan") != string::npos) month = 1;
  40. if (ss.find("Feb") != string::npos) month = 2;
  41. if (ss.find("Mar") != string::npos) month = 3;
  42. if (ss.find("Apr") != string::npos) month = 4;
  43. if (ss.find("May") != string::npos) month = 5;
  44. if (ss.find("Jun") != string::npos) month = 6;
  45. if (ss.find("Jul") != string::npos) month = 7;
  46. if (ss.find("Aug") != string::npos) month = 8;
  47. if (ss.find("Sep") != string::npos) month = 9;
  48. if (ss.find("Oct") != string::npos) month = 10;
  49. if (ss.find("Nov") != string::npos) month = 11;
  50. if (ss.find("Dec") != string::npos) month = 12;
  51. left = ss.find_first_of(' ');
  52. right = ss.find_first_of(' ', left+1);
  53. day = stoi(ss.substr(left+1, right-left-1));
  54. year = stoi(ss.substr(right+1));
  55. break;
  56. }
  57. }
  58. };
  59. void pirntDate(date& d){
  60. cout << d.year << "年" << d.month << "月"<< d.day<<"日"<<endl;
  61. }
  62. int main()
  63. {
  64. date d1("January 1, 1900");
  65. pirntDate(d1);
  66. date d2("1/2/1900");
  67. pirntDate(d2);
  68. date d3("Jan 1 1900");
  69. pirntDate(d3);
  70. return 0;
  71. }

练习9.52

思路:中缀表达式的计算。

/*

*说明:中缀表达式求值函数说明

1.扫描表达式

  a.如果 token 是 "+","-","*","/"

    a1.符号栈为空,token直接入栈

          a2.符号栈不空,比较token和栈顶符号的优先级

             如果token的优先级高,直接入栈

                   否则将栈里面的符号出栈,并和数字栈进行计算,设置新的栈顶符号和优先级,一直到优先级低的全部弹出栈,循环停止,新的符号入栈

  b.如果 token是 "(",直接入栈

  c. 如果token是")"

         当stack_opt.top !="(",既没有找到最近的和右括号匹配的左括号

                     操作符弹出栈,并和操作数计算

                     最后将"("也弹出去

                     ”)“不进栈,它的作用是为了消除掉最近的左括号) //循环外的pop是将左括号丢弃

  d.剩下的情况就都是数字,直接进入操作数栈

*/

  1. int priorityOpt(char opt) {
  2. switch(opt) {
  3. case '(':
  4. return 1;
  5. case '+':
  6. case '-':
  7. return 2;
  8. case '*':
  9. case '/':
  10. return 3;
  11. default:
  12. return 0;
  13. }
  14. }
  15. void compute(stack<int>& val, char opt) {
  16. int right = val.top();
  17. val.pop();
  18. int left = val.top();
  19. val.pop();
  20. int res = 0;
  21. switch(opt) {
  22. case '+':
  23. res = left + right;
  24. break;
  25. case '-':
  26. res = left - right;
  27. break;
  28. case '*':
  29. res = left * right;
  30. break;
  31. case '/':
  32. res = left/right;
  33. break;
  34. }
  35. val.push(res);
  36. }
  37. int getNum(string& s, int& index) {
  38. int left = index;
  39. int right = left+1;
  40. while(right == (s.find_first_of("0123456789",++index)))
  41. right++;
  42. return stoi(s.substr(left,right));
  43. }
  44. int caculator(string& s) {
  45. stack<char> symbol;
  46. stack<int> val;
  47. int res = 0;
  48. int index = 0;
  49. int tmp;
  50. while(index < s.size()) {
  51. if (s[index] == '(')
  52. symbol.push(s[index++]);
  53. else if (s[index] == ')') {
  54. while(symbol.top() != '(') {
  55. compute(val, symbol.top());
  56. symbol.pop();
  57. }
  58. symbol.pop();
  59. index++;
  60. }
  61. else if (s[index] == '+' || s[index] == '-' || s[index]=='*' || s[index]=='/') {
  62. if(symbol.empty())
  63. symbol.push(s[index]);
  64. else if (priorityOpt(s[index]) > priorityOpt(symbol.top())) {
  65. symbol.push(s[index]);
  66. }else {
  67. compute(val, symbol.top());
  68. symbol.pop();
  69. symbol.push(s[index]);
  70. }
  71. index++;
  72. }else {
  73. int num = getNum(s,index);
  74. val.push(num);
  75. }
  76. }
  77. while(!symbol.empty()){
  78. compute(val, symbol.top());
  79. symbol.pop();
  80. }
  81. return val.top();
  82. }
  83. int main()
  84. {
  85. string s = "((1+2)*4+3/2-5)+3";
  86. cout<<caculator(s)<<endl;
  87. return 0;
  88. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/178267
推荐阅读
相关标签
  

闽ICP备14008679号