How to declare a map which value type is an object ?

I want to have a map which consists of a key of type string and a value of type CoefElt. The declaration of the CoefElt is as follows :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// class CoefElt
class CoefElt
{
public:
	// Data Members
	uint8_t m_coef;
	int m_index;
	uint64_t m_nodeId;
	uint8_t m_length;
	// Constructors and a Destructor
	CoefElt ();
	CoefElt (uint8_t coef, int index, uint64_t nodeId);
	~CoefElt ();
	// Methods
	uint8_t GetCoef () const;
	void SetCoef (uint8_t);
	int GetIndex() const;
	void SetIndex (int index);
	uint64_t GetNodeId ();
	void SetNodeId (uint64_t nodeId);
	std::string Key ();
	bool operator== (const CoefElt& coef) const;
	CoefElt& operator= (const CoefElt& coef);
};


To declare such a map, is it adequate to write map<std::string, CoefElt> myDesiredMap; or I need to write more additional lines ?

Any help would be appreciated,
Ali
Last edited on
Should be fine

Do you need the method

std::string Key(); ?

You don't need this to create the map

For the data member I think you can use any class with default contructor and assignment operator

The key class needs operator<


AFAIK it is not necessary to provide a default constructor, as long as you don't use operator[]
You can have that funcionallity with find and insert.
Thanks ne555, for your reply :-).
Do you mean default constructor of the CoefElt class ?. One more thing, Would you please tell me what I'm required to do if I want to use operator[] ?. I will be grateful if you explain it to me by some code and in simple words.

BR,
Ali
Last edited on
You already have default constructor

This is

CoefElt ();
(you have the code for this somewhere?)

just go ahead and create your map

map<string,CoefElt> mymap;
Topic archived. No new replies allowed.