将矢量指针移动到 C++ 中矢量的派生 class?
Moving vector pointer to derived class of vector in C++?
我想将向量的指针移动到我的 A 对象 (this) 的向量。我想这样做是因为我使用我的帮助向量(用于合并排序)并且我想要原始向量中的帮助向量的值。然而,我只想使用 1 个操作(因此应该通过移动来完成,而不是复制元素)。
这是我使用的代码:
template<class T>
class A:public vector<T> {
public:
void fillAndMove();
vector<T> help;
}
template<class T>
void A<T>:fillAndMove() {
// Fill a help array with random values
help.resize(2);
help[0] = 5;
help[1] = 3;
// This line doesn't work
*this = move(help);
}
我收到以下错误:
no match for 'operator=' (operand types are 'A<int>' and 'std::remove_reference<std::vector<int, std::allocator<int> >&>::type {aka std::vector<int, std::allocator<int> >}')
我认为问题在于需要将帮助向量转换为 class A 对象,但我不知道该怎么做。谁能帮帮我?
如果你想那样使用它,你必须重载运算符赋值
A & operator=(const std::vector<T> & rhs)
{
for(auto it : help)
{
this->push_back(it);
}
return *this;
}
您想实现移动赋值运算符,这将在 O(1) 中完成。
template<class T>
class A :public vector<T> {
public:
void fillAndMove();
vector<T> help;
A & operator=(std::vector<T> && rhs)
{
static_cast<vector<T>&>(*this) = move(rhs);
return *this;
}
};
它也允许将法线向量分配给你的 A class,这将保持 help
向量不变,因此你可能想要使这个运算符 private
并实现移动 A class public.
的赋值运算符
test = std::vector<int>{ 5,6 }; // possible - should assigment operator be private?
此代码不可能:
template<class T>
class A :public vector<T> {
public:
void fillAndMove();
vector<T> help;
A & operator=(A && rhs)
{
// Move as you want it here, probably like this:
help = std::move(rhs.help);
static_cast<vector<T>&>(*this) = move(rhs);
return *this;
}
private:
A & operator=(std::vector<T> && rhs)
{
static_cast<vector<T>&>(*this) = move(rhs);
return *this;
}
};
此外,在执行此操作时,您还应该实现移动构造函数。
我想将向量的指针移动到我的 A 对象 (this) 的向量。我想这样做是因为我使用我的帮助向量(用于合并排序)并且我想要原始向量中的帮助向量的值。然而,我只想使用 1 个操作(因此应该通过移动来完成,而不是复制元素)。
这是我使用的代码:
template<class T>
class A:public vector<T> {
public:
void fillAndMove();
vector<T> help;
}
template<class T>
void A<T>:fillAndMove() {
// Fill a help array with random values
help.resize(2);
help[0] = 5;
help[1] = 3;
// This line doesn't work
*this = move(help);
}
我收到以下错误:
no match for 'operator=' (operand types are 'A<int>' and 'std::remove_reference<std::vector<int, std::allocator<int> >&>::type {aka std::vector<int, std::allocator<int> >}')
我认为问题在于需要将帮助向量转换为 class A 对象,但我不知道该怎么做。谁能帮帮我?
如果你想那样使用它,你必须重载运算符赋值
A & operator=(const std::vector<T> & rhs)
{
for(auto it : help)
{
this->push_back(it);
}
return *this;
}
您想实现移动赋值运算符,这将在 O(1) 中完成。
template<class T>
class A :public vector<T> {
public:
void fillAndMove();
vector<T> help;
A & operator=(std::vector<T> && rhs)
{
static_cast<vector<T>&>(*this) = move(rhs);
return *this;
}
};
它也允许将法线向量分配给你的 A class,这将保持 help
向量不变,因此你可能想要使这个运算符 private
并实现移动 A class public.
test = std::vector<int>{ 5,6 }; // possible - should assigment operator be private?
此代码不可能:
template<class T>
class A :public vector<T> {
public:
void fillAndMove();
vector<T> help;
A & operator=(A && rhs)
{
// Move as you want it here, probably like this:
help = std::move(rhs.help);
static_cast<vector<T>&>(*this) = move(rhs);
return *this;
}
private:
A & operator=(std::vector<T> && rhs)
{
static_cast<vector<T>&>(*this) = move(rhs);
return *this;
}
};
此外,在执行此操作时,您还应该实现移动构造函数。