Accelerated C++:通过示例进行编程实践——习题解答(第1章)

Accelerated C++:通过示例进行编程实践——习题解答(第1章)


1-0. Compile, execute, and test the programs in this chapter.


#include <iostream>  
#include <string>  
  
using namespace std;  
  
int main()  
{  
    cout<<"input your name:";  
    string name;  
    cin>>name;  
  
    const string greet="Hello, "+name+"!";  
    const string row3="* "+greet+" *";  
    const string row2Space(greet.size()+2,' ');  
    const string row2="*"+row2Space+"*";  
    const string row1(row2.size(),'*');  
  
    cout<<row1<<endl<<row2<<endl<<row3<<endl<<row2<<endl<<row1<<endl;  
  
    return 0;  
}  


lyj@qt:~/Desktop$ g++ 1-0.cpp -o 1-0
lyj@qt:~/Desktop$ ./1-0
input your name:tianya
******************
*                       *
* Hello, tianya! *
*                       *
******************
lyj@qt:~/Desktop$ 


1-1. Are the following definitions valid? Why and why not?

const std::string hello = “Hello”;

const std::string message = hello + “, world” + “!”;

Answer:  Yes.

1-2. Are the following definitions valid? Why and why not?

const std::string exclam = “!”;

const std::string message1 = “Hello” + “, world” + exclam;

Answer: Unfortunately, the second statement will cause an “invalid operands to binary expressions(‘const char*’ and ‘const char*’). Which happend at ‘”Hello” + “, world”‘.

There is no ‘+’ operator to add two C-style strings – char *, char[]. while the “Hello” and “, world” alike strings are actually C-style strings. But the string from standard library contains the ‘+’ operator which should do the right job. So simply change the code to the following and it will work:

const std::string exclam = “!”;

const std::string message1 = std::string(“Hello”) + std::string(“, world”) + exclam;

1-3. Is the following program valid? If so, what does it do? If not, why not?
#include <iostream>
#include <string>
int main()
{
    { const std::string s = "a string";
      std::cout << s << std::endl; }
   
    { const std::string s = "another string";
      std::cout << s << std::endl; }
    return 0;
}

Ans:"{}"的作用,变量作用范围问题。

1-4. What about this one? What if we change }} to };} in the third line from the end?
#include <iostream>
#include <string>
int main()
{
    { const std::string s = "a string";
      std::cout << s << std::endl;
    { const std::string s = "another string";
      std::cout << s << std::endl; }}
    return 0;
}

Ans:同上题,将}}修改为};}依然输出a string \n another string;添加的";"是一条空语句。

1-5. Is this program valid? If so, what does it do? If not, say why not, and rewrite it to be valid.
#include <iostream>
#include <string>
int main()
{
    { std::string s = "a string";
    { std::string x = s + ", really";
    std::cout << s << std::endl; }
    std::cout << x << std::endl;
    }
    return 0;
}

Ans:invalid,因为最后一条cout引用了未定义的变量x,x在最内层{}内有效,将最内层{}去除即可。

1-6. What does the following program do if, when it asks you for input, you type two names (for example, Samuel Beckett)? Predict the behavior before running the program, then try it.
#include <iostream>
#include <string>
int main()
{
    std::cout << "What is your name? ";
    std::string name;
    std::cin >> name;   name1
    std::cout << "Hello, " << name<< std::endl << "And what is yours? ";

    std::cin >> name;//name2
    std::cout << "Hello, " << name<< "; nice to meet you too!" << std::endl;
    return 0;
}

Ans:输入name1,输出Hello, name1 And what is yours?然后要求输入name2,输出Hello, name2;nice to meet you too!


参考链接:

http://blog.csdn.net/yj_cs/article/details/39674105

https://acceleratedcplusplus.wordpress.com/


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
If you don’t have a lot of time, but still want to learn the latest in C++, you don’t have to learn C first. You might learn more by digging into current language features and classes from the very beginning. That’s the approach that’s offered by Accelerated C++, a text that delves into more advanced C++ features like templates and Standard Template Library (STL) collection classes early on. This book arguably can get a motivated beginning programmer into C++ more quickly than other available tutorials. What separates this title from the rest of the pack is that it jumps right in with samples that take advantage of the Standard C++ of today–from streams to built-in container classes, such as vectors and linked lists. Early examples are more complex than in other books, but the thoroughness and relaxed pace of the explanations will bring the novice up to speed. (Although it ships at a slender 350 pages, Accelerated C++ is packed with samples, tips, and example problems; about 10 per chapter.) After a tour of basic C++, the book looks at more advanced C++ features, such as templates, including built-in support for containers. Besides tapping the strength of Standard C++, you also will learn to design with your own templates. (Other tutorials would defer this material until later on.) The authors have tested the approach in the book in their own teaching, and honed a set of worthwhile examples that will help anyone get familiar with these powerfullanguage features . All examples make use of the command line and console (without GUI programs), but the advantage is that this code should run on any of today’s operating systems and compilers. Later sections cover the basics of class design, which include good coverage of operator overloading and inheritance. With its innovative approach to teaching the language, Accelerated C++ will challenge readers in the right way. It suggests that you don’t need to learn C to be productive in C++. Written in an approachable style, it deserves a close look from any C++ novice. –Richard Dragan
本书全面介绍了C++语言。作为一本入门书(Primer),它以教程的形式对C++语言进行清晰的讲解,并辅以丰富的示例和各种学习辅助手段。与大多数入门教程不同,本书对C++语言本身进行了详尽的描述,并特别着重介绍了目前通行的、行之有效的程序设计技巧。   无数程序员曾使用本书的前几个版本学习C++,在此期间C++也逐渐发展成熟。这些年来,C++语言的发展方向以及C++程序员的关注点,已经从以往注重运行时的效率,转到千方百计地提高程序员的编程效率上。随着标准库的广泛可用,我们现在能够比以往任何时候更高效地学习和使用C++。本书这一版本充分体现了这一点。第4版的改动为了体现现代C++编程风格,我们重新组织并重写了本书。书中不再强调低层编程技术,而把中心转向标准库的使用。书中很早就开始介绍标准库,示例也已经重新改写,充分利用了标准库设施。我们也对语言主题叙述的先后次序进行了重新编排,使讲解更加流畅。除重新组织内容外,为了便于读者理解,我们还增加了几个新的环节。每一都新增了“小结”和“术语”,概括本要点。读者可以利用这些部分进行自我检查;如果发现还有不理解的概念,可以重新学习该中的相关部分。书中还加入了下述几种学习辅助手段:重要术语用黑体表示,我们认为读者已经熟悉的重要术语则用楷体表示。这些术语都会出现在后的“术语”部分。书中用特殊版式突出标注的文字,是为了向读者提醒语言的重要特征,警示常见的错误,标明良好的编程实践,列出通用的使用技巧。希望这些标注可以帮助读者更快地消化重要概念,避免犯常见错误。为了更易于理解各种特征或概念间的关系,书中大量使用了前后交叉引用。.. 对于某些重要概念和C++新手最头疼的问题,我们进行了额外的讨论和解释。这部分也以特殊版式标出。学习任何程序设计语言都需要编写程序。因此,本书提供了大量的示例。所有示例的源代码可从下列网址获得: http://www.awprofessional.com/cpp_primer 万变不离其宗,本书保持了前几版的特色,仍然是一部全面介绍C++的教程。我们的目标是提供一本清晰、全面、准确的指南性读物。我们通过讲解一系列示例来教授C++语言,示例除了解释语言特征外,还展示了如何善用这门语言。虽然读者不需要事先学过C语言(C++最初的基础)的知识,但我们假定读者已经掌握了一种现代结构化语言。本书结构本书介绍了C++国际标准,既涵盖语言的特征,又讲述了也是标准组成部分的丰富标准库。C++的强大很大程度上来自它支持抽象程序设计。要学会用C++高效地编程,只是掌握句法和语义是远远不够的。我们的重点,在于教会读者怎样利用C++的特性,快速地写出安全的而且性能可与C语言低层程序相媲美的程序。 C++是一种大型的编程语言,这可能会吓倒一些新手。现代C++可以看成由以下三部分组成: l 低级语言,多半继承自C。 l 更高级的语言特征,用户可以借此定义自己的数据类型,组织大规模的程序和系统。 l 标准库,使用上述高级特征提供一整套有用的数据结构和算法。多数C++教材按照下面的顺序展开:先讲低级细节,再介绍更高级的语言特征;在讲完整个语言后才开始解释标准库。结果往往使读者纠缠于低级的程序设计问题和复杂类型定义的编写等细节,而不能真正领会抽象编程的力量。就更不用说学到足够的知识去创建自己的抽象了。本版中我们独辟蹊径。一开始就讲述语言的基础知识和标准库,这样读者就可以写出比较大的有实际意义的程序来。透彻阐释了使用标准库(并且用标准库编写了各种抽象程序)的基础知识之后,我们才进入下一步,学习用C++的其他高级特征,来编写自己的抽象。第一和第二部分讨论语言的基础知识和标准库设施。其重点在于学会如何编写C++程序,如何使用标准库提供的抽象设施。大部分C++程序员需要了解本书这两部分的内容。除了讲解基础知识以外,这两部分还有另外一个重要的意图。标准库设施本身是用C++编写的抽象数据类型,定义标准库所使用的是任何C++程序员都能使用的构造类的语言特征。我们教授C++的经验说明,一开始就使用设计良好的抽象类型,读者会更容易理解如何建立自己的类型。第三到第五部分着重讨论如何编写自己的类型。第三部分介绍C++的核心,即对类的支持。类机制提供了编写自定义抽象的基础。类也是第四部分中所讨论的面向对象编程和泛型编程的基础。全书正文的最后是第五部分,我们在这一部分讨论了一些高级特征,它们在构建大型复杂系统时最为常用。致谢与前几版一样,我们要感谢Bjarne Stroustrup,他不知疲倦地从事着C++方面的工作,他与我们的深厚友情由来已久。我们还要感谢Alex Stepanov,正是他最初凭借敏锐的洞察力创造了容器和算法的概念,这些概念最终形成了标准库的核心。此外,我们要感谢C++标准委员会的所有成员,他们多年来为C++

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值