相关疑难解决方法(0)

为什么字符串和向量不同类型?

它们都是可调整大小的数组,而std :: basic_string没有任何特定于字符的函数,如upper().字符串有什么特别之处,可以让字符数据变得更好?

c++ string stl vector

6
推荐指数
2
解决办法
847
查看次数

尝试编写可以从std :: string移动语义的字符串类

我正在编写自己的字符串类,仅仅是为了学习和巩固一些知识.我有一切工作,除了我想有一个使用移动语义与std :: string的构造函数.

在我的构造函数中,我需要复制并清空std :: string数据指针和其他东西,它需要保留为空但有效的状态,而不删除字符串指向的数据,我该怎么做?

到目前为止,我有这个

class String
{
private:
char* mpData;
unsigned int mLength;
public:
String( std::string&& str)
    :mpData(nullptr), mLength(0)
    {
    // need to copy the memory pointer from std::string to this->mpData

    // need to null out the std::string memory pointer
    //str.clear();  // can't use clear because it deletes the memory
    }
~String()
{
delete[] mpData;
mLength = 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ stdstring dynamic-memory-allocation move-semantics c++11

3
推荐指数
1
解决办法
609
查看次数