-1

I have 2 classes A and B. I create objects which are then put into a multimap. I want to print the all the keys with their corresponding values. However, my attempt to do this was not so successfull as I could create an iterator. I would like to ask how can I use the equal_range() method to accomplish this. Thanks in advance!

#include "pch.h"
#include <iostream>
#include <map>
using namespace std;
class A {
    int key;
public:
    A(int k) {
        key = k;
    }
    A(A ob) {
        key = ob.key;
    }
    int getKey() {
        return key;
    }
};
class B {
    string value;
public:
    B(string v) {
        value = v;
    }
};
multimap <A, B> mp;
int main() {
    mp = {
        make_pair(A(1),B("Crime and punishment")),
        make_pair(A(1),B("The Idiot")),
        make_pair(A(1),B("Brothers' Karamazov")),
        make_pair(A(2),B("Andreshko")),
        make_pair(A(2),B("The Gerak family")),
        make_pair(A(3),B("The name of the rose")),
        make_pair(A(3),B("Baudolino"))
    };
    for (auto ml = mp.begin(); ml != mp.end();ml++) {
        multimap<pair<int, string>, pair<int, string>>::iterator it;

    }



}

2 Answers2

0

You already have your loop, but let's use C++11 ranged-loop instead.

You are just missing an accessor for the value, so let's assume that you have it (getValue) and just access the iterator:

for (const auto& pair : mp) {
    std::cout << ml.first.getKey() << "\t" << ml.second.getValue() << std::endl;

}

Also change this:

A(A ob)

To

A(const A& ob)

This will give a real copy assignment. But the default copy constructor is also fine, so don't mention it at all, the default one is already good for you.

Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
0

Some things you need:

  • Getter methods for class B.
  • A less than operator for your key class A.
  • A copy constructor for class A.

In the code below if remove code not needed (but would be nice to keep in live code) to show what is really used.

#include <iostream>
#include <map>
#include <string>

using namespace std;

class A {
    int key;
public:
    A(int k) : key(k){}
    A(A const& ob) : key(ob.key) {}
    A& operator=(A const& ob) = delete;
    int getKey() const { return key; }
    friend bool operator<(A const&left, A const&right) { return  left.key < right.key; }
};

class B {
    string value;
public:
    B(string const& v) : value(v) {}
    B(B const&) = default;
    B& operator=(B const&) = delete;
    string const& getValue() const { return value; }
};

multimap<A, B> mp;

int main() {
    mp = {
            make_pair(A(3), B("Baudolino")),
            make_pair(A(1), B("Crime and punishment")),
            make_pair(A(1), B("Brothers' Karamazov")),
            make_pair(A(2), B("Andreshko")),
            make_pair(A(1), B("The Idiot")),
            make_pair(A(2), B("The Gerak family")),
            make_pair(A(3), B("The name of the rose"))
    };
    for (auto const & item : mp) {
        cout << "key:" << item.first.getKey() << " value:\"" << item.second.getValue() << "\"\n";
    }
}
Bo R
  • 2,334
  • 1
  • 9
  • 17