C++中的STL中map用法详解 (看到博客里面最全的)
好好看完以上差不多就会简单的应用了,至于具体的适合使用什么样的方式去给map这个容器“赋值”,看情况而定。
这两天遇到一个很有意思的东西,
一个东西是fortran写的,然后C++写接口,由于现实需求,Fortran里面的一些东西需要使用C++的接口。但是这些东西不是连续的,以前只是书本上的map,这次实践了,用map怎么写?
key为int,value是自己写的类。
自己写的demon。
记得stdafx.h 文件 #include "stdafx.h"上加一句话 #define _CRT_SECURE_NO_WARNINGS
// U.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "targetver.h"
#include <iostream>
#include <map>
#include <vector>
using namespace std;
class c1{
public:
void f(wchar_t * str)
{
cout << "f(wchar_t * str) ";
f_str = new wchar_t[wcslen(str) + 1];
wcscpy(f_str, str);
cout << endl;
}
void g(wchar_t * str)
{
g_str = new wchar_t[wcslen(str) + 1];
wcscpy(g_str, str);
cout << "g(wchar_t * str) " ;
wcout << str;
cout << endl;
}
void getf()
{
cout << "getf() ";
wcout << f_str;
cout << endl;
}
void getg()
{
cout << "getf() ";
wcout << g_str;
cout << endl;
}
void D()
{
delete f_str;
f_str = NULL;
delete g_str;
g_str = NULL;
}
private:
wchar_t * f_str;
wchar_t * g_str;
};
namespace nsmapClass{
// const int const nf = 1;
//static int nc = 0;
//typedef void(*cfp)(wchar_t *);
//map <int, cfp> mcfpa; // int numberFile
//typedef pair<int, cfp> m_pairf;
map<int, c1> mcc;
//typedef pair<int, cfp> m_pairc;
//c1 *cc = new c1;
//vector<int> vnF(nc, nf);
}
void interface(wchar_t * str, int &n)
{
nsmapClass::mcc[n].f(str);
}
void interface2(wchar_t * str, int &n)
{
nsmapClass::mcc[n].g(str);
}
int _tmain(int argc, _TCHAR* argv[])
{
using namespace nsmapClass;
int m = 2;
interface(_T("我是大洋洋"), m);
int n = 3;
interface2(_T("dasgjhda"), n);
interface2(_T("我爱吃饭"), m);
interface(_T("爱你,不爱你,是个问题吗"), n);
cout << "_________________________" << endl;;
map<int, c1>::iterator iter; // 如果将c1改为*c1 ,再改下面会崩掉,为什么呐?? 挺好玩的,自己猜吧
for (iter = mcc.begin(); iter != mcc.end(); iter++)
{
cout << iter->first << ' ';
iter->second.getf();
iter->second.getg();
iter->second.D();
}
system("pause");
return 0;
}