码迷,mamicode.com
首页 > 编程语言 > 详细

C++primer 13.6.1节练习

时间:2017-09-05 17:00:03      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:locate   div   span   algorithm   void   创建   new   拷贝   copy   

练习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 }

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

C++primer 13.6.1节练习

标签:locate   div   span   algorithm   void   创建   new   拷贝   copy   

原文地址:http://www.cnblogs.com/wuyinfenghappy/p/7479196.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!