C++

来自CGTWiki
跳转至:导航搜索

C++程序的内存分配

C++常用代码汇总

C++部分介绍

C++11智能指针

在PPT中简要介绍了C++11智能指针

文件:C++11智能指针简介.pptx

rapidjson

实现快速序列化

#pragma once
#include "RapidjsonHelper.h"
using namespace PBLIB::RapidJsonHelper;
 
class MyClass3 :public JsonBase
{
public:
	int age;
	char name[100];
	std::string text;
	double money;
 
	MyClass3(){
		memset(name, 0, ARRAY_SIZE(name));
	}
	~MyClass3(){}
 
	void ToWrite(Writer<StringBuffer> &writer)
	{
		RapidjsonWriteBegin(writer);
		RapidjsonWriteString(text);
		RapidjsonWriteChar(name);
		RapidjsonWriteInt(age);
		RapidjsonWriteDouble(money);
		RapidjsonWriteEnd();
	}
 
	void ParseJson(const Value &val)
	{
		RapidjsonParseBegin(val);
		RapidjsonParseToString(text);
		RapidjsonParseToInt(age);
		RapidjsonParseToDouble(money);
		RapidjsonParseToChar(name);
		RapidjsonParseEnd();
	}
 
};
#include "RapidjsonHelper.h"
#include "MyClass3.h"
 
using namespace PBLIB::RapidJsonHelper;
 
class MyClass :public JsonBase
{
public:
	MyClass(){
		memset(name, 0, ARRAY_SIZE(name));
	}
	~MyClass(){}
 
	int age;
	char name[100];
	std::string text;
	double money;
	JsonArray<int> lst;
	JsonArray<MyClass3> lst2;
 
	void ToWrite(Writer<StringBuffer> &writer)
	{
		RapidjsonWriteBegin(writer);
		RapidjsonWriteString(text);
		RapidjsonWriteChar(name);
		RapidjsonWriteInt(age);
		RapidjsonWriteDouble(money);
		RapidjsonWriteClass(lst);
		RapidjsonWriteClass(lst2);
		RapidjsonWriteEnd();
	}
 
	void ParseJson(const Value& val)
	{
		RapidjsonParseBegin(val);
		RapidjsonParseToString(text);
		RapidjsonParseToInt(age);
		RapidjsonParseToDouble(money);
		RapidjsonParseToChar(name);
		RapidjsonParseToClass(lst);
		RapidjsonParseToClass(lst2);
		RapidjsonParseEnd();
	}
};
// PB_RapidJsonHelper.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "MyClass.h"

int main(int argc, _TCHAR* argv[])
{
	MyClass mylclass2;
	mylclass2.age = 10;
	strcpy(mylclass2.name, "pengbo");
	mylclass2.text = "123456";
	mylclass2.money = 1.123;
	for (unsigned i = 0; i < 4; i++)
	{
		MyClass3 tmp;
		tmp.age = 10;
		strcpy(tmp.name, "pengbo");
		tmp.text = "12345我6";
		tmp.money = 1.123;
		mylclass2.lst2.arr.push_back(tmp);
	}
	for (unsigned i = 0; i < 4; i++)
	{
		mylclass2.lst.arr.push_back(i);
	}
	//构造完成
 
	//std::string str2= mylclass2.lst2.ToJson();
 
	//Json序列化
	std::string str = mylclass2.ToJson();//序列化完成
 
	MyClass mylclassnew;
	MyClass::FromJson(&mylclassnew, str);//反序列化完成

	return 0;
}