C++ Code Snippets and Hints

How to Iterate Over an STL Collection with for each:

   #include <map>
   map<const char*, int> months;
   months["january"] = 31;
   ...
   
   map<const char*, int> months_30;
   
   for each( pair<const char*, int> c in months )
      if ( c.second == 30 )
         months_30[c.first] = c.second;

from: http://msdn.microsoft.com/en-us/library/ms177203(VS.80).aspx

 

Creating a Directory Chooser in MFC:

MFC does not provide access to the directory-picking dialog as it does the other common dialogs so the solution is to use use the ShBrowseForFolder function.

void CMyDlg::OnButton1() {
    char Dest[MAX_PATH];
    PickDir("Hello", Dest);
}
      
bool CMyDlg::PickDir(const char *prompt, char * dest) {
    // Dest is assumed to be _MAX_PATH characters in length
    BROWSEINFO bi;
    ITEMIDLIST* pItemIDList;
    char folder[_MAX_PATH];
    memset(&bi, 0, sizeof(bi));
    bi.hwndOwner = m_hWnd;
    bi.pszDisplayName = folder;
    bi.lpszTitle = prompt;
    if ((pItemIDList=SHBrowseForFolder(&bi)) != NULL) {
        SHGetPathFromIDList(pItemIDList, dest);
        return(true);
    } else return false;
}

From an Example by R.J.Menadue formerly at: http://rossm.net/Electronics/Computers/Software/C++/MFC.htm (2008)

Length of a LPTSTR

To get the length of a null-terminated LPTSTR in C or C++, use _tcslen, declared in TCHAR.H.

from: http://wwhyte.livejournal.com/105514.html