C++ 调用vector.empty()时访问错误
在调用向量时,我得到了一个C++ 调用vector.empty()时访问错误,c++,C++,在调用向量时,我得到了一个EX\u BAD\u访问权限。空向量为空 bool empty = elements.empty(); 它在这里抛出了一个例外 /** * Returns a read-only (constant) iterator that points one past * the last element in the %vector. Iteration is done in * ordinary element
EX\u BAD\u访问权限
。空向量为空
bool empty = elements.empty();
它在这里抛出了一个例外
/**
* Returns a read-only (constant) iterator that points one past
* the last element in the %vector. Iteration is done in
* ordinary element order.
*/
const_iterator
end() const
{ return const_iterator(this->_M_impl._M_finish); } // EXCEPTION
呼叫时
/**
* Returns true if the %vector is empty. (Thus begin() would
* equal end().)
*/
bool
empty() const
{ return begin() == end(); } // EXCEPTION
最可能的原因是
元素
在该点上是无效对象。对于有效的向量对象,永远不应该出现此异常。最可能的原因是元素在该点上是无效的对象。对于有效的向量对象,您永远不应该出现此异常。正如@microtherion所述,元素很可能是无效的对象。实现这一点的简单方法是:
std::vector<MyType> *theElements = nullptr;
std::vector<MyType> &elements = *theElements;
bool empty = elements.empty();
std::vector*theElements=nullptr;
标准::向量和元素=*元素;
bool empty=elements.empty();
或者通过返回一个向量引用,该向量引用是一个临时对象,当它在函数结束时超出范围时消失:
std::vector<MyType>& GetElements() {
std::vector<MyType> temporaryElements;
//add to vector here.
return temporaryElements;
}
void foo() {
std::vector<MyType> &elements = GetElements();
bool empty = elements.empty();
}
std::vector&GetElements(){
向量时间元素;
//在这里添加到向量。
返回时间元素;
}
void foo(){
std::vector&elements=GetElements();
bool empty=elements.empty();
}
或者通过持有对另一个类的成员变量的引用,该变量在以下情况之前被清除:
class Foo {
private: std::vector<MyType> mElements;
public: std::vector<MyType>& GetElements();
};
class Bar {
private:
std::vector<MyType>& elements;
public:
Bar(Foo& foo) : elements(foo.GetElements()) { }
void Do(void) { bool empty = elements.empty(); }
};
//Probably much more hidden between multiple function calls, rather than this clear example.
void func(void) {
Bar* bar = nullptr;
{
Foo foo;
bar = new Bar(foo);
//foo dies, and so does the elements vector bar.elements refers to.
}
bar->Do();
delete bar;
}
class-Foo{
私人:std::矢量融合;
public:std::vector&GetElements();
};
分类栏{
私人:
std::向量和元素;
公众:
Bar(Foo&Foo):元素(Foo.GetElements()){
void Do(void){bool empty=elements.empty();}
};
//可能在多个函数调用之间隐藏得更多,而不是这个清晰的示例。
无效函数(无效){
Bar*Bar=nullptr;
{
富富,;
bar=新的bar(foo);
//foo dies,向量bar.elements引用的元素也是。
}
bar->Do();
删除条;
}
正如@microtherion所述,元素很可能是无效对象。实现这一点的简单方法是:
std::vector<MyType> *theElements = nullptr;
std::vector<MyType> &elements = *theElements;
bool empty = elements.empty();
std::vector*theElements=nullptr;
标准::向量和元素=*元素;
bool empty=elements.empty();
或者通过返回一个向量引用,该向量引用是一个临时对象,当它在函数结束时超出范围时消失:
std::vector<MyType>& GetElements() {
std::vector<MyType> temporaryElements;
//add to vector here.
return temporaryElements;
}
void foo() {
std::vector<MyType> &elements = GetElements();
bool empty = elements.empty();
}
std::vector&GetElements(){
向量时间元素;
//在这里添加到向量。
返回时间元素;
}
void foo(){
std::vector&elements=GetElements();
bool empty=elements.empty();
}
或者通过持有对另一个类的成员变量的引用,该变量在以下情况之前被清除:
class Foo {
private: std::vector<MyType> mElements;
public: std::vector<MyType>& GetElements();
};
class Bar {
private:
std::vector<MyType>& elements;
public:
Bar(Foo& foo) : elements(foo.GetElements()) { }
void Do(void) { bool empty = elements.empty(); }
};
//Probably much more hidden between multiple function calls, rather than this clear example.
void func(void) {
Bar* bar = nullptr;
{
Foo foo;
bar = new Bar(foo);
//foo dies, and so does the elements vector bar.elements refers to.
}
bar->Do();
delete bar;
}
class-Foo{
私人:std::矢量融合;
public:std::vector&GetElements();
};
分类栏{
私人:
std::向量和元素;
公众:
Bar(Foo&Foo):元素(Foo.GetElements()){
void Do(void){bool empty=elements.empty();}
};
//可能在多个函数调用之间隐藏得更多,而不是这个清晰的示例。
无效函数(无效){
Bar*Bar=nullptr;
{
富富,;
bar=新的bar(foo);
//foo dies,向量bar.elements引用的元素也是。
}
bar->Do();
删除条;
}