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;
//
//struct Person
//{
//	string  strName;
//	int		nAge;
//	char  ch[MAX_SIZE];
//};
//
//typedef struct tagRecord
//{
//	string     strNumber;
//	vector<Person> vcPersonList;
//}Record;
//
//void MyMethod() 
//{
//	Record record;
//	record.strNumber = "100000285";
//	vector<Person> *pPersonList = &record.vcPersonList;
//
//
//	Person person1;
//	person1.nAge = 22;
//	person1.strName = "sunkaifang";
//	pPersonList->push_back(person1); 
//
//
//	Person person2;
//	person2.nAge = 23;
//	person2.strName = "ganquafnu";
//
//	pPersonList->push_back(person2);   
//
//
//	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;
//	}
//	cout << record.vcPersonList.size() << record.vcPersonList.capacity() << endl;
//	record.vcPersonList.clear();
//	vector<Person>().swap(*pPersonList);//这中方法是可以内存空间的
//	cout << record.vcPersonList.size() << record.vcPersonList.capacity() << endl;
//}
//
//void main()
//{
//	MyMethod();
//	int wait;
//	cin >> wait;
//}
 
 
 
#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 MyMethod() 
{
	Record record;
	record.strNumber = "100000285";
	//vector<Person*> *pPersonList = &record.vcPersonList;
 
	Person *person = new Person();
	person->nAge = 22;
	person->strName = "sunkaifang";
	record.vcPersonList.push_back(person); //注意这类,拷贝一份Person结构体
//	delete person;              //这才真正释放内存
//	person = NULL;
 
 
	person = new Person();
	person->nAge = 23;
	person->strName = "ganquafnu";
 
	record.vcPersonList.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 )
	{
		if (*tem != NULL)
		{
			cout << (*tem)->strName << "  " << (*tem)->nAge << endl;
			delete *tem;
		}
		*tem = NULL;
	}
 
	//vector<Person>().swap(record.vcPersonList);//这中方法是可以是否内存空间的
	//至此所有的new出来的内存空间全部释放了
}
 
void main()
{
	MyMethod();
 
	int wait;
	cin >> wait;
}

原文链接: https://www.cnblogs.com/ganquanfu2008/p/3149690.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

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

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/93044

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月10日 上午1:58
下一篇 2023年2月10日 上午1:58

相关推荐