Click here to Skip to main content
16,011,754 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralOpening the CD ROM drive Pin
Ashman20-Jan-01 0:47
Ashman20-Jan-01 0:47 
GeneralRe: Opening the CD ROM drive Pin
24-Jan-01 2:56
suss24-Jan-01 2:56 
GeneralSmall question about MFC\DIALOG Pin
Ahmad19-Jan-01 21:01
Ahmad19-Jan-01 21:01 
GeneralRe: Small question about MFC\DIALOG Pin
19-Jan-01 21:37
suss19-Jan-01 21:37 
GeneralRe: Small question about MFC\DIALOG Pin
Christian Graus19-Jan-01 23:09
protectorChristian Graus19-Jan-01 23:09 
QuestionDuplicate entries inside my std::map. Why? Pin
Alvaro Mendez19-Jan-01 10:48
Alvaro Mendez19-Jan-01 10:48 
AnswerRe: Duplicate entries inside my std::map. Why? Pin
Chris Losinger19-Jan-01 11:35
professionalChris Losinger19-Jan-01 11:35 
GeneralRe: Duplicate entries inside my std::map. Why? Pin
Alvaro Mendez22-Jan-01 5:54
Alvaro Mendez22-Jan-01 5:54 
I really appreciate you looking into this problem for me Chris! Unfortunately I wasn't expecting to receive a "could not reproduce it" response otherwise I would have taken greater care to avoid it.

Here's how I was able to reproduce this problem. I took my sample code into a new MFC dialog based application I called "Doodle". I added a button to it and then a handler which I called OnButtonTryMap. Then inside I called the (global) accumulate function 100 times to insert/update the same MyInfo objects to the map each time. Here's how it looked:

#pragma warning (disable:4786)  // disables: identifier was truncated to '255' characters in the debug information
#include <map>


struct MyInfo
{  
	CString m_str1;  
	CString m_str2;  
	COleDateTime m_dtm1;  
	CString m_str3;   
	
	MyInfo(const CString& str1, const CString& str2, const COleDateTime& dtm1, const CString& str3) :     
		m_str1(str1), m_str2(str2), m_dtm1(dtm1), m_str3(str3)  
	{ }   
		
	bool operator<(const MyInfo& info) const  
	{    
		return (m_str1 < info.m_str1 ||            
				m_str2 < info.m_str2 ||            
				m_dtm1 < info.m_dtm1 ||            
				m_str3 < info.m_str3);  
	}
};
 
// Global objects (for testing purposes of course)
std::map< MyInfo, CString > g_mapInfo;
const COleDateTime g_dtmCurrent = COleDateTime::GetCurrentTime();
 
void accumulate(const CString& str1, const CString& str2, const COleDateTime& dtm1, const CString& str3, const CString& strValue)
{   
	g_mapInfo[MyInfo(str1, str2, dtm1, str3)] += strValue;
}
 
void CDoodleDlg::OnButtonTryMap() 
{
	CString str1, str2, str3;
	COleDateTime dtm = g_dtmCurrent;
 
	// Add the same 100 items each time this handler is called
	for (int iItem = 0; iItem < 100; iItem++)
	{
		str1.Format("str1 = %d", iItem);
		str2.Format("str2 = %d", iItem + 1);
		str3.Format("str3 = %d", iItem + 2);
		dtm += 1;

		accumulate(str1, str2, dtm, str3, str1);
	}
 
	// This should be 100 but...
	CString strMessage;
	strMessage.Format("There are %d items in the map.", g_mapInfo.size());
	AfxMessageBox(strMessage);
}


Try this and see if you get the same results. The first time I press the button I get "100 items" in the message. The second time, I get 118! The third time I get 142, then 154, and so on. If I've done this correctly, I should always see 100 items, shouldn't I? Confused | :confused:

Thanks for your help,
Alvaro
GeneralRe: Duplicate entries inside my std::map. Why? Pin
Alvaro Mendez22-Jan-01 8:16
Alvaro Mendez22-Jan-01 8:16 
GeneralRe: Duplicate entries inside my std::map. Why? Pin
Chris Losinger22-Jan-01 7:58
professionalChris Losinger22-Jan-01 7:58 
GeneralRe: Duplicate entries inside my std::map. Why? Pin
Alvaro Mendez22-Jan-01 7:10
Alvaro Mendez22-Jan-01 7:10 
GeneralOpen up a dialog resource from a DLL Pin
kk919-Jan-01 10:34
kk919-Jan-01 10:34 
GeneralRe: Open up a dialog resource from a DLL Pin
David Fedolfi19-Jan-01 10:48
David Fedolfi19-Jan-01 10:48 
GeneralActivate window in Win2000 Pin
Mike Melnikov19-Jan-01 3:36
Mike Melnikov19-Jan-01 3:36 
GeneralRe: Activate window in Win2000 Pin
AlexMarbus19-Jan-01 7:17
AlexMarbus19-Jan-01 7:17 
GeneralI've found another solution Pin
Mike Melnikov23-Jan-01 1:12
Mike Melnikov23-Jan-01 1:12 
GeneralRe: Activate window in Win2000 Pin
Michael Dunn19-Jan-01 14:34
sitebuilderMichael Dunn19-Jan-01 14:34 
GeneralI've found another solution Pin
Mike Melnikov23-Jan-01 1:15
Mike Melnikov23-Jan-01 1:15 
GeneralMember initialization Pin
Suha Aktan19-Jan-01 2:46
Suha Aktan19-Jan-01 2:46 
GeneralRe: Member initialization Pin
David Fedolfi19-Jan-01 3:32
David Fedolfi19-Jan-01 3:32 
GeneralRe: Member initialization Pin
David Fedolfi19-Jan-01 3:38
David Fedolfi19-Jan-01 3:38 
Generalshare folder Pin
derhackler19-Jan-01 1:51
derhackler19-Jan-01 1:51 
GeneralRe: share folder Pin
Ghazi H. Wadi19-Jan-01 4:27
Ghazi H. Wadi19-Jan-01 4:27 
GeneralRe: share folder Pin
derhackler19-Jan-01 5:15
derhackler19-Jan-01 5:15 
GeneralRe: share folder Pin
Ghazi H. Wadi19-Jan-01 7:18
Ghazi H. Wadi19-Jan-01 7:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.