连接两个动态字符串数组

Concatenating two dynamic string arrays?

本文关键字:动态 字符串 数组 两个 连接      更新时间:2023-10-16
template <typename Object>
class Vector1 {
public:
explicit Vector1(const Object & value = Object()) : size_{0} {
    array_ = new Object{value};
    size_++;
}
Vector1(const Vector1 & rhs) : size_{rhs.size_} { //copy constructor
    array_ = new Object[size_];
    for (int i = 0; i < size_; i++) {
        array_[i] = rhs.array_[i];
    }
}
Vector1 & operator=(const Vector1 & rhs) { //copy assignment operator
    array_ = new Object[rhs.size_];
    if (this != &rhs) {
        size_ = rhs.size_;
        for (int i = 0; i < size_; i++) {
            array_[i] = rhs.array_[i];
        }
    }
    return *this; 
}
Vector1(Vector1 && rhs) : array_{rhs.array_}, size_{rhs.size_} { //move constructor
    rhs.array_ = nullptr;
    rhs.size_ = 0;
}
Vector1 & operator=(Vector1 && rhs) { //move assignment operator
    if (this != &rhs) {
        std::swap(size_, rhs.size_);
        std::swap(array_, rhs.array_);
    }
    return *this;
}
void print(ostream & out) const {
    for (int i = 0; i < size_; i++) {
        out << array_[i] << " ";
    }
}
void ReadVector1() {
    int count = 0;
    cout << "Enter a size: ";
    cin >> size_;
    array_ = new Object[size_];
    for (int i = 0; i < size_; i++) {
        cout << "Enter element " << count + 1 << ": ";
        cin >> array_[i];
        count++;
    }
}
size_t Size() const {
    return size_;
}
**Vector1 operator+=(Vector1 & rhs) {
    size_t combosize = size_ + rhs.size_;
    Object combo[combosize];
    int count = 0, rhscount = 0;
    for (int i = 0; i < combosize; i++) {
        if (i % 2 == 0) {
            combo[i] = array_[count];
            count++;
        }
        else {
            combo[i] = rhs.array_[rhscount];
            rhscount++;
        }
    }
    std::swap(combosize, rhs.size_);
    std::swap(combo, rhs.array_);
    return *this;
}
Vector1 operator+(const Vector1 & rhs) const {
    Vector1 temp(*this);
    temp += rhs;
    return temp;
}**
~Vector1() { //destructor
    delete[] array_;
}
private:
    size_t size_; 
    Object *array_;
};
template <typename Object>
ostream & operator<<(ostream & out, const Vector1<Object> & rhs) {
    rhs.print(out);
    return out;
}

int main(int argc, char **argv) {
   Vector1<string> a, b;
   a.ReadVector1(); //user provides input for Vector1 a
   cout << a << endl;
   b.ReadVector1(); //user provides input for Vector1 b
   cout << b << endl; 
   cout << a + b << endl; //concatenates the two Vector1s
   Vector1<string> d = a + b;
   cout << d;
   return 0;
}

以上是我的代码(到目前为止(。我试图完成的是将 a 的动态数组与 b 的动态数组连接起来(我不能使用向量或任何 STL,这意味着对向量的基本模仿(。

例:

用户输入大小为 2 的 a,然后输入"Hello"和"World"。用户为 b 输入大小 2,然后输入"再见"和"世界"。输出应为"你好世界再见世界"。

我强调了我认为的问题是什么,即 + 和 += 运算符的重载。我的理由是我创建一个新数组,用 a 和 b 的值填充它,交换值,然后返回假定的串联。

我的推理可能看起来不合理,因为坦率地说,我对如何进行这项工作感到非常困惑。

您的代码在operator +=方面存在一些问题。

首先,operator +=应该返回对当前对象的引用,即 return *this; . 它不应该返回全新的Vector1对象。

二、这一行:

size_t combosize = size_ + rhs.size_;
Object combo[combosize];

不是有效的 ANSI C++,因为必须使用编译时表达式声明数组来表示条目数。 由于combosize是运行时值,因此无法使用。

您可能正在使用GCC编译器,其中有一个可变长度数组扩展,但同样,这是一个扩展,并不是C++语言的一部分。

此外,您还缺少一些可以使编写operator +=变得更加容易的构造函数。 您缺少的Vector1的构造函数是这个:

Vector1::Vector1(size_t num) : array_(new Object[num]), size_(num) {}

此构造函数仅构造一个包含num条目的Vector1


鉴于上述情况,要解决这些问题:

Vector1& operator+=(const Vector1 & rhs) 
{
    // create a temporary vector 
    Vector1 temp(size_ + rhs.size_);
    // copy elements to temp array
    for (int i = 0; i < size_; i++) 
       temp.array_[i] = array_[i];
    // copy elements from rhs to temp array
    int j = 0;
    for (int i = size_; i < size_ + rhs.size_; i++, j++) 
       temp.array_[i] = rhs.array_[j];
    // assign and return
    *this = temp;
    return *this;
}
Vector1 operator+(const Vector1 & rhs) const 
{
    Vector1 temp(*this);
    temp += rhs;
    return temp;
}

请注意,在operator +=中,我们只创建一个临时Vector1,用 *this 和传入的值填充它 Vector1 ,并将其分配给当前对象。 *this = temp;行需要工作分配操作员。 请参阅下面的下一节。


另一个问题是您的Vector1::operator=不正确。 它不会释放分配给以前分配给_array的内存,因此存在内存泄漏。

解决此问题的最简单方法是使用您在operator=(Vector1 &&)中使用的复制/交换:

Vector1 & operator=(const Vector1 & rhs) 
{ 
    Vector1 temp(rhs);
    std::swap(size_, temp.size_);
    std::swap(array_, temp.array_);
    return *this;
}