I want to create a map that will contain composite key. For example I have student’s roll no and semester in which he/she is studying. Now I want to create a map such that roll no and semester together act as the key for the map.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Rather than defining your own class for the key and having to define your own comparison operators, since you only care about roll number and semester, I’d use an std::pair.
If you’re using something other than an int for the roll number and semester, you can just as easily use those in the pair. Just keep in mind that if you’re using custom structures for those objects, they’ll need to implement equality and comparison operators, in which case you lose the benefit of using a pair instead of using some other structure directly.