?? maptest.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 + -