This function is not the best solution for sorting STL types like 'vector'. You should avoid qsort for the following reasons:

qsort() is not guaranteed to work with non-POD types (structures and classes)

qsort() is not type-safe, casting is needed

qsort() is old C-Style

qsort() is slow in comparision to qsort(), because the C++ templates generates optimized code for a particular data-type

qsort() needs a function to compare the elements

The best solution to make a sort is to use the std::sort function; however, let's get back to Ayumi's problem of Ayumi. What do you think if Ayumi uses the std:sort function with the following code, will he get the desired result ?

But that is not the desired result! Do you know what went wrong? Does the sort not work like excepted?

Actually, the sort worked perfectly. Try to enter some other values in the array and you will see that the sort works, but the where is the problem?

The problem is that the underscore that comes after the '1' in the ASCII collating sequence. Thats why we got this output. But the problem is still not solved! The solution is to sort the characters between the underscore characters. In our case "11" and "1".

Aside from looping through the vector manually, what do I do to determine if the vector contains the value 20?

As always, Paul McKenzie is one of the people on CodeGuru who suggested the right solution for StevenS's problem. The std::find function provides the the possibility to find an entry in a vector without looping. Here is the code:

Postfix increment and decrement operators create a temporary object when used while prefix operators don't.

It is therefore more efficient to use the prefix version where possible, particularly in for loop constructs as whatever is used will be called multiple times in that case, i.e., we should use:

for (int nType = 0; nType < MM_NUM_TYPES; ++nType)
{
?
}

Is ++i really faster then i++ ? If yes, why it is faster?

If incrementing is all that you want to do (you are not assigning the variable to something else), then yes this is a good practice, especially if you are using the increment operator on a class.

// Prefix operators are preferred in cases in the
// following common cases:
for (;checkstate(x);++x) dosomething(x);
++x;

For standard data types, there is no performance difference, but for classes there are! The reason is that (for most common implementations of) postfix operators retain a temporary copy of original variable and because the return value is returned by value, not by reference. The following code exemplifies this:

Although this is again a very simple question, I think this can be very useful to many readers. In this thread, indiocolifa, we want to know whether the following definition:

enum { cg = 1 };

is better than this:

const int cg = 1;

What do you think? Well, it actually depends on whether you need a single value or multiple values. If you need a single value, you should go for a constant; whereas const can not be used in the class declaration. You can declare const either in a function or a structure. To use const in a class initialization, the constant must be a static const.

class X
{
static const int cg = 1;
}

An enum can be defined also in a class.

class X
{
enum {cg = 1};
}

Most coders tend to use enum because it is portable and does not use non-standard extensions of the in-class initialization.

In this thread post68 want to know which character should be set to display unicode characters in DC.

What character set should i use to display Unicode characters in
a DC? ie - Arial or courier?

Well, it actually depends on the type of unicode characters. According to Zhefu Zhang, you can use a font called "SimSun" that comes with Win2k, but should surely also be available to download on several Web sites. If you are going to use this font for your edit box or richedit box, you just need to call SetFont and you are ready to use it. If you are going to draw something, you can use the unicode function TextOutW or DrawTextW.

Dullboy is a bit confused. He wants to know why should he link a LIB file when a header file has been already inluded.

Why would we need to link a LIB file if a header file has been
included? When would we need to link a LIB file?

Actually, the header only contains the function/prototypes, wheras the real code resides in the DLL or LIB. If you just include the header without linking to the LIB or DLL, you get a fatal error. A normal LIB is always linked as static. If you specify a DLL, it gets linked dynamically.

Two main rules to remember:

Include header files to make the compiler happy.

Link the LIB files to make the linker happy.

Please enable Javascript in your browser, before you post the comment! Now Javascript is disabled.