C++primer 13.6.1节练习

练习13.45

右值引用:使用&&,他是必须绑定到右值的引用,右值短暂,要么是字面常量,要么是表达式求职过程中的创建的临时对象;不能将一个右值引用绑定到一个右值引用类型的变量上;

练习13.46

 1 #include <iostream>
 2 #include <string>
 3 #include <utility>
 4 #include <memory>
 5 #include <algorithm>
 6 #include <vector>
 7 
 8 using namespace std;
 9 
10 int f() {
11     return 3;
12 }
13 
14 int main()
15 {
16     vector<int> vi(100);
17     int&& r1 = f();
18     int& r2 = vi[0];
19     int& r3 = r1;
20     int&& r4 = vi[0] * f();
21     system("pause");
22     return 0;
23 }

练习13.47

  1 #include <iostream>
  2 #include <string>
  3 #include <utility>
  4 #include <memory>
  5 #include <algorithm>
  6 
  7 using namespace std;
  8 
  9 class String {
 10     friend ostream &print(ostream &os, String &s);
 11 public:
 12     String(): element(nullptr), first_free(nullptr) {}
 13     String(char *);
 14     size_t size() const{ return first_free - element; }
 15     String(const String&);
 16     String& operator=(const String&);
 17     ~String() { free(); }
 18 private:
 19     static allocator<char> alloc;
 20     pair<char *, char *> alloc_n_copy(const char*, const char *);
 21     void free();
 22     char *element;
 23     char *first_free;
 24 };
 25 allocator<char> String::alloc;
 26 ostream &print(ostream &os, String &s);
 27 
 28 int main()
 29 {
 30     String s1;
 31     String s2("hello");
 32     String s3("hello world");
 33     String s4(s2);
 34     s1 = s3;
 35     print(cout, s1);
 36     print(cout, s2);
 37     print(cout, s3);
 38     system("pause");
 39     return 0;
 40 }
 41 
 42 String::String(char *s)
 43 {
 44     int i = 0;
 45     while (s[i] != '\0')
 46         i = i + 1;
 47     auto newloc = alloc.allocate(i);
 48     auto dest = newloc;
 49     for (auto count = 0; count != i;++count)
 50         alloc.construct(dest++, s[count]);
 51     element = newloc;
 52     first_free = dest;
 53 }
 54 
 55 String::String(const String &s)
 56 {
 57     auto newdata = alloc_n_copy(s.element, s.first_free);
 58     element = newdata.first;
 59     first_free = newdata.second;
 60     cout << "拷贝构造函数" << endl;
 61 }
 62 
 63 String & String::operator=(const String &rhs)
 64 {
 65     auto newdata = alloc_n_copy(rhs.element, rhs.first_free);
 66     free();
 67     element = newdata.first;
 68     first_free = newdata.second;
 69     cout << "拷贝赋值运算符" << endl;
 70     return *this;
 71     // TODO: 在此处插入 return 语句
 72 }
 73 
 74 pair<char*, char*> String::alloc_n_copy(const char *b, const char *e)
 75 {
 76     auto data = alloc.allocate(e - b);
 77     return{ data,uninitialized_copy(b,e,data) };
 78 }
 79 
 80 void String::free()
 81 {
 82     if (element)
 83     {
 84         for (auto p = first_free; p != element;)
 85             alloc.destroy(--p);
 86         alloc.deallocate(element,first_free - element);
 87     }
 88 }
 89 
 90 ostream & print(ostream &os,String &s)
 91 {
 92     while (s.element != s.first_free)
 93     {
 94         os << *(s.element);
 95         s.element++;
 96     }
 97     cout << endl;
 98     return os;
 99     // TODO: 在此处插入 return 语句
100 }

该题的代码存在内存释放的问题,请大家指正

练习13.48

  1 #include <iostream>
  2 #include <string>
  3 #include <utility>
  4 #include <memory>
  5 #include <algorithm>
  6 #include <vector>
  7 
  8 using namespace std;
  9 
 10 class String {
 11     friend ostream &print(ostream &os, String &s);
 12 public:
 13     String(): element(nullptr), first_free(nullptr) {}
 14     String(char *);
 15     size_t size() const{ return first_free - element; }
 16     String(const String&);
 17     String& operator=(const String&);
 18     ~String() { free(); }
 19 private:
 20     static allocator<char> alloc;
 21     pair<char *, char *> alloc_n_copy(const char*, const char *);
 22     void free();
 23     char *element;
 24     char *first_free;
 25 };
 26 allocator<char> String::alloc;
 27 ostream &print(ostream &os, String &s);
 28 
 29 int main()
 30 {
 31     vector<String> vec;
 32     String s1("hello");
 33     String s2("world");
 34     String s3(s1);
 35     String s4 = s2;
 36     s3 = s1;
 37     vec.push_back(s1);
 38     vec.push_back(s2);
 39     vec.push_back(s3);
 40     vec.push_back(s4);
 41      system("pause");
 42     return 0;
 43 }
 44 
 45 String::String(char *s)
 46 {
 47     int i = 0;
 48     while (s[i] != '\0')
 49         i = i + 1;
 50     auto newloc = alloc.allocate(i);
 51     auto dest = newloc;
 52     for (auto count = 0; count != i;++count)
 53         alloc.construct(dest++, s[count]);
 54     element = newloc;
 55     first_free = dest;
 56 }
 57 
 58 String::String(const String &s)
 59 {
 60     auto newdata = alloc_n_copy(s.element, s.first_free);
 61     element = newdata.first;
 62     first_free = newdata.second;
 63     cout << "拷贝构造函数" << endl;
 64 }
 65 
 66 String & String::operator=(const String &rhs)
 67 {
 68     auto newdata = alloc_n_copy(rhs.element, rhs.first_free);
 69     free();
 70     element = newdata.first;
 71     first_free = newdata.second;
 72     cout << "拷贝赋值运算符" << endl;
 73     return *this;
 74     // TODO: 在此处插入 return 语句
 75 }
 76 
 77 pair<char*, char*> String::alloc_n_copy(const char *b, const char *e)
 78 {
 79     auto data = alloc.allocate(e - b);
 80     return{ data,uninitialized_copy(b,e,data) };
 81 }
 82 
 83 void String::free()
 84 {
 85     if (element)
 86     {
 87         for (auto p = first_free; p != element;)
 88             alloc.destroy(--p);
 89         alloc.deallocate(element,first_free - element);
 90     }
 91 }
 92 
 93 ostream & print(ostream &os,String &s)
 94 {
 95     while (s.element != s.first_free)
 96     {
 97         os << *(s.element);
 98         s.element++;
 99     }
100     cout << endl;
101     return os;
102     // TODO: 在此处插入 return 语句
103 }

上一题的问题已解决,可能是编译器抽风。。。。

转载于:https://www.cnblogs.com/wuyinfenghappy/p/7479196.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
c primer 15.9作业是关于异常处理的内容。在这一中,主要介绍了C语言中的错误处理机制和异常处理方式。 异常处理是一种程序设计中的重要思想,它允许应对出现的各种错误或异常情况,从而增加程序的健壮性和可靠性。C语言中的异常处理主要通过使用错误码和错误处理函数来实现。 在进行异常处理时,通常需要先定义一些错误码,用于标识可能出现的异常情况。C语言提供了一些标准的错误码,如errno.h头文件中定义的错误码,还可以根据需要自定义错误码。 接下来,我们需要在程序中合适的位置进行错误检测并进行异常处理。可以使用if语句或者switch语句等条件语句来检测错误码,并根据不同的错误码执行相应的错误处理代码。 错误处理代码的内容可以根据具体情况而定,它可以是打印错误信息、修复错误、返回错误码等操作。在处理完异常后,可以继续执行后续的程序逻辑,或者返回到调用处继续处理。 除了使用错误码和错误处理函数进行异常处理外,C语言还提供了一种特殊的异常处理方式,即信号处理。信号处理是通过捕捉和处理操作系统发送的信号来实现的,通过注册信号处理函数,可以在程序遇到特定信号时执行相应的处理代码。 总之,C语言中的异常处理是一种重要的错误处理机制,可以提高程序的可靠性和健壮性。通过定义错误码、错误处理函数和信号处理,可以有效地捕捉和处理各种异常情况。在编写C程序时,合理地使用异常处理机制是至关重要的。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值