map C++

C++中的STL中map用法详解 (看到博客里面最全的)

MSDN 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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ma_Hong_Kai

微信 2936729162

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值