类的静态成员变量是map类型(二)

#include <map>
#include <iostream>
using namespace std;

class MyClass
{
public:

	typedef std::map<int, int> map_str_int;

	//引用
	static map_str_int& Registry();
	static void Add_map_str_int(int& int_i, int& int_j);

private:

};


MyClass::map_str_int&  MyClass::Registry()
{
	static map_str_int* msi = new map_str_int;

	return *msi;
}

void MyClass::Add_map_str_int(int& int_i, int& int_j)
{
	map_str_int& registry = Registry();
	registry[int_i] = int_j;
}

void my_cycle()
{
	for (int i = 0; i < 10; i++)
	{
		int j = i + 10;
		MyClass::Add_map_str_int(i, j);
	}
}

void my_print()
{
	MyClass::map_str_int& registry = MyClass::Registry();
	for (int i = 0; i < 10; i++)
	{
		cout << registry[i] << endl;
	}
}


//=============================//
int main()
{
	my_cycle();
	my_print();
	return 0;
}

输出结果:

10
11
12
13
14
15
16
17
18
19

猜你喜欢

转载自blog.csdn.net/zlf19910726/article/details/81217368
今日推荐