Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Retriving Data from Map

Retriving Data from Map

Scheduled Pinned Locked Moved C / C++ / MFC
question
8 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    brucewayn
    wrote on last edited by
    #1

    I want to retrive the data from the end in a map. Generally for retriving we write below code for getting data from begining. so please suggest how can we get the data from the end in a map??? map<int,cstring>::iterator itera; for(itera = m_mapFinalSortVirusData.begin();itera != m_mapFinalSortVirusData.end();itera++) { int nCount= itera->;first; CString strvirname = itera->;second; CString str = ""; str.Format("%d %s",nCount,strvirname); AfxMessageBox(str); }

    C C D A J 5 Replies Last reply
    0
    • B brucewayn

      I want to retrive the data from the end in a map. Generally for retriving we write below code for getting data from begining. so please suggest how can we get the data from the end in a map??? map<int,cstring>::iterator itera; for(itera = m_mapFinalSortVirusData.begin();itera != m_mapFinalSortVirusData.end();itera++) { int nCount= itera->;first; CString strvirname = itera->;second; CString str = ""; str.Format("%d %s",nCount,strvirname); AfxMessageBox(str); }

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      see here [^]. :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      C 1 Reply Last reply
      0
      • B brucewayn

        I want to retrive the data from the end in a map. Generally for retriving we write below code for getting data from begining. so please suggest how can we get the data from the end in a map??? map<int,cstring>::iterator itera; for(itera = m_mapFinalSortVirusData.begin();itera != m_mapFinalSortVirusData.end();itera++) { int nCount= itera->;first; CString strvirname = itera->;second; CString str = ""; str.Format("%d %s",nCount,strvirname); AfxMessageBox(str); }

        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #3

        Use a reverse iterator: it is the same principle as an iterator, except that it starts from the end of the container. You have to use the rbegin function (which returns the begining of the reverted map, aka the end of your original map).

        Cédric Moonen Software developer
        Charting control [v1.5] OpenGL game tutorial in C++

        1 Reply Last reply
        0
        • C CPallini

          see here [^]. :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #4

          Not exactly correct: when iterating in reverse mode, you should replace the usual begin() by rbegin() and the usual end() by rend(). Thus rend doesn't return you the last element in your map but point before the first element.

          Cédric Moonen Software developer
          Charting control [v1.5] OpenGL game tutorial in C++

          C 1 Reply Last reply
          0
          • B brucewayn

            I want to retrive the data from the end in a map. Generally for retriving we write below code for getting data from begining. so please suggest how can we get the data from the end in a map??? map<int,cstring>::iterator itera; for(itera = m_mapFinalSortVirusData.begin();itera != m_mapFinalSortVirusData.end();itera++) { int nCount= itera->;first; CString strvirname = itera->;second; CString str = ""; str.Format("%d %s",nCount,strvirname); AfxMessageBox(str); }

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            brucewayn wrote:

            so please suggest how can we get the data from the end in a map???

            What about something like:

            itera = m_mapFinalSortVirusData.end();
            itera--;
            int nCount= itera->first;
            CString strvirname = itera->second;

            "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            1 Reply Last reply
            0
            • B brucewayn

              I want to retrive the data from the end in a map. Generally for retriving we write below code for getting data from begining. so please suggest how can we get the data from the end in a map??? map<int,cstring>::iterator itera; for(itera = m_mapFinalSortVirusData.begin();itera != m_mapFinalSortVirusData.end();itera++) { int nCount= itera->;first; CString strvirname = itera->;second; CString str = ""; str.Format("%d %s",nCount,strvirname); AfxMessageBox(str); }

              A Offline
              A Offline
              ahmad_ali
              wrote on last edited by
              #6

              According to this page http://www.cplusplus.com/reference/stl/map/[^] map supports bidirectional iterator. This means you could start from map.end() and then use the operator-- to obtain the last element.

              1 Reply Last reply
              0
              • C Cedric Moonen

                Not exactly correct: when iterating in reverse mode, you should replace the usual begin() by rbegin() and the usual end() by rend(). Thus rend doesn't return you the last element in your map but point before the first element.

                Cédric Moonen Software developer
                Charting control [v1.5] OpenGL game tutorial in C++

                C Offline
                C Offline
                CPallini
                wrote on last edited by
                #7

                Check out the example in the linked page. :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                1 Reply Last reply
                0
                • B brucewayn

                  I want to retrive the data from the end in a map. Generally for retriving we write below code for getting data from begining. so please suggest how can we get the data from the end in a map??? map<int,cstring>::iterator itera; for(itera = m_mapFinalSortVirusData.begin();itera != m_mapFinalSortVirusData.end();itera++) { int nCount= itera->;first; CString strvirname = itera->;second; CString str = ""; str.Format("%d %s",nCount,strvirname); AfxMessageBox(str); }

                  J Offline
                  J Offline
                  Jijo Raj
                  wrote on last edited by
                  #8

                  Hello brucewayn, Is your logic dependent on the order of inserted items in map? I just want to tell you that, unlink other containers such as vector, list etc, there is no order for the inserted item in map. So be careful. Regards, Jijo.

                  _____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups