⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 maptest.cpp

📁 Vector和Map的一些用法.还有其他人的一些算法.
💻 CPP
字号:
//   map.cpp   :   Defines   the   entry   point   for   the   console   application.   
  //   
    
//  #include   "stdafx.h"   
#pragma   warning(disable:4786)   
#pragma   warning(disable:4251)   
#pragma   warning(disable:4273)   

#include <windows.h>
#include <math.h>
#include <map>
#include <vector>
#include <algorithm>
#include <iostream>
#include <string>
using   namespace   std; 

typedef struct tag_INFORMATION 
{
	int iIndex;
	char chInfo[MAX_PATH];

	tag_INFORMATION()
	{
		iIndex = 0;
		memset(chInfo,0,strlen(chInfo));
	}
}INFORMATION;

typedef std::vector<INFORMATION> INFORMATION_VEC;
typedef std::vector<INFORMATION_VEC> INFORMATION_VEC_VEC;

typedef std::vector<string> STRING_VEC;
typedef std::vector<STRING_VEC> STRING_VEC_VEC;

/*****************************************************************************************************
STL(Standard Template Library,标准模板库)是惠普实验室开发的一系列软件的统称。
它是由Alexander Stepanov、Meng Lee和David R Musser在惠普实验室工作时所开发出来的。
现在虽说它主要出现在C++中,但在被引入C++之前该技术就已经存在了很长的一段时间。

 STL的代码从广义上讲分为三类:algorithm(算法)、container(容器)和iterator(迭代器),
 几乎所有的代码都采用了模板类和模版函数的方式,这相比于传统的由函数和类组成的库来说提供了更好的代码重用机会。
 在C++标准中,STL被组织为下面的13个头文件:
 <algorithm>、<deque>、<functional>、<iterator>、<vector>、<list>、<map>、
 <memory>、<numeric>、<queue>、<set>、<stack>和<utility>。
 以下笔者就简单介绍一下STL各个部分的主要特点。
*****************************************************************************************************/
//#define _TEST_VEC_SORT	1														//打开测试vector的开关 
//#define  _TEXT_MAP 1																//测试映射关系
//#define  _TEST_CONVERT																//字符串转换函数
#define _TEST_VECEQUAL																	//用来测试两个VEC是否相等
struct   ltstr																	//用来排序的回调函数
{   
	bool   operator()(const   char*   s1,   const   char*   s2)   const   
	{   
		return   strcmp(s1,   s2)   <   0;   
	}   
};

bool rule( const int& p1, const int& p2 )
{
	return p1 > p2;
}
   

int   main(int   argc,   char*   argv[])   
{   
	
#ifdef _TEST_MAP																//测试MAP的用法的代码
	map<const   char*,   int,   ltstr>   months;   
	months["january"]   =   31;   
	months["february"]   =   28;   
	months["march"]   =   31;   
	months["april"]   =   30;   
	months["may"]   =   31;   
	months["june"]   =   30;   
	months["july"]   =   31;   
	months["august"]   =   31;   
	months["september"]   =   30;   
	months["october"]   =   31;   
	months["november"]   =   30;   
	months["december"]   =   31;   
	map<const   char*,   int,   ltstr>::size_type   st   =   months.size();   
	int   i=months["january"];   
	i=months["february"];
	months["machengqi"] = 41;
	months.clear();
	cout<<i<<endl;
#else ifdef _TEST_VEC_SORT														//测试VECTOR排序的算法
	
	int source[] = { 1, 4, 4, 6, 1, 2, 2, 3, 1, 6, 6, 6, 5, 7,5, 4, 4 };
	int sourceNum = sizeof(source)/sizeof(source[0]);
	vector<int> coll;
	
	
	cout<<"把源数据插入到vector中:"<<endl;
	copy (source, source+sourceNum,              //source
		back_inserter(coll)) ;					//由于是第一次所以一定要分配内存,不然后出拒绝访问内存溢出错误                 //destination                     //把source目标中的数据插入到vector当中
	
    cout<<"排序之前的顺序:"<<endl;
	copy (coll.begin(), coll.end(), ostream_iterator<int>(cout," "));
	cout << "\n\n";
	
	sort(coll.begin(),coll.end());
	
	cout<<"排序后的顺序"<<endl;
	copy(coll.begin(),coll.end(),ostream_iterator<int>(cout," "));
	cout<<endl;
	/*for (int i=0;i<coll.size();i++)
	{
		cout<<coll[i];
	}
	cout<<endl;*/

	vector<int> :: iterator pos;
	pos = unique (coll.begin(), coll.end());
	cout<<"取唯一值后的顺序:"<<endl;
	copy (coll.begin(), coll.end(), ostream_iterator<int>(cout," "));
	cout << "\n\n";
	
	cout<<"从开始位置输出唯一的序列:"<<endl;
	copy (coll.begin(), pos, ostream_iterator<int>(cout," "));      
	cout << "\n\n";
	
	
	cout<<"重新copy一遍覆盖排序后的顺序:"<<endl;
	copy (source, source+sourceNum,coll.begin());      
	
	
	cout<<"排序之前的顺序:"<<endl;
	copy (coll.begin(), coll.end(),ostream_iterator<int>(cout," "));
	cout << "\n\n";
	
	pos = unique(coll.begin(),coll.end(),greater<int>());
	cout<<"不经过排序直接取唯一值后的顺序:"<<endl;
	copy(coll.begin(),coll.end(),ostream_iterator<int>(cout," "));
	cout<<endl;
	//remove elements if there was a previous greater element
	//如果先前创建了多个元素就删除这个元素
	coll.erase (pos, coll.end()); 
	
	//把数组的内容复制到屏幕
	cout<<"擦除数组当中从唯一元素位置到结束位置的元素后的顺序:"<<endl;
	copy (coll.begin(), coll.end(), ostream_iterator<int>(cout," "));
	cout << "\n\n";	

	//使用自定义规则输出
	OutputDebugString("清除所有内容\n");
	//afxDump<<"清除所有数据!\n";
	coll.clear();
	OutputDebugString("重新插入数据!\n");
	copy(source,source+sourceNum,back_inserter(coll));
	cout<<"新插入的数据的顺序"<<endl;
	copy(coll.begin(),coll.end(),ostream_iterator<int>(cout," "));
	cout<<endl;

	OutputDebugString("用自己的方式得到唯的元素!\n");
	pos = unique(coll.begin(),coll.end(),rule);

	//得到唯一序列后的元素
	cout<<"用自己顺序排序后的顺序"<<endl;
	copy(coll.begin(),coll.end(),ostream_iterator<int>(cout," "));
	cout<<endl;

#endif

#ifdef _TEST_CONVERT
  char   *string, *stopstring;
   double x;
   long   l;
   int    base;
   unsigned long ul;
   string = "3.1415926This stopped it";
   x = strtod( string, &stopstring );
   printf( "string = %s\n", string );
   printf("   strtod = %f\n", x );
   printf("   Stopped scan at: %s\n\n", stopstring );
   string = "-10110134932This stopped it";
   l = strtol( string, &stopstring, 10 );
   printf( "string = %s", string );
   printf("   strtol = %ld", l );
   printf("   Stopped scan at: %s", stopstring );
   string = "10110134932";
   printf( "string = %s\n", string );
   /* Convert string using base 2, 4, and 8: */
   for( base = 2; base <= 8; base *= 2 )
   {
      /* Convert the string: */
      ul = strtoul( string, &stopstring, base );
      printf( "   strtol = %ld (base %d)\n", ul, base );
      printf( "   Stopped scan at: %s\n", stopstring );
   }

   string = "4D";
   stopstring = "\\0";
   long lResult = strtol(string, 0, 16);
   cout<<lResult<<endl;
#endif

#ifdef _TEST_VECEQUAL

   string strArray[] = {"00001","00002","00003","00004","00005","00006","00007","00008","00009",""};

   STRING_VEC_VEC strVecVec1;
   STRING_VEC_VEC strVecVec2;

   INFORMATION_VEC_VEC strVecVec3;
   INFORMATION_VEC_VEC strVecVec4;

   int iIndex = 0;
   while (true)
   {
		if (strArray[iIndex] == "")
		{
			break;
		} 
		STRING_VEC strVec;
		strVec.push_back(strArray[iIndex]);
		strVecVec1.push_back(strVec);
		strVecVec2.push_back(strVec);
	
		iIndex++;
   }

   strVecVec1.erase(&strVecVec1[0]);
   STRING_VEC strVec;
   STRING_VEC_VEC strVecVec;
   strVec.push_back(strArray[0]);
   strVec.push_back("machengqi");
   strVecVec.push_back(strVec);
   //strVecVec1.push_back(strVec);
   //strVecVec1[0].clear();
   strVecVec1.insert(strVecVec1.begin(),strVecVec.begin(),strVecVec.end());

   strVecVec1[0][0].swap(strVecVec1[0][1]);

   cout<<"数组一的内容:"<<endl;
   for (int i=0;i<strVecVec1.size();i++)
   {
	   for (int k=0;k<strVecVec1[i].size();k++)
	   {
		   cout<<strVecVec1[i][k]<<endl;
	   }
	   //cout<<strVecVec1[i][0]<<endl;
   }

   cout<<"数组二的内容"<<endl;
   for (int j=0;j<strVecVec2.size();j++)
   {
	   for (int k=0;k<strVecVec2[j].size();k++)
	   {
		   cout<<strVecVec2[j][k]<<endl;
	   }
   }

   if (strVecVec1 == strVecVec2)
   {
	   cout<<"数组相等!"<<endl;
   }
   else
   {
	   cout<<"数组不相等!"<<endl;
   }

 /*  if (strVecVec3 == strVecVec4)
   {
	   cout<<"结构体数组相等!"<<endl;
   }
   else
   {
	   cout<<"结构体数组不相等"<<endl;
   }*/
#endif
   
	getchar();
	return   0;   
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -