我是新的cpp.I定义了一个向量与此代码,并在最后,我想完全擦除它。C++,删除向量
#include <string>
#include <stdio.h>
#include <vector>
#include <iostream>
using namespace std;
struct MCLASS
{
string *a = new string();
string *b = new string();
MCLASS(string ax,string bx)
{
a = new string(ax);
b = new string(bx);
}
~MCLASS()
{
delete a;
delete b;
}
};
vector<MCLASS*> *vc = new vector<MCLASS*>;
int main()
{
for (int i = 0 ; i <= 1000000 ; i++)
vc->push_back(new MCLASS("EHEM","UHUM"));
for (int i = 0 ; i <= 1000000 ; i++)
delete vc->at(i);
vc->clear();
delete vc;
cout<<"Deleted";
cin.get();
return 0;
}
但有一个疑难问题无法抹去它completely.I试图删除向量,向量明确项目的每个项目,并删除它。但是当我检查这个应用程序的内存使用,它使用约130MB.Please向我解释做什么。谢谢!
您需要阅读操作系统的内存模型。总之:大部分时间竞技场并没有减少到有更好的表现。 – VladimirS
您不应该使用所有这些指针...... – Jarod42
您可以通过不使用* raw指针*并声明不带'new'的变量来减轻这些指针问题。如果你*必须*使用指针使用*智能指针*。 –