C++ vector容器释放内存应注意的地方(三)

#include <iostream>
#include <vector>
#include <stdio.h>
#include <boost/date_time.hpp>
using namespace std;
using namespace boost::gregorian;
//返回dtLastDatetime相对于dtCurDatetime日期的第几周
int GetOrderWeek(const date &dtLastDatetime, const date &dtCurDatetime);
 
const int MAX_SIZE = 100*1024*1024;
 
struct Person
{
	string  strName;
	int		nAge;
	char    num[MAX_SIZE] ;
};
 
typedef struct tagRecord
{
	string     strNumber;
	vector<Person> vcPersonList;
}Record;
 
void main()
{
	Record record;
	record.strNumber = "100000285";
	vector<Person> *pPersonList = &record.vcPersonList;
 
	Person *person = new Person();
	person->nAge = 22;
	person->strName = "sunkaifang";
	pPersonList->push_back(*person); //注意这类,拷贝一份Person结构体
	//person = NULL;
 
 
	//person; /*= new Person();*/ 无需重新分配内存空间了,这样效率更高
         person->nAge = 23;
	person->strName = "ganquafnu";
 
	pPersonList->push_back(*person);   //注意这类,拷贝一份Person结构体
//	delete person;              //这才真正释放内存
	//person = NULL;
 
	vector<Person>::reverse_iterator tem;
	cout << record.strNumber << endl;
	for (tem = record.vcPersonList.rbegin(); tem != record.vcPersonList.rend(); ++tem )
	{
		cout << tem->strName << "  " << tem->nAge << endl;
	}
 
	delete person;              //这才真正释放内存
	vector<Person>().swap(record.vcPersonList);//这中方法是可以是否内存空间的
	//至此所有的new出来的内存空间全部释放了
	int wait;
	cin >> wait;
}
 
posted @ 2013-06-22 13:57  Predator  阅读(556)  评论(0编辑  收藏  举报