Printing all elements of multimap, which are pair of 2 different objects in C++?









up vote
-1
down vote

favorite












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;















share|improve this question

























    up vote
    -1
    down vote

    favorite












    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;















    share|improve this question























      up vote
      -1
      down vote

      favorite









      up vote
      -1
      down vote

      favorite











      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;















      share|improve this question













      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;












      c++ oop printing key multimap






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 22:26









      D.Dimitrov

      1




      1






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote













          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.






          share|improve this answer



























            up vote
            0
            down vote













            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";







            share|improve this answer




















              Your Answer






              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "1"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253865%2fprinting-all-elements-of-multimap-which-are-pair-of-2-different-objects-in-c%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              0
              down vote













              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.






              share|improve this answer
























                up vote
                0
                down vote













                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.






                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  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.






                  share|improve this answer












                  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.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 11 at 22:57









                  Matthieu Brucher

                  10.8k21935




                  10.8k21935






















                      up vote
                      0
                      down vote













                      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";







                      share|improve this answer
























                        up vote
                        0
                        down vote













                        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";







                        share|improve this answer






















                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          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";







                          share|improve this answer












                          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";








                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 11 at 23:13









                          Bo R

                          616110




                          616110



























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253865%2fprinting-all-elements-of-multimap-which-are-pair-of-2-different-objects-in-c%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              這個網誌中的熱門文章

                              How to read a connectionString WITH PROVIDER in .NET Core?

                              Node.js Script on GitHub Pages or Amazon S3

                              Museum of Modern and Contemporary Art of Trento and Rovereto