How To Sort An Array

September 18, 2008

The same sort function used to sort STL vector can be used for sorting arrays also. Surprised? Sometimes life is easier than we expect. 

#include <iostream>
#include <algorithm>
using namespace std;

int main()
{  
   int MyArray[] = {10, 9, 8, 7, 6};
   sort(MyArray, MyArray + 5);
   for (int i = 0; i < 5; ++i)
   {
      cout << MyArray[i] <<'\n';
   }
   return 0;
}

How To Convert CString To TCHAR*

September 18, 2008

See the following code segment.

CString myCString = _T("Hello"); 
const TCHAR* myCharString = LPCTSTR(myCString);

Some Background Info

  1. TCHAR will be evaluated to char if _UNICODE is not defined. Otherwise it will be evaluated to wchar.
  2. LPCTSTR is also a data type which is nothing but const char* or const wchar*, depending on whether _UNICODE defined or not.
  3. CString has a constructor which accepts TCHAR*.
  4. The macro _T will make the supplied string neutral. That is, if _UNICODE is defined, the string will be in 16 bit unicode format, otherwise in 8 bit ANSI format.