连接两个动态字符串数组?

时间:2016-02-13 20:41:44

标签: c++ arrays

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;
}

以上是我的代码(到目前为止)。我想要完成的是将动态数组连接到b的动态数组(我不能使用向量或任何STL,这是对矢量的初步模仿)。 / p>

示例:

用户为a输入大小2并输入&#34; Hello&#34;和&#34;世界&#34;。 用户输入b的大小为2并输入&#34; Goodbye&#34;和&#34;世界&#34;。 输出应该是#34; Hello World Goodbye World&#34;。

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

我的推理似乎听起来不太合理,因为坦率地说,我对如何处理这个问题感到很困惑。

1 个答案:

答案 0 :(得分:0)

您的代码与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) {}

此构造函数只构造Vector1num个条目。

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

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;
}