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

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

class MyClass
{
public:
	typedef std::map<int, int> map_str_int;

	static map_str_int msi;

	static void Add_map_str_int(int& int_i, int& int_j);

private:

};

//类的静态成员变量初始化
//这里的成员变量是map类型,我们预先不知道map中将来会有多少键值对
//变量类型(MyClass::map_str_int)  +  变量名字(MyClass::msi)
MyClass::map_str_int MyClass::msi;


void MyClass::Add_map_str_int(int& int_i, int& int_j)
{
	msi[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);
	}
}

//打印MyClass::msi中的键值对
void my_print()
{
	for (int i = 0; i < 10; i++)
	{
		cout << MyClass::msi[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/81217235
今日推荐