练习13.29
不会,因为该类中的数据成员都是内置类型的,而内置类型是没有特定版本的swap的,所以对swap的调用会调用标准库的std::swap;
练习13.30
1 #include <iostream> 2 #include <string> 3 #include <memory> 4 5 using namespace std; 6 7 8 class HasPtr { 9 friend ostream &print(ostream &os, HasPtr &h); 10 friend void swap(HasPtr &lhs, HasPtr &rhs); 11 public: 12 HasPtr(const string &s = string()) : ps(new string(s)), i(0) {} 13 HasPtr(const HasPtr &ptr); 14 HasPtr &operator=(const HasPtr &pt); 15 ~HasPtr() { delete ps; } 16 private: 17 string *ps; 18 int i; 19 }; 20 21 ostream &print(ostream &os, HasPtr &h); 22 inline void swap(HasPtr &lhs, HasPtr &rhs); 23 24 int main() 25 { 26 HasPtr has("hello"); 27 HasPtr have = has; 28 print(cout, have); 29 HasPtr h1("world"); 30 swap(have, h1); 31 print(cout, have); 32 print(cout, h1); 33 system("pause"); 34 return 0; 35 } 36 37 HasPtr::HasPtr(const HasPtr & ptr) : ps(new string(*(ptr.ps))), i(ptr.i) {} 38 39 HasPtr & HasPtr::operator=(const HasPtr & pt) 40 { 41 ps = new string(*pt.ps); 42 i = pt.i; 43 return *this; 44 // TODO: 在此处插入 return 语句 45 } 46 47 ostream & print(ostream & os, HasPtr & h) 48 { 49 os << *h.ps << h.i << endl; 50 return os; 51 // TODO: 在此处插入 return 语句 52 } 53 54 inline void swap(HasPtr & lhs, HasPtr & rhs) 55 { 56 swap(lhs.ps, rhs.ps); 57 cout << "ps already copy" << endl; 58 swap(lhs.i, rhs.i); 59 cout << "i already copy" << endl; 60 }
练习13.31
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 HasPtr { 11 friend ostream &print(ostream &os, HasPtr &h); 12 friend void swap(HasPtr &lhs, HasPtr &rhs); 13 friend bool operator<(HasPtr &lpt, HasPtr &rpt); 14 friend string & out(HasPtr &pt); 15 public: 16 HasPtr(const string &s = string()) : ps(new string(s)), i(0) {} 17 HasPtr(const HasPtr &ptr); 18 HasPtr &operator=(const HasPtr &pt); 19 ~HasPtr() { delete ps; } 20 21 private: 22 string *ps; 23 int i; 24 }; 25 26 ostream &print(ostream &os, HasPtr &h); 27 inline void swap(HasPtr &lhs, HasPtr &rhs); 28 bool operator<(HasPtr &lpt, HasPtr &rpt); 29 string & out(HasPtr &pt); 30 31 int main() 32 { 33 vector<HasPtr> svec; 34 string word; 35 while (cin >> word) 36 { 37 svec.push_back(*new HasPtr(word)); 38 } 39 sort(svec.begin(), svec.end()); 40 for (auto c : svec) 41 { 42 cout << out(c) << endl; 43 } 44 system("pause"); 45 return 0; 46 } 47 48 HasPtr::HasPtr(const HasPtr & ptr) : ps(new string(*(ptr.ps))), i(ptr.i) {} 49 50 HasPtr & HasPtr::operator=(const HasPtr & pt) 51 { 52 auto q = new string(*pt.ps); 53 delete ps; 54 ps = q; 55 i = pt.i; 56 return *this; 57 } 58 59 string & out(HasPtr & pt) 60 { 61 return *pt.ps; 62 } 63 64 bool operator<(HasPtr & lpt, HasPtr &rpt) 65 { 66 return (*lpt.ps).size() < (*rpt.ps).size(); 67 } 68 69 ostream & print(ostream & os, HasPtr & h) 70 { 71 os << *h.ps << h.i << endl; 72 return os; 73 } 74 75 inline void swap(HasPtr & lhs, HasPtr & rhs) 76 { 77 using std::swap; 78 swap(lhs.ps, rhs.ps); 79 cout << "ps already copy" << endl; 80 swap(lhs.i, rhs.i); 81 cout << "i already copy" << endl; 82 }
何时调用的swap函数不得而知。(疑问)
练习13.32
相比于类值版本的swap函数,指针版本交换的数据成员原本就是指针和整型,并没有优化的效果,所以应该没有得到益处。
时间: 2024-10-26 08:06:54