第十章 10.1节练习 & 10.2.1节练习
来源:互联网 发布:php中连接符 编辑:程序博客网 时间:2024/06/06 01:19
练习10.1:
头文件algorithm中定义了一个名为count的函数,它类似find,接受一对迭代器和一个值作为参数。count返回给定值的序列中出现的次数。编写程序,读取int序列存入vector中,打印有多少哥元素等于给定值。
解答:
// count algorithm example#include <iostream> // std::cout#include <algorithm> // std::count#include <vector> // std::vectorint main () { // counting elements in array: int myints[] = {10,20,30,30,20,10,10,20}; // 8 elements int mycount = std::count (myints, myints+8, 10); std::cout << "10 appears " << mycount << " times.\n"; // counting elements in container: std::vector<int> myvector (myints, myints+8); mycount = std::count (myvector.begin(), myvector.end(), 20); std::cout << "20 appears " << mycount << " times.\n"; return 0;}
这是cpp reference上给出的范例,这里就直接搬过来了。
练习10.2:
重做上一题,但读取string序列存入list中。
解答:
// count algorithm example#include <iostream> // std::cout#include <algorithm> // std::count#include <vector> // std::vector#include <string> // std::stringint main () { // counting elements in array: std::string myints[] = {"10","20","30","30","20","10","10","20"}; // 8 elements int mycount = std::count (myints, myints+8, "10"); std::cout << "10 appears " << mycount << " times.\n"; // counting elements in container: std::vector<std::string> myvector (myints, myints+8); mycount = std::count (myvector.begin(), myvector.end(), "20"); std::cout << "20 appears " << mycount << " times.\n"; return 0;}
简单的改了一下。
练习10.3:
用accumulate求一个vector<int>中元素之和。
解答:
#include <iostream> // std::cout#include <vector> // std::vector#include <numeric> // std::accumulateint main () { // counting elements in array: int myints[] = {10,20,30,30,20,10,10,20}; // 8 elements // counting elements in container: std::vector<int> myvector (myints, myints+8); std::cout << std::accumulate(myvector.cbegin(), myvector.cend(), 0) << std::endl; return 0;}
练习10.4:
假定v是一个vector<double>,那么调用accumulate(v.cbegin(), v.cend(), 0)有何错误(如果存在的话)?
解答:
accumulate会先将vector<double>中的元素转化成int类型,然后再进行家和运算。
【引用】accumulate的第三个参数的类型决定了函数中使用哪个加法运算符以及返回值的类型。
所以这里需要这样修改一下
accumulate(v.cbegin(), v.cend(), 0.)
或者
accumulate(v.cbegin(), v.cend(), 0.0)都是可以的。
练习10.5:
在本节对名册(roster)调用equal的例子中,如果两个名册中保存的都是C风格字符串而不是string,会发生什么?
解答:
自认为这样也是没有问题的,写了段代码进行验证,并没有出错。
#include <iostream>#include <vector>#include <string>#include <numeric>using namespace std;int main(){ vector<const char*> csl1{"hello world", "hello china"}; vector<const char*> csl2{"hello world", "hello china"}; cout << equal(csl1.cbegin(), csl1.cend(), csl2.cbegin()) << endl;}
0 0
- 第十章 10.1节练习 & 10.2.1节练习
- 第十章 10.2.3节练习 & 10.3.1节练习
- 第十章 10.2.2节练习
- 第十章 10.4.1节练习
- 第十章 10.5.1节练习
- 第十章 10.5.3节练习 & 10.6节练习
- 第十章 10.3.2节练习
- 第十章 10.3.3节练习
- 第十章 10.3.4节练习
- 第十章 10.4.2节练习
- 第十章 10.4.3节练习
- 第十章编程练习(1)
- 第十章 编程练习
- 第十章练习
- 10.2.1节练习
- C primer plus 第十章 练习1:
- C#第十章上机练习1
- 第十章编程练习(2)
- 71道经典Android面试题和答案--重要知识点都涉及到了
- 关于一些Android课程资料
- UVA 1471 - Defense Lines(扫描+二分)
- [SPOJ ORDERSET] Order statistic set [Treap]
- C# delegate方法invoke和beginInvoke的区别
- 第十章 10.1节练习 & 10.2.1节练习
- LRU算法
- SpannableString、ImageSpan的用途
- ajax跨域问题小结
- 2014先锋机试题:循环报数
- 黑马程序员_多线程
- linux 链接ln的使用 创建和删除符号连接(软、硬链接)
- ruby常用字符串处理函数
- 某人原创的排序