在String中添加移动构造函数和移动赋值运算符

简介: 13.50 没有定义析构函数 #include #include #include #include #include #include using namespace std; class String { public: String():elements(...

13.50 没有定义析构函数

#include<iostream>
#include<string>
#include<memory>
#include<utility>
#include<cstring>
#include<vector>
using namespace std;

class String
{
public:
    String():elements(nullptr),first_free(nullptr) {}
    String(char *c);
    String(const String&);
    String& operator=(const String&);
    string* begin() const { return elements;}
    string* end() const { return first_free;}

    String(String &&);
    String& operator=(String &&);
private:
    static allocator<string> alloc;
    string *elements;
    string *first_free;
};

allocator<string> String::alloc;
String::String(char *c)
{
    size_t capacity=strlen(c);
    auto data=alloc.allocate(capacity);
    auto dest=data;
    string s;
    s.copy(c,strlen(c));
    alloc.construct(dest++,s);
    elements=data;
    first_free=dest;
}

String::String(const String &s)
{
    cout<<"copy construct"<<endl;
    auto capacity=s.end()-s.begin();
    auto data=alloc.allocate(capacity);
    uninitialized_copy(s.begin(),s.end(),data);
    elements=data;
    first_free=data+capacity;
}

String& String::operator=(const String &s)
{
    cout<<"copy = construct"<<endl;
    auto capacity=s.end()-s.begin();
    auto data=alloc.allocate(capacity);
    uninitialized_copy(s.begin(),s.end(),data);
    if(elements)
    {
        auto begin=elements;
        auto end=first_free;
        while(begin!=end)
            alloc.destroy(begin++);
        alloc.deallocate(elements,first_free-elements);
    }
    elements=data;
    first_free=data+capacity;
    return *this;
}

String::String(String &&s):elements(s.elements),first_free(s.first_free)
{
    cout<<"move construct"<<endl;
    s.elements=s.first_free=nullptr;
}

String& String::operator=(String &&s)
{
    cout<<"move = construct"<<endl;
    if(this!=&s)
    {
        if(elements)
        {
            auto begin=elements;
            auto end=first_free;
            while(begin!=end)
                alloc.destroy(begin++);
            alloc.deallocate(elements,first_free-elements);
        }
    }
    elements=s.elements;
    first_free=s.first_free;
    s.elements=s.first_free=nullptr;
    return *this;
}

int main()
{
    vector<String> vec;
    char ch[]="hello";
    char ch1[]="world!";
    cout<<vec.capacity()<<endl;
     cout<<endl;
    vec.push_back(String(ch));
    cout<<vec.capacity()<<endl;
    cout<<endl;
    vec.push_back(String(ch1));
    cout<<vec.capacity()<<endl;
    cout<<endl;
    vec.push_back(String(ch));
    cout<<vec.capacity()<<endl;
    cout<<endl;
    vec.push_back(String(ch1));
    cout<<vec.capacity()<<endl;
    cout<<endl;
    vec.push_back(String(ch1));
    cout<<vec.capacity()<<endl;
    cout<<endl;
    vec.push_back(String(ch1));
    cout<<vec.capacity()<<endl;
    return 0;
}

运行结果如下:  结果中出现移动构造函数是因为调用String构造函数返回的结果是右值

定义析构函数时:

#include<iostream>
#include<string>
#include<memory>
#include<utility>
#include<cstring>
#include<vector>
using namespace std;

class String
{
public:
    String():elements(nullptr),first_free(nullptr){}
    String(char *c);
    String(const String&);
    String& operator=(const String&);
    string* begin() const { return elements;}
    string* end() const { return first_free;}
    //一定要定义析构函数,否则就算定义了移动构造函数还是不会调用,只会调用拷贝构造函数
    ~String()
    {
        if(elements)
        {
            auto begin=elements;
            auto end=first_free;
            while(begin!=end)
                alloc.destroy(begin++);
            alloc.deallocate(elements,first_free-elements);
        }
    }
    String(String &&) noexcept;
    String& operator=(String &&) noexcept;
private:
    static allocator<string> alloc;
    string *elements;
    string *first_free;
};

allocator<string> String::alloc;
String::String(char *c)
{
    size_t capacity=strlen(c);
    auto data=alloc.allocate(capacity);
    auto dest=data;
    string s;
    s.copy(c,strlen(c));
    alloc.construct(dest++,s);
    elements=data;
    first_free=dest;
}

String::String(const String &s)
{
    cout<<"copy construct"<<endl;
    auto capacity=s.end()-s.begin();
    auto data=alloc.allocate(capacity);
    uninitialized_copy(s.begin(),s.end(),data);
    elements=data;
    first_free=data+capacity;
}

String& String::operator=(const String &s)
{
    cout<<"copy = construct"<<endl;
    auto capacity=s.end()-s.begin();
    auto data=alloc.allocate(capacity);
    uninitialized_copy(s.begin(),s.end(),data);
    if(elements)
    {
        auto begin=elements;
        auto end=first_free;
        while(begin!=end)
            alloc.destroy(begin++);
        alloc.deallocate(elements,first_free-elements);
    }
    elements=data;
    first_free=data+capacity;
    return *this;
}

String::String(String &&s) noexcept :elements(s.elements),first_free(s.first_free)
{
    cout<<"move construct"<<endl;
    s.elements=s.first_free=nullptr;
}

String& String::operator=(String &&s) noexcept
{
    cout<<"move = construct"<<endl;
    if(this!=&s)
    {
        if(elements)
        {
            auto begin=elements;
            auto end=first_free;
            while(begin!=end)
                alloc.destroy(begin++);
            alloc.deallocate(elements,first_free-elements);
        }
    }
    elements=s.elements;
    first_free=s.first_free;
    s.elements=s.first_free=nullptr;
    return *this;
}

int main()
{
    vector<String> vec;
    char ch[]="hello";
    char ch1[]="world!";
    cout<<vec.capacity()<<endl;
     cout<<endl;
    String ss(ch);
    vec.push_back(ss);
    cout<<vec.capacity()<<endl;
    cout<<endl;
    vec.push_back(String(ch1));
    cout<<vec.capacity()<<endl;
    cout<<endl;
    vec.push_back(String(ch));
    cout<<vec.capacity()<<endl;
    cout<<endl;
    vec.push_back(String(ch1));
    cout<<vec.capacity()<<endl;
    cout<<endl;
    vec.push_back(String(ch1));
    cout<<vec.capacity()<<endl;
    cout<<endl;
    vec.push_back(String(ch1));
    cout<<vec.capacity()<<endl;
    cout<<"\n";

    std::vector<String> v;
    String s;
    for (unsigned i = 0; i != 4; ++i)
    {
        std::cout << v.capacity() << "\n";
        v.push_back(s);
    }
    return 0;
}

运行结果如下:

相关文章
|
C++ 索引
【C++STL基础入门】深入理解string类重新赋值(assign)与删除(erase)
【C++STL基础入门】深入理解string类重新赋值(assign)与删除(erase)
250 1
|
存储 Java
Java初始String和运算符
Java初始String和运算符
|
5月前
|
C++ 容器
C++字符串string容器(构造、赋值、拼接、查找、替换、比较、存取、插入、删除、子串)
C++字符串string容器(构造、赋值、拼接、查找、替换、比较、存取、插入、删除、子串)
TS定义布尔值,let flag:boolean = true,定义数字类型 let a1:number = 10,赋值 let str1:string = ‘‘,打印c~.log($(str1))
TS定义布尔值,let flag:boolean = true,定义数字类型 let a1:number = 10,赋值 let str1:string = ‘‘,打印c~.log($(str1))
|
6月前
|
JavaScript 前端开发
js基础语法:包括变量声明、数据类型(Number, String, Boolean, Null, Undefined, Symbol, Object)、运算符、流程控制语句(if...else, switch, for, while, do...while)等。具体案例使用演示
js基础语法:包括变量声明、数据类型(Number, String, Boolean, Null, Undefined, Symbol, Object)、运算符、流程控制语句(if...else, switch, for, while, do...while)等。具体案例使用演示
75 1
|
6月前
|
JavaScript 前端开发
JavaScript基础语法:包括变量声明、数据类型(Number, String, Boolean, Null, Undefined, Symbol, Object)、运算符、流程控制语句(if...else, switch, for, while, do...while)等。
JavaScript基础语法:包括变量声明、数据类型(Number, String, Boolean, Null, Undefined, Symbol, Object)、运算符、流程控制语句(if...else, switch, for, while, do...while)等。
64 0
|
存储 安全 Java
傻傻分不清楚?: 一文看懂Java中String的New操作和直接赋值字符串的区别
傻傻分不清楚?: 一文看懂Java中String的New操作和直接赋值字符串的区别
1204 0
傻傻分不清楚?: 一文看懂Java中String的New操作和直接赋值字符串的区别
|
存储 Java 索引
[oeasy]python0071_字符串类型_str_string_下标运算符_中括号
[oeasy]python0071_字符串类型_str_string_下标运算符_中括号
83 0
|
JavaScript 前端开发
使用 JavaScript 中的变量、数据类型和运算符,计算出两个 number 类型的变量与一个 string 类型的变量的和,根据 string 类型处于运算符的不同位置得到不同的结果
使用 JavaScript 中的变量、数据类型和运算符,计算出两个 number 类型的变量与一个 string 类型的变量的和,根据 string 类型处于运算符的不同位置得到不同的结果
138 0
使用 JavaScript 中的变量、数据类型和运算符,计算出两个 number 类型的变量与一个 string 类型的变量的和,根据 string 类型处于运算符的不同位置得到不同的结果
详解C++ 编写String 的构造函数、拷贝构造函数、析构函数和赋值函数
详解C++ 编写String 的构造函数、拷贝构造函数、析构函数和赋值函数
395 0