C++primer 14.3.1节练习

练习14.16

 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 &operator<<(ostream &os, String &s);
11     friend bool operator==(const String& lhs, const String& rhs);
12     friend bool operator!=(const String& lhs, const String& rhs);
13 public:
14     String() : element(nullptr), first_free(nullptr) {}
15     String(char *);
16 private:
17     static allocator<char> alloc;
18     char *element;
19     char *first_free;
20 };
21
22 allocator<char> String::alloc;
23 bool operator==(const String& lhs, const String& rhs);
24 bool operator!=(const String& lhs, const String& rhs);
25
26 int main()
27 {
28     String s1;
29     String s2("ello");
30     String s3("hello world");
31     cout << s1;
32     cout << s2;
33     cout << s3;
34     cout << (s2 == s3);
35     cout << endl;
36     system("pause");
37     return 0;
38 }
39
40 String::String(char *s)
41 {
42     int i = 0;
43     while (s[i] != ‘\0‘)
44         ++i;
45     auto newloc = alloc.allocate(i);
46     auto dest = newloc;
47     for (auto count = 0; count != i;++count)
48         alloc.construct(dest++, s[count]);
49     element = newloc;
50     first_free = dest;
51 }
52
53 ostream & operator<<(ostream &os, String &s)
54 {
55     while (s.element != s.first_free)
56     {
57         os << *(s.element);
58         s.element++;
59     }
60     cout << endl;
61     return os;
62     // TODO: 在此处插入 return 语句
63 }
64
65 ostream &operator<<(ostream &os, bool b);
66
67 bool operator==(const String & lhs, const String & rhs)
68 {
69     auto i = lhs.element;
70     auto j = rhs.element;
71     if ((lhs.first_free - lhs.element) != (rhs.first_free - rhs.element))
72         return false;
73     while ((i != lhs.first_free) && (j != rhs.first_free))
74         if (*(i++) != *(j++))
75             return false;
76 }
77
78 bool operator!=(const String & lhs, const String & rhs)
79 {
80     return !(lhs == rhs);
81 }

这段代码不知道为何结果不对,实在找不出来错误在哪。

 1 bool operator==(const StrVec & lhs, const StrVec & rhs)
 2 {
 3     auto i = lhs.elements;
 4     auto j = rhs.elements;
 5     if (lhs.size() == rhs.size())
 6     {
 7         while (i != lhs.first_free)
 8         {
 9             if (*i == *j)
10             {
11                 ++i;
12                 ++j;
13             }
14         }
15         return true;
16     }
17     else
18         return false;
19 }

类似的代码放在StrVec类中是正确的。

练习14.17

时间: 2024-08-06 16:05:57

C++primer 14.3.1节练习的相关文章

C++primer 14.2.1节练习

练习14.6 1 #include <iostream> 2 #include <string> 3 #include <memory> 4 #include <vector> 5 #include <algorithm> 6 #include <numeric> 7 8 using namespace std; 9 10 class Sales_data { 11 friend istream& operator >&

C++primer 14.2.2节练习

练习14.9 1 istream &operator >> (istream &is, Sales_data &item) 2 { 3 double price; 4 is >> item.bookNo >> item.units_sold >> price; 5 if (is) 6 item.revenue = item.units_sold * price; 7 else 8 item = Sales_data(); 9 retu

C++primer 14.3节练习

练习14.13 对于Sales_data类来说,没有其他的算术运算符需要重载了,对于其它的运算没有意义: 练习14.14 首先operator+有两个参数,其参数类型为const,是不需要改变的,其返回类型为Sales_data类型的一个拷贝.不过每次都需要在函数体内定义一个临时变量,用来返回拷贝. 而operator+=有一个参数,其参数类型为const,不需要改变,其返回类型为Sales_data类型的引用.每次不需要在函数内创建临时变量,直接可返回*this. 如果用operator+来定

C++primer 15.7.3节练习

练习15.26 写的时候不小心写到了派生类Disc_quote,其实是一样的,主要明白原理即可 1 #include <iostream> 2 #include <string> 3 #include <utility> 4 #include <memory> 5 #include <vector> 6 7 using namespace std; 8 9 class Quote { 10 public: 11 Quote() : bookNo(

C++primer 15.2.2节练习

练习15.4 a)错误,一个类不能派生它本身 b)正确,Derived从他的基类Base派生,且规定派生类从基类继承的数据成员对于派生类的用户是不可见. c)错误,派生类的声明与其他类相差不大,声明中包含类名但是不包含他的派生列表: 练习15.5 1 #include <iostream> 2 #include <string> 3 #include <utility> 4 #include <memory> 5 #include <vector>

C++primer 15.7.4节练习

练习15.27 1 #include <iostream> 2 #include <string> 3 #include <utility> 4 #include <memory> 5 #include <vector> 6 7 using namespace std; 8 9 class Quote { 10 public: 11 Quote() : bookNo(""), price(0.0) { cout <<

C++ Primer 6.5.3节练习

练习 6.47: 改写6.3.2节(第205页)练习中使用递归输出vector内容的程序,使其有条件地输出与执行过程有关的信息.例如,每次调用时输出vector对象的大小.分别在打开和关闭调试器的情况下编译并执行这个程序. ///这一题需要在前面输出vector内容的程序中,添加新的功能---->有条件地输出与执行过程有关的信息. 为了简便解答该题,我们采用vector引用,vector的类型是string型,在过程中不改变容器大小. 经过测试得到一个现象,编译器将会按照 #define DEB

C++primer 11.3.5节练习

练习11.27 对于multimap来说统计关键字出现的次数用count会很好,而对于map来说寻找关键字来说更加妥当: 练习11.28 1 #include <iostream> 2 #include <string> 3 #include <set> 4 #include <map> 5 #include <algorithm> 6 #include <vector> 7 #include <algorithm> 8

C++primer 11.2.3节练习

练习11.12 1 #include<iostream> 2 #include<string> 3 #include <iostream> 4 #include <vector> 5 #include <algorithm> 6 #include <list> 7 #include <functional> 8 #include <iterator> 9 #include <map> 10 #inc