Q: How do I evaluate the time difference between two events?
A: Accurate time difference between events cannot be evaluated by clocking the time using 'CTime::GetCurrentTime()' or by using a timer. First method is limited in its accuracy to around a second and second method has limitations of lower priority.
Here I present a function which returns the time elapsed from the last call. First call will always return a zero value. This uses the high-resolution performance counter. If it does not exist, each call will return zero.
Time difference is returned in seconds and the accuracy is determined by the high-resolution performance counter frequency.
Note: Most of the material in this article is taken from codeguru.com
Q: How to get the local IP address(es)?
A: The following example will get up to ten assigned IP addresses...
As with everything, there exists of course other ways and other information which can be retrieved...some examples can be found in the following knowledge base article.
Note: Most of the material in this article is taken from codeguru.com
Q: How to get the local hostname?
A:
Q: How to build a DLL to be used from another programming language?
A: When you create a DLL with Visual C++, and you want to use the DLL with other languages, there are a few things that you should do (creating a DLL isn't just compiling code).
The LIBRARY command must match the name of your DLL. So if your DLL is named YOURDLL.DLL, the LIBRARY command is followed by YOURDLL.
The DESCRIPTION is any description you feel is appropriate for your DLL. The actual description is preceeded by a single quote (').
The EXPORTS lists the functions that you exported, along with an ordinal number. It doesn't matter what ordinal numbers you use, so long as you maintain different numbers for each function, and you shouldn't change the ordinal numbers when you create an upgraded version of your DLL -- persons preferring to use ordinal numbers to call your functions will get angry.
Note: Most of the material in this article is taken from codeguru.com
Q: I have this simple function call:
The compiler raises the following error and I don't understand why.
A: Simply answered, that happens because the project is built for UNICODE.
Microsoft run-time library provides Microsoft-specific generic-text mappings for many data types, routines and other objects, mappings that are defined in TCHAR.h. There are three supported character sets:[/list][*]ASCII (single-byte character set
Q: What is the meaning of the 'Alloc' template parameter in 'vector'?
Q: How do I define my own allocator for use in STL containers?
A: The whole concept is very well explained in Gabriel and Andreas' article about Allocators.
The template parameter 'Alloc' specifies the type of allocator that the container should use to manage its memory. Every time the container needs a bit of memory, it will ask the allocator to retrieve it for him. The allocator usually just calls 'new' (and 'delete' when it has to free memory), but there can be more sophisticated strategies for allocators.
Usually, you are fine with the standard allocator provided by your STL. However, at times you may need to specify the allocation strategy explicitly. In theory, writing a custom allocator is not very difficult, but the task is complicated by the fact that the standard C++ design relies on template member functions which quite a few compilers don't support (e.g. Visual C++ 6).
A simple example for an allocator which never frees memory is this code (note that you need a recent compiler to get this to work):
Q: What is the best way to remove all elements of a particular value from a container?
A: Use the so-called 'remove/erase' idiom:
This is because of the simple fact that 'remove()' is a generic algorithm which takes iterators as its arguments. The iterators know nothing of their container; the STL was designed so as to grant the smallest possible amount of coupling. Thus, the iterators cannot call any container's 'erase()' or similar function.
'remove()' DOES return the container's new 'end()' (which just
happens to be the first element that was removed) and this value
can be used in the container's 'erase()' call.
Note: Most of the material in this article is taken from codeguru.com
Q: How to convert from std::string to CString?
A: This is simple... Use the CString constructor.
Let me add that CStringT can construct from both character or wide-character strings. i.e. It can convert from char* (i.e. LPSTR) or from wchar_t* (LPWSTR).
In other words, char-specialization (of CStringT) i.e. CStringA, wchar_t-specilization CStringW, and TCHAR-specialization CString can be constructed from either char or wide-character, null terminated (null-termination is very important here) string sources.
Q: How to convert from CString to std::string?
A: There are really many ways to do it.
But, the simplest one is just this -
CString strSomeCstring ("This is a CString Object");
// Use ANSI variant CStringA to convert to char*; construct from it -
std::string strStdString (CStringA (strSomeCstring));
CStringA is a template specialization of class CStringT for type char avaílable with Visual Studio 7.x and better.
Q: What are correlations between the CString Family and the Standard String Family?
A: If we were to draw a correlation between the CString Family and Standard String Classes, we would notice -
However, the standard library itself doesn't contain a "CString" equivalent - one that can at best be -
This makes CString (VS 7.x +) "cool".
Q: The history of CString...
A: CString really wasn't always as likeable as it is today. If one were to compare the CString of Visual Studio 7.x and better with the class supplied by MSVC 6.0, one might not be wrong in judging the erstwhile version as relatively inflexible, and restrictive.
What made CString of yesterday "inflexible"?
1.Only for MFC-folks!
Yes... CString with MSVC 6.0 was notoriously connected to MFC Projects as if other project types didn't have any need for a TCHAR String Wrapper Class! One reason for it would be the linker error one would get for using this class in a non-MFC Project.
2. Need to use a CString? Convert to TCHAR!
CString is a string wrapper for TCHAR strings. In doing so, it came with a good number of helpful member methods. However, the implementation of this class in MSVC 6.0 was such that if a programmer needed to make use of the utility member methods, he would need to convert his string data type to TCHAR - back and forth.
This "restriction" automatically excluded programmers using ANSI strings (char*) in UNICODE builds from using this class, and ditto for those using UNICODE strings (WCHAR*) in non-UNICODE builds from using this class - as in both these cases, the strings in question aren't TCHAR strings - the type that CString wraps.
What makes CString of today "flexible" and actually more "likeable"?
1. Use it in any project type - even in a console application!
Yes... All the programmer needs to do is to include the following header -
2. Don't change your String Type to suit CString! Choose the right CString instead!
With Visual Studio 7.x and better, CString is actually a specialization of template Class CStringT for type T as TCHAR.
Additionally, there exist two useful specializations of CStringT -
Note: Most of the material in this article is taken from codeguru.com
Q: What is the difference between '\n' and '\r\n'?
A:
Background
There are a few characters which can indicate a new line. The usual ones are these two:
This difference gives rise to a number of problems. For example, a file created under Unix (so with newlines as a single LF) will not open correctly under Window's Notepad. Any Windows program that expects newlines to be CRLF will not work correctly with these files.
To unify things a bit, so that writing portable C/C++ programs is possible, file streams have both a "translated" and an "untranslated" mode. If you open a file in translated mode, the runtime library will convert a '\n' to the appropriate newline character(s). If the following program is compiled under Unix, the file will contain a single LF to indicate the newline. If it's compiled under windows, it will contain a CRLF.
If you look at the generated file with a hex editor, you will see that the windows version has generated the following:
H e l l o W o r l d CR LF
0x48 0x65 0x6C 0x6C 0x6F 0x20 0x57 0x6F 0x72 0x6C 0x64 0x0D 0x0A
So file streams are handled in a transparent way, provided of course that you only handle files compatible with your operating system. But many times you have to pass multi-line strings directly to some system functions.
In practice
In Windows you have to pass multi-line strings with '\r\n', otherwise the system functions don't recognize them correctly as multi-line. This is true e.g. for setting the text of Edit controls, Labels, Windows etc. Also, when you read multi-line text from a file that initially contains '\r\n' in translated mode, the string in memory will contain only a single '\n'. See for example the documentation on MSDN about 'fread()':
Q: What types of strings are there?
A:
Note: Most of the material in this article is taken from codeguru.com