您好,
我下面的代码添加了一个向量类的实例,然后使用擦除()删除向量的第一个项目。真正擦除()删除的第一项。但它删除第五届
它试图在Visual Studio和Xcode /海湾合作委员会。我在做什么错?
谢谢,
托马斯
---代号:---
:2246366 |会员
#include "stdafx.h"
#include <vector>
bool bVectorErase = false;
class c
{
public:
c()
{
pStr = 0;
}
c()
{
if(pStr)
delete pStr;
}
//copy constructor:
c(const c & other)
{
pStr = 0;
SetI(other.i);
}
//in will be passed to the struct's int and string
void SetI(int in)
{
i = in;
if(pStr)
delete pStr;
pStr = new char [10];
sprintf(pStr, "%d", in);
};
int i;
char * pStr;
};
void TestVectorErase()
{
long ix;
std::vector<c> vctc;
for(ix = 0; ix < 5; ix ++)
{
c cinst;
cinst.SetI(ix);
vctc.push_back(cinst);
}
bVectorErase = true;
char * pShouldBeDeleted = vctc[0].pStr;
char * pShouldNotBeDeleted = vctc[4].pStr;
vctc.erase(vctc.begin());
}