Code:[ZachxPKU/AcceleratedCPlusPlus]

Accelerated C++

  • Chapter 6 Using library algorithms
    • 6.1 Analyzing strings
      • 6.1.1 Another way to split
      • 6.1.2 Palindromes 回文
      • 6.1.3 Finding URLs
    • 6.2 Comparing grading schemes
      • 6.2.1 Working with student records
      • 6.2.2 Analyzing the grades
      • 6.2.3 Grading based on average homework grade
      • 6.2.4 Median of the completed homework
      • 6.3 Classifying students, revisited
      • 6.3.1 A two-pass solution
      • 6.3.2 A single-pass solution
    • 6.4 Algorithms, containers, and iterators
    • 6.5 Details
    • Tips
      • Tip 6-3
      • Tip 6-4

Chapter 6 Using library algorithms

6.1 Analyzing strings

  • generic algorithm 范型算法
  • postfix 后缀 i++
  • prefix 前缀 ++i
  • iterator adaptors 迭代器适配器

6.1.1 Another way to split

这里需要注意为什么要自己写一个space函数取代库函数isspace()。

6.1.2 Palindromes 回文

//
// Created by Zach on 2022/9/3.
//  #include <iostream>
#include <string>
using namespace std;  bool is_palindromes(const string &s){  return equal(s.begin(), s.end(), s.rbegin());
}  int main(){  cout << "Please enter a word: " << endl;  string s;  cin >> s;  if(is_palindromes(s))  cout << s << " is a palindrome." << endl;  else        cout << s << " is not a palindrome." << endl;  return 0;
}

这里要注意,equal在比较两个序列时,默认第二个序列和第一个序列等长。

6.1.3 Finding URLs

//
// Created by Zach on 2022/9/3.
//  #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cctype>  using namespace std;  bool not_url_char(char c){  static const string url_ch = "~;/?:@=&$-_.+!*'(),";  return !(isalnum(c) || find(url_ch.begin(), url_ch.end(), c) != url_ch.end());
}  string::const_iterator url_end(string::const_iterator b, string::const_iterator e){  return find_if(b, e, not_url_char);
}  string::const_iterator url_beg(string::const_iterator b, string::const_iterator e){  static const string sep = "://";  typedef string::const_iterator iter;  iter i = b;  while((i = search(i,e,sep.begin(), sep.end())) != e) {  if(i != b && i + sep.size() != e) {  iter beg = i;  while(beg != b && isalpha(beg[-1]))  --beg;  if(beg != i && !not_url_char(i[sep.size()]))  return beg;  }  i += sep.size();  }  return e;
}  vector<string> find_urls(const string& s){  vector<string> ret;  typedef string::const_iterator iter;  iter b = s.begin(), e = s.end();  while(b != e){  b = url_beg(b,e);  if(b != e) {  iter after = url_end(b,e);  ret.push_back(string(b,after));  b = after;  }  }  return ret;
}  int main(){  string s;  cout << "Please enter a string: " << endl;  getline(cin, s);  vector<string> urls = find_urls(s);  vector<string>::const_iterator iter = urls.begin();  if(urls.size() == 0) {  cout << "no url in the string!" << endl;  }  while(iter != urls.end()) {  cout << *iter << endl;  ++iter;  }  return 0;
}

6.2 Comparing grading schemes

6.2.1 Working with student records

It is a better idea to use this function to check for an empty container than it is to compare the size with 0, because for some kinds of containers, it might be more efficient to check whether the container has any elements than to figure out exactly how many elements there are.

对于一些容器来说,判断其是否为空,比判断其元素个数是否为0要高效。

6.2.2 Analyzing the grades

auxiliary function 辅助函数的用法

void

6.2.3 Grading based on average homework grade

#include <numeric> 数值计算头文件

6.2.4 Median of the completed homework

6.3 Classifying students, revisited

6.3.1 A two-pass solution

6.3.2 A single-pass solution

注意一下 partition 和 stable_partition 的区别,一个会打乱组内顺序,一个不会打乱组内顺序。

6.4 Algorithms, containers, and iterators

Algorithms act on container elements—they do not act on containers.

算法是作用在容器的元素上,而不是作用在容器上。

students.erase(remove_if(students.begin(), students.end(), fgrade),
students.end());

举个例子,这里remove_if是标准库中的算法,它作用在容器的元素上,它不会改变容器的性质,比如大小。而erase是容器的成员函数,它会改变容器的大小。

另外,本节中还特别强调了部分算法和成员函数对迭代器的影响,有时候它们会使迭代器失效,需要重新设置迭代器。

6.5 Details

需要特别注意的是<numeric>里面的accumulate函数的说明。

accumulate(b, e, t)

Creates a local variable and initializes it to a copy of t (with the same type as t, which means that the type of t is crucially important to the behavior of accumulate), adds each element in the range [b, e) to the variable, and returns a copy of the variable as its result. Defined in <numeric>.

函数返回的值由t的类型决定,前面章节也提到0.0和0对结果的影响。

Tips

Tip 6-3

//
// Created by Zach on 2022/9/4.
//  #include <iostream>
#include <vector>
#include <algorithm>  using namespace std;  int main(){  vector<int> u(10, 100);  vector<int> v;  copy(u.begin(), u.end(), v.begin());  vector<int>::const_iterator iter = v.begin();  cout << v.size();  while(iter != v.end()){  cout << *(iter++) << endl;  }  return 0;
}

这段代码是会报错的,因为初始化v时,没有给v分配内存空间,v.begin()是一个空指针。

Tip 6-4

上题中的正确代码如下:

//
// Created by Zach on 2022/9/4.
//  #include <iostream>
#include <vector>
#include <algorithm>  using namespace std;  int main(){  vector<int> u(10, 100);  vector<int> v;  //    copy(u.begin(), u.end(), back_inserter(v));  copy(u.begin(), u.end(), inserter(v, v.begin()));  vector<int>::const_iterator iter = v.begin();  while(iter != v.end()){  cout << *(iter++) << endl;  }  return 0;
}

《Accelerated C++》导读(Chapter 6)相关推荐

  1. 【读书笔记】《小王子》- [法] 安托万•德•圣埃克苏佩里 / [法国] 安东尼·德·圣-埃克苏佩里

    不停的阅读,然后形成自己的知识体系. 2023.06.15 读 文章目录 导读 Chapter 01 Chapter 02 Chapter 03 Chapter 04 Chapter 05 Chapt ...

  2. 读书笔记——Accelerated C++ Chapter 12 使类对象获得数值功能

    一个简单的string类--自动转换--Str操作--有些转换是危险的--类型转化操作函数--类型转换与内存管理-- 一个简单的string类 通常情况下,一个不需要析构函数的类也不需要显示的定义复制 ...

  3. Accelerated C++ Chapter4.1 用函数来计算学生成绩

    最近在自学C++,在看斯坦福推荐的一本C++教材(没有核实是不是真的斯坦福推荐的,也是某乎上看到的,然后就买了书跟着敲代码) Learner:JC 书名:Accelerated C++: Practi ...

  4. Accelerated C++ 习题答案

    1. 在google的groups,也就是http://groups.google.com里发现很多人在讨论这本书中个别题的答案.如果你想要某一道题的答案,就在那搜Accelerated C++ ex ...

  5. survival | 生存分析(5):加速失效时间模型(Accelerated Failure Time Model)

    本篇来介绍另外一种生存模型:加速失效时间模型(Accelerated Failure Time Model,AFT模型).AFT模型是对生存时间进行建模的.它常使用在工业领域,如研究零件寿命受温度的影 ...

  6. 《小王子》精彩章节——Chapter 21

    <小王子>精彩章节--Chapter 21 说明: 金色字体是文中细节性的描述,红色加粗字体是值得品读的句子 方框内对应的是上边红色加粗字体,是个人的浅薄认知 另外,本文也附加了李继宏在导 ...

  7. [翻译] 神经网络与深度学习 第三章 提升神经网络学习的效果 - Chapter 3 Improving the way neural networks learn

    目录: 首页 译序 关于本书 关于习题和难题 第一章 利用神经网络识别手写数字 第二章 反向传播算法是如何工作的 > 第三章 提升神经网络学习的效果 第四章 可视化地证明神经网络可以计算任何函数 ...

  8. 深度学习优化函数详解(5)-- Nesterov accelerated gradient (NAG) 优化算法

    深度学习优化函数详解系列目录 深度学习优化函数详解(0)– 线性回归问题 深度学习优化函数详解(1)– Gradient Descent 梯度下降法 深度学习优化函数详解(2)– SGD 随机梯度下降 ...

  9. 比Momentum更快:揭开Nesterov Accelerated Gradient的真面目NAG 梯度下降

    d为累计梯度 作为一个调参狗,每天用着深度学习框架提供的各种优化算法如Momentum.AdaDelta.Adam等,却对其中的原理不甚清楚,这样和一条咸鱼有什么分别!(误)但是我又懒得花太多时间去看 ...

最新文章

  1. Django3.0 +Python3 连接mysql遇到django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer
  2. 【ACM】杭电OJ 2015
  3. 如何看待程序媛们的职场焦虑和未来职业规划?
  4. 怎么知道一名研究生有没有科研潜力?
  5. java commons.util_Java — CommonUtil
  6. 一个java文件中多个类
  7. 数据库引起的性能瓶颈应如何优化?
  8. 《白鹿原》金句摘抄(八)
  9. (剑指Offer)面试题4:替换空格
  10. 操作系统(王道笔记第二章)
  11. 硅谷Web 2.0时代即将结束? 实用型公司更受关注
  12. C++11 range-based for loop
  13. 安装ECShop报 Non-static method cls_image::gd_version() should not be called statically 解决方案
  14. 【Python学习笔记之一】Python关键字及其总结
  15. 基于verliog的异步模10计数器(含模块代码以及测试代码)
  16. html怎么给图片加动画效果,html5怎么添加图片动画效果
  17. 2006年网络名言100句(转)
  18. 关于fabricjs移动、放大等一些列操作后位置不对的问题
  19. 信诺计算机怎么算一次函数,excel用一次函数进行计算的方法步骤
  20. 软件测试是做什么的?好学的吗?

热门文章

  1. DSP、DMP、CDP、CRM
  2. 极品英文翻唱歌曲--《谢谢你的爱》歌词俸上
  3. Java开发校招面经
  4. Copilot免费时代结束!正式版67元/月,学生党和热门开源项目维护者可白嫖
  5. 软件项目,什么叫坑爹!大家注意了
  6. 如何使用条码打印机?打印标签?
  7. 软考:常见小题目计算题
  8. matlab中枝切法解包裹,一种基于改进枝切法的激光散斑相位解包裹方法与流程
  9. 计算n的阶乘有多少个尾随零
  10. poj2386 Lake Counting