What will be printed out as a result of the follwing code execution?
#include <iostream>
#include <map>

struct A {
    int val;
    A() { };
    A(int val) : val(val) { }; 
    bool operator< (const A* tmp ) const {
        return val < tmp->val;        

int main() {
    std::map<A*,int> m;

    A *a1 = new A(2);
    A *a2 = new A(2);
    A *a3 = new A(3);
    A *a4 = new A(1);

    m[a1] = 0;
    m[a2] = 0;
    m[a3] = 0;
    m[a4] = 0;

    std::cout << m.size() << std::endl;

    return 0;
std::map, unlike std::multimap, cannot contain several elements with the same keys. However, in this case, pointers will be compared, not types. Therefore, the operator< method will not be called even once, and all pointers will have different addresses.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Sign Up Now
or Subscribe for future quizzes