连接两个动态字符串数组
Concatenating two dynamic string 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;
}
以上是我的代码(到目前为止(。我试图完成的是将 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;
}
- 分段故障 11,从类函数显示动态 C 字符串
- 删除动态字符串时程序崩溃
- C++动态字符串的动态数组
- 当有1个以上的对象时,动态字符串类断裂与SEG故障
- 动态字符串阵列构造函数和空返回
- 动态 c 字符串类的重载>>运算符
- 调整动态字符串的大小会导致内存泄漏
- Objective-C 预处理器定义,动态 C 字符串到 NSString 声明
- 连接两个动态字符串数组
- 动态字符串网格C++生成器
- 当我重载动态字符串类的运算符时没有匹配的构造函数
- 动态字符串数组分配错误
- 如何在VS调试器中查看c++中动态字符串数组的内容
- c++动态字符串数组分配
- 不使用矢量的动态字符串数组
- 动态字符串数组容器类在delete[]时挂起
- 初始化动态字符串数组(c++)
- 创建动态字符串数组时出现Seg错误
- 无法从函数返回动态字符串
- 如何使动态字符串在控制台中使用UTF-8 ?