Retriving Data from Map
-
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); }
-
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); }
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] -
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); }
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++ -
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]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++ -
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); }
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
-
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); }
-
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++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] -
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); }
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.