- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在操纵定义如下的对象 vector :
class Hyp{
public:
int x;
int y;
double wFactor;
double hFactor;
char shapeNum;
double* visibleShape;
int xmin, xmax, ymin, ymax;
Hyp(int xx, int yy, double ww, double hh, char s): x(xx), y(yy), wFactor(ww), hFactor(hh), shapeNum(s) {visibleShape=0;shapeNum=-1;};
//Copy constructor necessary for support of vector::push_back() with visibleShape
Hyp(const Hyp &other)
{
x = other.x;
y = other.y;
wFactor = other.wFactor;
hFactor = other.hFactor;
shapeNum = other.shapeNum;
xmin = other.xmin;
xmax = other.xmax;
ymin = other.ymin;
ymax = other.ymax;
int visShapeSize = (xmax-xmin+1)*(ymax-ymin+1);
visibleShape = new double[visShapeSize];
for (int ind=0; ind<visShapeSize; ind++)
{
visibleShape[ind] = other.visibleShape[ind];
}
};
~Hyp(){delete[] visibleShape;};
};
当我创建一个 Hyp 对象、分配/写入内存给 visibleShape 并使用 vector::push_back 将该对象添加到一个 vector 时,一切都按预期工作:visibleShape 指向的数据是使用复制构造函数复制的。
但是当我使用 vector::erase 从 vector 中删除一个 Hyp 时,除了现在指向错误地址的指针成员 visibleShape 之外,其他元素都被正确移动了!如何避免这个问题?我错过了什么吗?
最佳答案
我认为您缺少 Hyp
的重载赋值运算符。
关于c++ - vector::erase with pointer 成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2677770/
假设您要按值从 vector 中删除单个元素。 remove 之间有什么区别? -删除: vector v; // add some values vector::iterator it = remo
class CSensor { public: CSensor(int nVal1,char* pVal2,unsigned int nVal3); CSensor(cons
在回答过程中another question我偶然发现 std::vector::erase() 和 std::deque::erase() 的措辞略有不同。 这就是 C++14 关于 std::de
我想处理 vector 中的元素一段时间。为了优化这一点,我不想在处理项目时删除它,而是在最后删除所有已处理的项目。 vector::iterator it; for(it = items.begin
是否 std::set erase ( x ) 在 STL 中以相同的方式实现 erase ( collection.find( x ) ); (其中 x 是 const key_type&) 吗?
This question already has answers here: How to catch .erase exception in vectors? (2个答案) 8天前关闭。 当我尝试
我有以下代码: #include using namespace std; int main() { set S; S.insert("item1"); S.insert("i
在'helper'函数中输出集合元素时,预计元素'2'已经被移除。但实际结果仍然是'1 2 3 4 5'。 但是,集合的大小是 4。 我想知道潜在的问题。 #include #include us
有点难以解释,但我想制作一个以图片为背景的网站,并在图片上覆盖一层灰色。然后我想在覆盖层上方添加一些文本,在文本所在的位置,灰色覆盖层被移除,因此文本是在没有灰色覆盖层的情况下写入的。 我在一年前用图
这是导致错误的代码: 工厂.h: #include #include namespace BaseSubsystems { template class CFactory
我正在研究结构 vector 。 当我试图用迭代器调用这个函数时,像这样: vec2.erase (vec2.begin()+iter2); 它向我发送了这个错误: "no match for 'op
进程文件: eraser or eraser.exe 进程名称: Eraser 进程类别:存在安全风险的进程 英文描述: eraser.exe is the main executable
我正在编写一个简单的程序,它使用了 std::map::erase . 该程序很好,但有一些我不明白的地方。 如果我传给 erase function 第一个迭代器超出第二个迭代器的间隔,该函数不会删
如果我的变量是 0 到 6294 之间的随机数,我如何让我的输出仅打印出第一个变量? 我的随机数代码是 int random = (int)(Math.random() * 6294); 例如,如果它
我正在尝试通过一个测试程序,并且我通过了所有测试,除了涉及到我的删除功能时,程序崩溃了。 我最好的猜测可能是前面或后面的哨兵节点正在被删除。或内存泄漏。 最佳答案 我怀疑有人愿意阅读所有这些代码并为您
背景:这是一个程序,用于将存储在 vector 中的数字逐位加 1。 前面有 0 的数字可以作为输入,但不能作为输出。例如:0123 和 123 都是有效输入 但 0124 是无效输出 而 124 是
我正在尝试创建一个垂直滚动的射击游戏,当您按下空格键时,会创建一颗子弹,然后当子弹离开屏幕时,子弹就会被销毁。我通过声明为 vector bullets; 的 vector 跟踪子弹当我试图销毁屏幕外
我在下面写这段代码,发现了这个奇怪的行为: #include #include #include using namespace std; int main() { map map1;
我为 SFML 编写了一个线程渲染器,它接受指向可绘制对象的指针并将它们存储在一个 vector 中以在每一帧中绘制。开始向 vector 添加对象和从 vector 中删除对象会经常导致段错误 (S
如果我想从 map 中删除单个元素(除了可能的错误检查之外我不关心返回值),有两种方法可以实现:erase按值键或删除: http://ideone.com/YWocN7 #include #inc
我是一名优秀的程序员,十分优秀!