本章中主要教我们怎样使用几个库算法来解决与处理字符串和学生成绩相关的问题。
1、分析字符串
使用一个循环来连接两幅字符图案

等价于

也等价于


分析:这里指的是复制了bottom中的全部的元素而且把它们加入到ret的末尾。

在这个函数借宿之后。ret的长度将加入bottom.size()。


补充:back_inserter(c) 对容器c产生一个迭代器,这个迭代器会给c加入元素。这个容器必须支持链表,向量以及字符串类型都会支持push_back操作。
copy(b, e, d) 把[b,e)所指示的系列拷贝到由d指示的目的地中。


2、实现split的方法

补充:
find_if(b,e,p)  依据谓词p来检查每个元素;find(b,e,t) 在序列中查找一个特定值的算法。

3、回文
回文就是顺读和逆读都一样的单词。

执行结果:
Accelerated C++学习笔记7—<使用库算法>

程序分析:rbegin返回一个迭代器。这个迭代器会从容器的最后一个元素開始。而且从后向前地逆向訪问容器。

4、查找URL
一个URL(统一资源地址)的字符序列:protocol-name://resource-name
Accelerated C++学习笔记7—<使用库算法>
主函数:

程序分析:getline(is,s) 从is读一行输入并把它储存在s中
urls头文件:

urls源文件:


执行结果:
Accelerated C++学习笔记7—<使用库算法>

程序分析:
1)isalnum函数是检验它的參数是否是一个字母数字字符(一个字母或者一个数字);
2)isalpha 推断不是字母。
3)search(b,e,b2,e2) 在序列[b,e)中查找一个特定的算法,查找由[b2,e2)所指示的序列。

6、对计算成绩的方案进行比較
编敲代码解决以下两个不同的问题:
1)读所有学生的记录。把做了所有家庭作业的学生与其它的学生分隔开
2)对每一组中的全部学生分别使用每个的计算成绩的方案,报告每一组的中值成绩。


主函数:

简单检查一个学生是否做个全部的家庭作业:

不同的分析函数:

程序分析:
1)accumulate(b,e,t)                 把区间[b,e)中的元素的总和加上t之后储存在t中,是在<numeric>中定义的。
2)find(b,e,t)                       在序列[b,e)中查找值t;
3)find_if(b,e,p)                    在序列[b,e)中依据谓词p来检查每个元素。
4)search(b,e,b2,e2)                 在序列[b,e)中查找一个特定的算法。查找由[b2,e2)所指示的序列;
5)copy(b,e,d)                       把序列[b,e)拷贝到由d指示的目的地中,复制整个序列;
6)remove_copy(b,e,d,t)              把序列[b,e)中全部不等于t的元素拷贝到由d指示的目的地中;
7)remove_copy_if(b,e,d,t)           把序列[b,e)中全部使谓词p为假的元素拷贝到由d指示的目的地中;
8)remove_if(b,e,p)                  使在区间[b,e)中使谓词p为假的元素位于这个域的头部,返回一个迭代器,这个迭代器指示了位于那些不被删除的元素之后的那个位置
9)remove(b,e,t)                     作用和上面那个一样,可是检測了哪些元素值不等于t;
10)transform(b,e,d,f)               依据域[b,e)中的元素执行函数f,把f的结果存储在d中;  

7、对学生进行分类并回想下我们的问题
1)使用两次传递的解决方式

这里计算了两次成绩。所以须要改进

2)一种一次传递的解决方式
程序分析:
1)partition(b,e,p)    以谓词p为基础从而划分在域[b,e)中的元素以使那些使谓词为真的元素处于容器的头部。

返回一个迭代器,这个迭代器指示了第一个令谓词为假的元素;

2)stable_partition(b,e,p)  会让在每个区域内的元素的输入顺序保持不变  

8、以下的这个程序片段做什么的?
编写一个包括这个片段的程序并编译以及执行这个程序。
方法一:

//改进上面方法:利用迭代适配器,循环 //时间:2014.5.24 #include "stdafx.h" #include <algorithm> #include <iterator> #include <vector> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { vector<int> u(10,100); vector<int> v; copy(u.begin(),u.end(),inserter(v,v.begin())); return 0; }</span>


方法二:

程序说明:vector<int> u(10,100)定义了一个vector u,该vector中存有10个100。将u中的元素通过copy函数所有拷贝到v。

9、计算成绩的分析程序的一部分功能是读入学生记录并对其进行分类。这一部分程序依赖于学生是否做了所有的家庭作业,这个问题和extract_fails中所须要解决的类似

程序说明:这里须要注意的就是stable_partition.

10、编写一个函数,用这个函数来依照你自己的选择准则来对学生进行分类,使用它来取代extract_fails程序从而对它进行測试
主函数:

//功能:编写一个函数。用这个函数来依照你自己的选择准则来对学生进行分类,使用它来取代extract_fails程序从而对它进行測试 //时间:2014.5.24 #include "stdafx.h" #include <algorithm> #include <vector> #include <iostream> #include <string> #include "6-7-classify.h" #include "Student_info.h" #include "grade.h" using namespace std; int _tmain(int argc, _TCHAR* argv[]) { vector<Student_info> vs; Student_info s; string::size_type maxlen = 0; while(read(cin,s)) { maxlen = max(maxlen, s.name.size()); vs.push_back(s); } sort(vs.begin(), vs.end(),compare); vector<Student_info> fails = classify(vs, pgrade); for(int i = 0; i < fails.size(); ++i) cout << fails[i].name << " " << grade(fails[i]) << endl; return 0; } </span>


分类函数:

11、在lesson6_7中继续编写一个函数在程序中用它来分析学生的成绩
主函数:

//功能:在lesson6_7中继续编写一个函数在程序中用它来分析学生的成绩 #include "stdafx.h" #include <vector> #include <iostream> #include "6-8-classify.h" #include "analysis.h" #include "Student_info.h" using namespace std; int _tmain(int argc, _TCHAR* argv[]) { // students who did and didn't do all their homework vector<Student_info> did; // read the student records and partition them Student_info student; while (read(cin, student)) did.push_back(student); vector<Student_info> didnt = classify(did, did_all_hw);//这里是我们的两点地方 // verify that the analyses will show us something if (did.empty()) { cout << "No student did all the homework!" << endl; return 1; } if (didnt.empty()) { cout << "Every student did all the homework!" << endl; return 1; } // do the analyses write_analysis(cout, "median", median_analysis, did, didnt); write_analysis(cout, "average", average_analysis, did, didnt); write_analysis(cout, "median of homework turned in",optimistic_median_analysis, did, didnt); return 0; } </span>


12、使用一个库算法连接一个vector<string>对象中的全部元素

执行结果:
Accelerated C++学习笔记7—&lt;使用库算法&gt;

                                                                                     ——To_捭阖_youth

相关文章:

  • 2021-09-24
  • 2021-06-02
  • 2022-01-24
  • 2022-12-23
  • 2021-11-01
  • 2021-12-28
  • 2022-12-23
猜你喜欢
  • 2021-08-16
  • 2021-12-21
  • 2022-12-23
  • 2021-06-06
  • 2022-12-23
  • 2021-08-23
  • 2022-12-23
相关资源
相似解决方案