C++primer 13.6.3 sessions

Source: Internet
Author: User

Exercise 13.55

1 void push_back (conststring& S) &&;

Exercise 13.56

Copy a copy at this time, but the problem comes, ret is an lvalue, return his sorted function, will continue to recursively, and the function does not have a termination condition, so the final stack overflow, causing the program to terminate abnormally;

Exercise 13.57

At this point the function returns a temporary object's sorted function, and the temporary object is an rvalue, then the right value of the sorted overload function is called, the program runs normally, but the original object is still not sorted, because he is a const or an lvalue, changing the order of the elements of the temporary object only, Unfortunately, the temporary object is destroyed after the function is run. Note that the previous question is an lvalue that acts on the entire function body scope. In short, he has an address.

Exercise 13.58

1#include <iostream>2#include <string>3#include <memory>4#include <vector>5#include <algorithm>6#include <numeric>7 8 using namespacestd;9 Ten classFoo { OneFriendvoidPrintConstFoo &); A  Public: - Foo (); -Foo (vector<int> &a); theFoo (Constfoo&); -foo&operator=(Constfoo&); -Foo (foo &&) noexcept; -Foo &operator= (foo&&) noexcept; +Foo Sorted ()Const&; -Foo Sorted () &&; + Private: Avector<int>data; at }; -  - voidPrintConstFoo &); -  - intMain () - { invector<int> vec{1,3,5,2,6,3,7,4,0 }; - Foo F1; to Foo F2 (VEC); + print (F1); - print (F2); theF1 =Std::move (F2); * print (F1); $ print (F2);Panax Notoginseng Foo f3 (VEC); - print (f3.sorted ()); the     //Print (Std::move (F3). sorted ()); + print (F3); ASystem"Pause"); the     return 0; + } -  $ Foo::foo () $ { -vector<int> Data1 (Ten,0); -data =data1; the } - WuyiFoo::foo (vector<int>&a) the { -data =A; Wu } -  AboutFoo::foo (ConstFoo &f) $ { -data =F.data; - } -  AFoo & foo::operator=(ConstFoo &f) + { thedata =F.data; -     return* This; $     //TODO: Insert a return statement here the } the  theFoo::foo (Foo &&f) noexcept the { -data =Std::move (f.data); in } the  theFoo & foo::operator= (Foo &&f) noexcept About { thedata =Std::move (f.data); the     return* This; the     //TODO: Insert a return statement here + } -  theFoo foo::sorted ()Const&Bayi { theFOO RET (* This); the     /*sort (Ret.data.begin (), Ret.data.end ()); - cout << "1" << Endl;*/ -cout << &ret <<Endl; the     returnret.sorted (); the     //return Foo (*this). sorted (); the } the  -Foo foo::sorted () && the { the sort (Data.begin (), Data.end ()); thecout <<"0"<<Endl;94     return* This; the } the  the voidPrintConstFoo &f)98 { About      for(auto C:f.data) -cout << C <<" ";101cout <<Endl;102}

C++primer 13.6.3 sessions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.