随手写的一些STL中vector,map等容器的测试代码,菜鸟请轻拍。。怕疼

#include "stdafx.h"

#include <vector>
#include <list>
using namespace std;
#include <iostream>
#include <algorithm>
#include <string>
#include <set>


typedef struct TestStruct
{
	long lAge;
	char szName[32];
	int nSex;

	/*friend bool operator<(const TestStruct&t1,const TestStruct&t2)
	{
		return t1.lAge<t2.lAge;
	}*/

	/*bool operator()(const TestStruct&t1,const TestStruct&t2) const
	{
		return t1.lAge<t2.lAge;
	}*/
}MyTestStruct,*pMyTestStruct;

class TestStruct_Rules
{
public:
	bool operator()(const TestStruct&t1,const TestStruct&t2) const
	{
		return t1.lAge<t2.lAge;
	}
};

Test::Test()
{

}

Test::~Test()
{

}

BOOL isSort(long const& l1,long const& l2)
{
	return l1>l2;
}

void Test::VectorTest1()
{
	CString csTemp;
	vector<long> vTest1;
	vector<long> vTest2(vTest1);
	vector<long> vTest3(5);
	vector<long> vTest4(5,6);
	vector<string> vTest5(4,"2");

	vTest5.push_back("2");

	long lData[5]={9,2,6,3,8};
	list<long> lstTest1(lData,lData+5);
	/*
	vector<long> vTest6{1,2,3,4};
	vector<string> vTest7{4,"null"};
	vector<string> vTest8{"null","null","null"};
	*/



	MyClass myclass1;
	vector<MyClass> vTest9(1,myclass1);

	MyClass* myclass2=new MyClass;
	vector<MyClass*> vTest10(1,myclass2);

	long lData2[6]={10,2,5,6,11,0};
	vector<long> vTest11(lData2,lData2+6);

	size_t nSize=vTest1.size();
	csTemp.Format(_T("vTest1 Size = %d\r\n"),nSize);
	OutputDebugString(csTemp);

	nSize = vTest3.size();
	csTemp.Format(_T("vTest3 Size = %d\r\n"),nSize);
	OutputDebugString(csTemp);

	nSize = vTest4.size();
	csTemp.Format(_T("vTest4 Size = %d\r\n"),nSize);
	OutputDebugString(csTemp);

	csTemp.Format(_T("vTest3-- value0=%d,value1=%d\r\n"),vTest3[0],vTest3[1]);
	OutputDebugString(csTemp);

	csTemp.Format(_T("vTest4-- value0=%d,value1=%d\r\n"),vTest4[0],vTest4[1]);
	OutputDebugString(csTemp);

	csTemp.Format(_T("vTest4-- value0Addr=[%x],value1Addr=[%x]\r\nsizeof(long)=%d\r\n")
		,&vTest4[0],&vTest4[1],sizeof(long));
	OutputDebugString(csTemp);

	csTemp.Format(_T("vTest5-- value0Addr=[%x]\tvalue0=[%s]\r\nvalue1Addr=[%x]\tvalue1=[%s]\r\nsizeof(string)=%d\r\n")
		,&vTest5[0],vTest5[0].c_str(),&vTest5[1],vTest5[1].c_str(),sizeof(string));
	OutputDebugString(csTemp);
	csTemp.Empty();

#ifdef _DEBUG

	list<long>::iterator lst_ite=lstTest1.begin();
	csTemp.Format(_T("lstTest1-- value0=[%x]\r\n"),lst_ite);
	OutputDebugString(csTemp);
	csTemp.Format(_T("lstTest1-- value0=[%x]\r\n"),&(*lst_ite));
	OutputDebugString(csTemp);
	csTemp.Format(_T("lstTest1-- value1=[%d]\r\n"),*(++lst_ite));
	OutputDebugString(csTemp);

	csTemp.Format(_T("myclass1 Addr = [%x]\r\nvTest9--value0Addr=[%x]\r\nsizeof(MyClass)=%d\r\n")
		,&myclass1,&vTest9[0],sizeof(myclass1));
	OutputDebugString(csTemp);

	csTemp.Format(_T("size = %d\tcapacity = %d\r\n"),vTest4.size(),vTest4.capacity());
	OutputDebugString(csTemp);
	csTemp.Format(_T("value0Addr=[%x]\tvalue1Addr=[%x]\r\n"),&vTest4[0],&vTest4[1]);
	OutputDebugString(csTemp);
	vTest4.push_back(0);
	csTemp.Format(_T("value0Addr=[%x]\tvalue1Addr=[%x]\r\n"),&vTest4[0],&vTest4[1]);
	OutputDebugString(csTemp);
	csTemp.Format(_T("size = %d\tcapacity = %d\r\n"),vTest4.size(),vTest4.capacity());
	OutputDebugString(csTemp);
	vTest4.push_back(0);
	vTest4.push_back(0);
	vTest4.push_back(0);
	csTemp.Format(_T("size = %d\tcapacity = %d\r\n"),vTest4.size(),vTest4.capacity());
	OutputDebugString(csTemp);

	vector<long>(vTest4).swap(vTest4);
	csTemp.Format(_T("size = %d\tcapacity = %d\r\n"),vTest4.size(),vTest4.capacity());
	OutputDebugString(csTemp);

	vTest1.reserve(1000);
	csTemp.Format(_T("size = %d\tcapacity = %d\r\n"),vTest1.size(),vTest1.capacity());
	OutputDebugString(csTemp);
	for (int i = 1; i <= 1000; ++i) 
		vTest1.push_back(i);

	vector<long>::iterator ite=vTest4.begin();
	csTemp.Format(_T("Addr=[%x]\tvalue=[%x]\r\n"),&(*ite),(*(ite._Ptr)));
	OutputDebugString(csTemp);

	csTemp.Empty();
	for(long i=0;ite!=vTest4.end();++i,++ite)
	{
		if(!csTemp.IsEmpty()) csTemp.Append(_T(","));
		csTemp.AppendFormat(_T("%d"),vTest4[i]);
	}
	OutputDebugString(csTemp);
	OutputDebugString(_T("\r\n"));

	csTemp.Empty();
	vector<long>::const_reverse_iterator ite1=vTest4.crbegin();
	while (ite1 != vTest4.crend())
	{
		if(!csTemp.IsEmpty()) csTemp.Append(_T(","));
		csTemp.AppendFormat(_T("%d"),*ite1);
		++ite1;
	}
	OutputDebugString(csTemp);
	OutputDebugString(_T("\r\n"));
	delete myclass2;
	

	vTest4.clear();
	csTemp.Format(_T("size=[%d]\tcapacity=[%d]\r\n"),vTest4.size(),vTest4.capacity());
	OutputDebugString(csTemp);
	vTest4.push_back(11);
	csTemp.Format(_T("size=[%d]\tcapacity=[%d]\r\n"),vTest4.size(),vTest4.capacity());
	OutputDebugString(csTemp);
	
	csTemp.Format(_T("size=[%d]\tcapacity=[%d]\r\n"),vTest11.size(),vTest11.capacity());
	OutputDebugString(csTemp);
	csTemp.Empty();
	ite=vTest11.begin();
	for(long i=0;ite!=vTest11.end();++i,++ite)
	{
		if(!csTemp.IsEmpty()) csTemp.Append(_T(","));
		csTemp.AppendFormat(_T("%d"),vTest11[i]);
	}
	OutputDebugString(csTemp);
	OutputDebugString(_T("\r\n"));

	sort(vTest11.begin(),vTest11.end());
	csTemp.Empty();
	ite=vTest11.begin();
	for(long i=0;ite!=vTest11.end();++i,++ite)
	{
		if(!csTemp.IsEmpty()) csTemp.Append(_T(","));
		csTemp.AppendFormat(_T("%d"),vTest11[i]);
	}
	OutputDebugString(csTemp);
	OutputDebugString(_T("\r\n"));

	sort(vTest11.begin(),vTest11.end(),isSort);
	csTemp.Empty();
	ite=vTest11.begin();
	for(long i=0;ite!=vTest11.end();++i,++ite)
	{
		if(!csTemp.IsEmpty()) csTemp.Append(_T(","));
		csTemp.AppendFormat(_T("%d"),vTest11[i]);
	}
	OutputDebugString(csTemp);
	OutputDebugString(_T("\r\n"));
	csTemp.Empty();
	csTemp.Format(_T("%x"),&vTest11[0]);

	vTest11.push_back(11);
	vTest11.push_back(10);
	vTest11.push_back(2);
	csTemp.Empty();
	csTemp.Format(_T("%x"),&vTest11[0]);
	vTest11.reserve(20);
	csTemp.Empty();
	csTemp.Format(_T("%x"),&vTest11[0]);
	ite=vTest11.begin();
	csTemp.Empty();
	for(long i=0;ite!=vTest11.end();++i,++ite)
	{
		if(!csTemp.IsEmpty()) csTemp.Append(_T(","));
		csTemp.AppendFormat(_T("%d"),vTest11[i]);
	}
	OutputDebugString(csTemp);
	OutputDebugString(_T("\r\n"));
	csTemp.Empty();
	csTemp.Format(_T("%x"),&vTest11[0]);

	csTemp.Empty();
	for (auto autotest : vTest11)
	{
		if(!csTemp.IsEmpty()) csTemp.Append(_T(","));
		csTemp.AppendFormat(_T("%d"),autotest);
	}
	OutputDebugString(csTemp);
	OutputDebugString(_T("\r\n"));

	csTemp.Empty();
	sort(vTest11.begin(),vTest11.end());
	auto end_unique=unique(vTest11.begin(),vTest11.end());
	//vTest11.erase(end_unique,vTest11.end());
	for (auto autotest : vTest11)
	{
		if(!csTemp.IsEmpty()) csTemp.Append(_T(","));
		csTemp.AppendFormat(_T("%d"),autotest);
	}
	OutputDebugString(csTemp);
	OutputDebugString(_T("\r\n"));
	vector<long>().swap(vTest11);
	//{ std::vector<int> tmp = v;   v.swap(tmp);   }; 
#endif

	//vector<string> v4{"t","e"};
	vector<long> vTest12;
	//vTest12.size(10000001);
	//vTest12.reserve(10000001);
	/*long lCurrTime = ::GetTickCount(),lTime(0);
	for(long i=0;i<10000000;++i)
	vTest12.push_back(i);
	lTime = ::GetTickCount()-lCurrTime;
	csTemp.Format(_T("Time = [%3f]s\r\n"),(double)lTime/1000);
	OutputDebugString(csTemp);*/
}


/
MyClass::MyClass():m_lStatus(0)
{
	memset(m_chName,0,sizeof(m_chName));
}

MyClass::~MyClass()
{
}

/

BOOL isMapSort(const pair<CString,long>& data1,const pair<CString,long>& data2)
{
	return data1.second<data2.second;
}

BOOL isMapSort2(const pair<long,long>&d1,const pair<long,long>&d2)
{
	return d1.second<d2.second;
}

MapTest::MapTest()
{

}

MapTest::~MapTest()
{

}

void MapTest::MapTest1()
{
	CString csTemp;
	map<CString,long> mapTest1;
	multimap<CString,long> mapTest2;
	set<string> setTest1;

	//mapTest1.push_back(_T("test"),1);

	mapTest1.insert(make_pair(_T("沙僧"),0));
	mapTest1.insert(make_pair(_T("唐僧"),1));
	mapTest1.insert(make_pair(_T("白龙马"),3));
	mapTest1.insert(make_pair(_T("沙僧"),4));
	mapTest1.insert(make_pair(_T("玉帝"),0));
	mapTest1.insert(make_pair(_T("唐僧"),5));
	mapTest1.insert(make_pair(_T("孙悟空"),6));

	//sort(mapTest1.begin(),mapTest1.end());

	//map<string,string> authors={{"test","test"},{"test","test"}};

	mapTest2.insert(make_pair(_T("沙僧"),0));
	mapTest2.insert(make_pair(_T("唐僧"),1));
	mapTest2.insert(make_pair(_T("白龙马"),3));
	mapTest2.insert(make_pair(_T("沙僧"),4));
	mapTest2.insert(make_pair(_T("玉帝"),0));
	mapTest2.insert(make_pair(_T("唐僧"),5));
	mapTest2.insert(make_pair(_T("孙悟空"),6));

	multimap<long,long> mapTest4;
	mapTest4.insert(make_pair(1,1));
	mapTest4.insert(make_pair(8,1));
	mapTest4.insert(make_pair(5,1));
	mapTest4.insert(make_pair(2,1));
	mapTest4.insert(make_pair(3,1));
	mapTest4.insert(make_pair(4,1));


	/*
	pair标准库类型,定义在utility头文件中
	一个pair保存两个数据成员,由特定的类型模板
	*/
	pair<CString,long> name;
	//pair<CString,long> name1{_T("test"),1};
	pair<CString,long> name2(_T("test"),1);
	//pair<CString,long> name3={_T("test"),1};


	typedef map<CString,long>::key_type v1;
	v1  csTest = _T("test");
	typedef map<CString,long>::value_type v2;

	//map<string,int>::mapped_type v3;

	map<CString,long>::iterator ite1=mapTest1.begin();

	while(ite1!=mapTest1.end())
	{
		ite1->first;
		++ite1;
	}


	mapTest1.erase(_T("沙僧"));

	set<int> iset;
	int iData[10]={7,8,9,0,1,2,3,4,5,6};
	iset.insert(iData,iData+10);
	set<int>::iterator ite =iset.find(1);
	long lPos = iset.count(8);

	//mapTest1.equal_range();
	//sort(mapTest4.begin(),mapTest4.end(),isMapSort2);
	pair<CString,long> mappair;
	map<CString,long>::iterator ite2,ite3,ite4;
	auto pos = mapTest1.equal_range(_T("猪八戒"));
	typedef map<CString,long>::iterator  pp1;
	pair<pp1,pp1> pos1 = mapTest1.equal_range(_T("猪八戒"));
	int i(0);
	if(pos.first == pos.second)
		i=1;
	else i=2;

typedef pair<CString,long> pp;

	

	//pair<pp,pp> pairTemp(mapTest1.equal_range(_T("猪八戒")));
	ite2 = mapTest1.lower_bound(_T("唐僧"));
	ite3 = mapTest1.upper_bound(_T("唐僧"));
	pos = mapTest1.equal_range(_T("唐僧"));

	vector<pair<CString,long>> vTemp;
	for (map<CString,long>::iterator curr = mapTest1.begin();curr!=mapTest1.end();curr++)
	{
		vTemp.push_back(make_pair(curr->first,curr->second));
	}
	sort(vTemp.begin(),vTemp.end(),isMapSort);
	vector<pair<CString,long>>::iterator ite6=vTemp.begin();
	//sort(mapTest1.begin(),mapTest1.end(),isMapSort);
	while (ite6!=vTemp.end())
	{
		csTemp.Format(_T("key=%s\t-->\tvalue=%d\r\n"),ite6->first,ite6->second);
		OutputDebugString(csTemp);
		ite6++;
	}
	OutputDebugString(_T("---------------------------------------\r\n"));
	
	
	map<CString,long> mapTest3;

	mapTest3[_T("tangseng")] = 1001;
	mapTest3[_T("sunwukong")] = 2002;
	mapTest3[_T("zhubajie")] = 3003;
	mapTest3[_T("bailongma")] = 4004;
	mapTest3[_T("guanyin")] = 5005;
	map<CString,long>::iterator ite5=mapTest3.begin();
	/*map<CString,long>::key_compare mycomp = mapTest3.key_comp();
	CString highest = mapTest3.rbegin()->first;
	do 
	{
		csTemp.Format(_T("key=%s\t-->\tvalue=%d\r\n"),ite5->first,ite5->second);
		OutputDebugString(csTemp);
	} while (mycomp((*ite5++).first, highest));

	OutputDebugString(_T("---------------------------------------\r\n"));*/

	pair<CString,long> highest1 = *mapTest3.rbegin();
	map<CString,long>::reverse_iterator ite7 = mapTest3.rbegin();
	ite5 = mapTest3.begin();
	do 
	{
		csTemp.Format(_T("key=%s\t-->\tvalue=%d\r\n"),ite5->first,ite5->second);
		OutputDebugString(csTemp);
	} while (mapTest3.value_comp()(*ite5++,highest1));
	OutputDebugString(_T("---------------------------------------\r\n"));

	ite5 = mapTest3.begin();
	while (ite7 != mapTest3.rend())
	{
		while(!(mapTest3.value_comp()(*ite5,*ite7)))
		{
			csTemp.Format(_T("key=%s\t-->\tvalue=%d\r\n"),ite5->first,ite5->second);
			OutputDebugString(csTemp);
			ite7++;
			if(ite7==mapTest3.rend()) break;
			ite5 = mapTest3.begin();
		}
		ite5++;
	}
	mapTest1.swap(mapTest3);

	mapTest1.insert(v2(_T("test"),1));

	map<MyTestStruct,long,TestStruct_Rules> mapTest5;
	mapTest5.clear();

	MyTestStruct mystruct;
	memset(&mystruct,0,sizeof(MyTestStruct));
	for (long i=0;i<5;i++)
	{
		mystruct.lAge=i*10;
		strncpy_s(mystruct.szName,"沙僧",31);
		mystruct.nSex = i/2;
		mapTest5.insert(make_pair(mystruct,i));
		memset(&mystruct,0,sizeof(MyTestStruct));
	}
	m_MyData.swap(mapTest1);
}

BOOL MapTest::MapTest2(map<CString,long>& maptest)
{
	maptest.insert(make_pair(_T("test"),1));
	return TRUE;
}

map<CString,long>& MapTest::MapTest3()
{
	return m_MyData;
}

map<CString,long>* MapTest::MapTest4()
{
	return &m_MyData;
}

/

StringTest::StringTest()
{

}

StringTest::~StringTest()
{

}



void StringTest::StringTest1()
{
#ifdef UNICODE
	std::wstring strTemp=_T("e:\\newGGDevelop\\Client\\release\\config\\system\\res\\FlexPaper\\DocumentView.htm?openType=DER&filetype=pdf&guid=E95EAAF3-00B0-40FA-AA84-2E54EACE2D0B&filepages=3&Download=1"),strTemp2;
	int nValue(-1);
	wstring::size_type pos,pos2,pos3;
	std::wstring strKey(_T("filetype=")),strWords(_T("doc|docx|txt|rar|zip|xls|xlsx|ppt|pptx"));
	pos = strTemp.find(strKey);
	if(pos != strTemp.npos)
	{
		pos2 = strTemp.find(L"&",pos);
		if(pos2 != strTemp.npos && pos2>pos)
		{
			strTemp2 = strTemp.substr(pos+strKey.length(),pos2-pos-strKey.length());
			pos3 = strWords.find(strTemp2);
			if(pos3!= strWords.npos)
				nValue =0;
			else
			{
				std::string strPath="C:\\1.pdf";
				char path[MAX_PATH] = {0};
				strncpy_s(path,strPath.c_str(),MAX_PATH);
				OpenFolderAndSelectFile(path);
			}
		}
	}
#else
#endif
}



BOOL OpenFolderAndSelectFile( LPSTR lpszFilePath)
{
	//
	// GetFolder
	//
	DWORD dw = lstrlenA(lpszFilePath) - 1;
	for (;dw != -1;dw--)
	{
		if ( lpszFilePath[dw] == '\\')
		{
			break;
		}
	}
	if ( dw == -1)
	{
		return FALSE;
	}
	//
	// Get a pointer to the Desktop's IShellFolder interface.
	// 
	LPSHELLFOLDER pDesktopFolder;
	if ( SUCCEEDED(SHGetDesktopFolder( &pDesktopFolder)))
	{
		// 
		// IShellFolder::ParseDisplayName requires the file name be in
		// Unicode.
		// 
		OLECHAR oleStr[MAX_PATH];

		lpszFilePath[dw] = '\0';
		MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED,
			lpszFilePath, -1, oleStr, _countof(oleStr));
		// 
		// Convert the path to an ITEMIDLIST.
		// 
		LPITEMIDLIST     pidl;
		ULONG             chEaten;
		ULONG             dwAttributes;
		HRESULT             hr;

		hr = pDesktopFolder->ParseDisplayName(
			NULL, NULL, oleStr, &chEaten, &pidl, &dwAttributes);
		if (FAILED(hr))
		{
			pDesktopFolder->Release();
			return FALSE;
		}
		LPCITEMIDLIST pidlFolder = pidl;

		lpszFilePath[dw] = '\\';
		MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED,
			lpszFilePath, -1, oleStr, _countof(oleStr));

		hr = pDesktopFolder->ParseDisplayName(
			NULL, NULL, oleStr, &chEaten, &pidl, &dwAttributes);
		if (FAILED(hr))
		{
			pDesktopFolder->Release();
			return FALSE;
		}
		LPCITEMIDLIST pidlFile = pidl;

		CoInitialize( NULL);
		hr = SHOpenFolderAndSelectItems( pidlFolder, 1, &pidlFile, 0);

		pDesktopFolder->Release();

		if ( hr == S_OK)
		{
			return TRUE;
		}
	}
	return FALSE;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值