吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3581|回复: 15
收起左侧

[其他转载] C++学习日记(第九章)

[复制链接]
SstudentT 发表于 2015-4-9 20:52
本帖最后由 SstudentT 于 2015-4-12 13:24 编辑

我写的是
C++ Primer 英文第五版 每章课后习题解答
本来打算在一个帖子写上所有的章节课后解答的,后来发现吾爱经常清理不活跃的人。。。
欢迎大家和我交流这本书的课后习题,指出我的不足。十分感谢!由于题目解答较短,容易理解,我就不写注释了。
/*
Exercise 9.1:
Which is the most appropriate—a vector, a deque, or a list—for the following program tasks?
Explain the rationale for your choice.
If there is no reason to prefer one or another container, explain why not.

(a) Read a fixed number of words, inserting them in the container alphabetically as they are entered. We’ll see in the next chapter that associative containers are better suited to this problem.
(b) Read an unknown number of words. Always insert new words at the back. Remove the next value from the front.
(c) Read an unknown number of integers from a file. Sort the numbers and then print them to standard output.

习题一的答案摘自git上的解答,虽然我选的答案跟他的一样,但是我说不出个所以然,而且我的英语表达能力太差 ,就尽量不献丑了。
(a)`std::set` is the best. now, we can select `vector` or `deque`, better than `list`, cause we don't need insert or delete elements in the middle.
(b) `deque`.
If the program needs to insert or delete elements at the front and the back, but not in the middle, use a deque
(c) `vector`, no need that insert or delete at the front or back. and
If your program has lots of small elements and space overhead matters, don’t
use list or forward_list.

*/




免费评分

参与人数 1热心值 +1 收起 理由
忘掉过去 + 1 谢谢分享!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| SstudentT 发表于 2015-4-12 12:39
/*exercise9.11
Show an example of each of the six ways to create and initialize a vector.Explain what value each vector contains.
*/
[C++] 纯文本查看 复制代码
#include<iostream>
#include<vector>
void Example(const std::vector<int>&v,int index);
int main()
{
    using std::vector;
    vector<int> v1;                      //Don't have any element
    vector<int> v2={11,22,33,44,55 };      //Five elements :11  22  33  44  55
    vector<int> v3=v2;                   //Same as v2
    vector<int> v4(10,1);                //Ten elements,each initialized to 1
    vector<int> v5(10);                  //Ten elements,each initialized to 0
    vector<int> v6(v3.cbegin(),v3.cend());//Same as v3
    Example(v1,1);
      Example(v2,2);
        Example(v3,3);
          Example(v4,4);
            Example(v5,5);
              Example(v6,6);
}
void Example(const std::vector<int>&v,int index)
{
    std::cout<<"Vector"<<index<<" element:";
    for(auto p:v)
    std::cout<<p<<" ";
    std::cout<<std::endl;

}

 楼主| SstudentT 发表于 2015-4-10 17:28
本帖最后由 SstudentT 于 2015-4-11 00:10 编辑

/*
exercise9.6
What is wrong with the following program? How might you correct it?
list<int> lst1;
list<int>::iterator iter1=lst1.begin(),iter2=lst2.end();
while(iter1<iter2)
*/
Answer:
We can't use the operator < to the list. We can use != to replace <.
/*
exercise9.7
What type should be used as the index into a vector of ints ?
*/
Answer: Page330
vector<int>::size_type         
Unsigned integral type big enough to hold the size of the largest possible container of this container type

/*
exercise9.8
What type should be used to read elements in a list of strings? To write them?
*/
Answer:
If just read:  list<string>::const_iterator
Need to write them: list<string>::iteraotr

/*
exercise9.9
What is the difference between the begin and cbegin functions?
*/
Answer:
The begin function returns the  container_type<data_type>::iterator.
You can change the element value through the iterator returned by begin function.
The cbegin function returns the container_type<data_type>::const_iterator.
The first letter in the "cbegin" is 'c' denote the "const". So you can't change the value  through the const_iterator.

/*
exercise9.10
What are the types of the following four objects?
vector<int> v1;
const vector<int> v2;
auto it1=v1.begin(),it2=v2.begin();
auto ti3=v1.cbegin(),it4=v2.cbegin();
*/
Answer:
auto it1 = v1.begin();
auto it2 = v2.begin(), it3 = v1.cbegin(), it4 = v2.cbegin();  
D:\CodeBlocks\myproject\C++Primer\TestMind\main.cpp|9|error: inconsistent deduction for 'auto': '__gnu_cxx::__normal_iterator<int*, std::vector<int> >' and then '__gnu_cxx::__normal_iterator<const int*, std::vector<int> >'|
头像被屏蔽
未来明星 发表于 2015-4-9 20:59
 楼主| SstudentT 发表于 2015-4-9 21:01
/*
exercise 9.2
Define a list that holds elements that are deques that hold ints.
*/
[C++] 纯文本查看 复制代码
#include <iostream>
#include<list>
#include<deque>
int main()
{
    using std::list;
    using std::deque;
    list<deque<int>> ls;
    deque<int> deq1={1,1,2,3,5,8,13};
    deque<int>deq2={21,33};
    ls.push_back(deq1);
    ls.push_back(deq2);
    auto it=ls.begin();
    for(;it!=ls.cend();it++)
    {
        for(auto de:*it)
            std::cout<<de<<" ";
        std::cout<<std::endl;
    }
    return 0;
}

非洲黑人 发表于 2015-4-9 21:11
完全看不懂
243201119 发表于 2015-4-9 21:15
看不懂英语
 楼主| SstudentT 发表于 2015-4-9 21:17
本帖最后由 SstudentT 于 2015-4-12 13:26 编辑
未来明星 发表于 2015-4-9 20:59
完全看不懂  英语差 咋办

别提了,我英语比你好不到那里去。
六级至今未敢报名。
要是对编程有兴趣,专业英语必须得学啊!以后经常用的到!
我刚开始看的时候是一个词一个词查的,根本不去记,后来每个不认识的词查了快50遍,我想忘记都难。
经常看就好。
还有,总算有人把沙发拿走了,我等了好久终于让我摆脱了”楼主抢沙发“的罪名。
 楼主| SstudentT 发表于 2015-4-9 21:25

查查单词连起来就懂了!
 楼主| SstudentT 发表于 2015-4-9 21:37
/*
exercise 9.3
What are the constraints on the iterators that form iterator ranges?
*/
Answer: page 331
An iterator range is denoted by a pair of iterators each of which refers to an element, or to one past the last element, in the same.
[begin,end)
 楼主| SstudentT 发表于 2015-4-10 13:06
/*
exercise 9.4
Write a function that takes a pair of iterators to a vector<int> and an int value. Look for that value in the range and return boo indicating whether it was found.
*/
[C++] 纯文本查看 复制代码
#include <iostream>
#include <vector>
bool findVecEle(std::vector<int>::const_iterator,std::vector<int>::const_iterator,int ele);
int main()
{
    std::vector<int> v={0,1,2,3,4,5,6,7,8,9};
    std::cout<<findVecEle(v.cbegin(),v.cend(),13);
    return 0;
}
bool findVecEle(std::vector<int>::const_iterator be,std::vector<int>::const_iterator en,int ele)
{
    while(be!=en&&*be!=ele)
        ++be;
    return (be==en)?false:true;
}

 楼主| SstudentT 发表于 2015-4-10 13:19
本帖最后由 SstudentT 于 2015-4-10 17:10 编辑

/*exercise 9.5
Rewrite the previous program to return an iterator to the requested element. Note that the program must handle the case where the element is not found.
*/
[C++] 纯文本查看 复制代码
#include <iostream>
#include <vector>
std::vector<int>::const_iterator  findVecEle(std::vector<int>::const_iterator,std::vector<int>::const_iterator,int ele);
int main()
{
    std::vector<int> v={0,1,2,3,4,5,6,7,8,9};
    std::cout<<*findVecEle(v.cbegin(),v.cend(),17);
    return 0;
}
std::vector<int>::const_iterator findVecEle(std::vector<int>::const_iterator be,std::vector<int>::const_iterator en,int ele)
{
    while(be!=en&&*be!=ele)
        ++be;
    std::cout<<((be==en)?"Not found! Will output rubbish:":"Found!");
    return be;
}

您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-13 23:30

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表