C++ 标准:设置<;myType>;断层
我创建了一个类型C++ 标准:设置<;myType>;断层,c++,stl,set,C++,Stl,Set,我创建了一个类型myType,以及一组比较运算符 const bool operator< (const myType& a, const myType& b); const bool operator> (const myType& a, const myType& b); const bool operator==(const myType& a, const myType& b); const bool operator!=(co
myType
,以及一组比较运算符
const bool operator< (const myType& a, const myType& b);
const bool operator> (const myType& a, const myType& b);
const bool operator==(const myType& a, const myType& b);
const bool operator!=(const myType& a, const myType& b);
const bool operator<=(const myType& a, const myType& b);
const bool operator>=(const myType& a, const myType& b);
回复评论:析构函数和赋值运算符也存在,但如果我发布它们,可能会有所帮助:
建造商:
myType::myType {
this->init(); // assigns NULL to a pointer attribute
rep.push_back((unsigned short) 0); // rep ist the vector attribute
}
复制构造函数:
myType::myType(const myType::myType& u) : rep(u.rep) {
this->init(); // by intention
this->cleanLeadingZeroes(); // removes leading zero from rep, if any
}
赋值运算符:
myType& myType::operator=(const myType& rhs) {
if ( this == &rhs ) return *this;
this->rep.clear();
this->rep = rhs.rep;
return *this;
}
析构函数:
myType::~myType() {
rep.clear();
if ( this->pointerVar != NULL ) delete this->pointerVar;
}
运算符\u M\u impl.\u M\u end \u存储
为0x0,而此->\u M\u impl.\u start
为非0x0。有人知道为什么会这样吗
短暂性脑缺血发作
Thomas您的复制构造函数或您的比较(您的复制构造函数或您的比较(给我们看一下
myType
的定义如何?既然您提到了一个复制构造函数而不是析构函数或复制赋值运算符,我怀疑您违反了规则并观察到内存损坏。您好,感谢您指出这一点。我为问题添加了详细信息。响应中的myType::operator的定义是什么要编辑,请创建一个普通类型的向量副本(不是您自己制作的)几乎肯定不会失败,除非系统内存不足,除非这是一个巨大的数据集。如果你的向量是用户定义的类型,那么你可能也弄乱了该类型的复制构造函数,否则你可能是在转移视线,问题可能出在其他地方。我仍然认为你r cleanleadaingzeroos()函数将在第一次迭代时亲自隔离故障。不过我今晚不在-祝你好运!@w00te非常感谢你的努力。下一步我将尝试在linux机器上编译并运行此函数。向我们展示myType
的定义如何?因为你提到了复制构造函数,但没有提到析构函数或复制签名操作符,我怀疑您违反了并观察到内存损坏。您好,感谢您指出这一点。我在问题中添加了详细信息。myType::operator的定义是什么?在对您的编辑的响应中,一个普通类型的向量副本(您自己没有制作的)几乎肯定不会失败,除非系统内存不足,除非这是一个巨大的数据集。如果你的向量是用户定义的类型,那么你可能也弄乱了该类型的复制构造函数,否则你可能是在转移视线,问题可能出在其他地方。我仍然认为你r cleanleadaingzeroos()函数将在第一次迭代时亲自隔离故障。不过我今晚不在-祝你好运!@w00te非常感谢你的努力。下一步我将尝试在linux机器上编译并运行它。我提供了3个函数的定义。感谢你的回复。感谢你的建议。我更改了方法。我现在检查以太代表[rep.size()-1]!=0(然后我退出)否则我会删除向量的最后一个元素。它仍然是segfaults。调试器也不会在该方法中停止,并且该方法已经在没有segfaulting的情况下进行了数千次测试,因此我怀疑它是罪魁祸首。我提供了3个函数的定义。感谢您的回复。感谢您的建议。我更改了该方法。我现在检查rep[rep.size()-1]!=0(然后退出),否则我将删除向量的最后一个元素。它仍然存在分段错误。调试器也不会在该方法中停止,并且该方法已在没有分段错误的情况下测试了数千次,因此我怀疑它是罪魁祸首。
myType::myType(const myType::myType& u) : rep(u.rep) {
this->init(); // by intention
this->cleanLeadingZeroes(); // removes leading zero from rep, if any
}
myType& myType::operator=(const myType& rhs) {
if ( this == &rhs ) return *this;
this->rep.clear();
this->rep = rhs.rep;
return *this;
}
myType::~myType() {
rep.clear();
if ( this->pointerVar != NULL ) delete this->pointerVar;
}
const bool operator<(const myType& a, const myType& b) {
unsigned aNrBTs = a.size() - a.countLeadingZeroes(); // myType represents a big Int
unsigned bNrBTs = b.size() - b.countLeadingZeroes(); // here the representations
// are compared. size
// just returns rep.size()
if ( aNrBTs < bNrBTs ) return true;
if ( aNrBTs > bNrBTs ) return false;
for (int i = aNrBTs - 1; i >= 0; --i) {
if ( a.get( i ) < b.get( i ) ) return true; // get returns ith entry in
else if (a.get( i ) > b.get( i ) ) return false; // vector
else continue;
}
return false;
}
void myType::init() {
this->pointerVar = NULL; // pointerVar is a pointer attribute of type myType *
}
void myType::cleanLeadingZeroes() {
auto it = rep.rbegin();
while( it!= rep.rend()) {
if (*it != (unsigned short)0 ) break;
++it;
auto end = rep.end();
--end;
rep.erase(end);
}
if (this->rep.size() == 0) rep.push_back((unsigned short)0); // make sure vector
// contains one element
}
size_type
capacity() const
{ return size_type(this->_M_impl._M_end_of_storage
- this->_M_impl._M_start); }
myType::init();
myType::cleanLeadingZeroes();
const bool operator< (const myType& a, const myType& b);
void myType::cleanLeadingZeroes() {
auto it = rep.rbegin(); ****GET ITERATOR TO LAST ELEMENT****
while( it!= rep.rend()) {
if (*it != (unsigned short)0 ) break;
++it;
auto end = rep.end(); ****GET ITERATOR TO LAST ELEMENT****
--end; ****Moves from last+1 to last ****
rep.erase(end); ****ERASE LAST ELEMENT ****
} ****LOOP CYCLE, tries to progress
iterator that uses erased element****
if (this->rep.size() == 0) rep.push_back((unsigned short)0); // make sure vector
// contains one element
}