How to declare and use map in class with C++

1 Answer

0 votes
#include <iostream>
#include <map>

using std::cout;
using std::endl;
using std::map;

class Test {
private:
	const int key;
public:
	typedef map<int, char> TMap;
	static TMap mp;

	Test(const int k) : key(k) { };
	char get_char() { return mp[key]; };
};

Test::TMap Test::mp = { {1, 'a'}, {2, 'b'}, {3, 'c'} };

int main()
{
	Test o(2);

	cout << o.get_char() << endl;

	return 0;
}

/*
run:

b

*/

 



answered May 6, 2018 by avibootz

Related questions

1 answer 239 views
1 answer 198 views
2 answers 230 views
1 answer 130 views
1 answer 107 views
...