Three Ways to Find Files

Let’ say we have to implement a function that search a folder to recursively find files having an extension from a given list of extensions. This article shows three possible implementations: one using FindFirstFile and FindNextFile WinAPI functions, one using CFileFind MFC class and one using Filesystem Library. Find files using FindFirstFile and FindNextFile WinAPI … Read more

Using Lambdas in MFC Applications – Sorting Arrays

Beginning with Visual Studio 2010 which supports lambda expressions introduced by C++11 standard, you can handily sort an MFC array like in the following example: Sorting CStringArray by using a lambda expression // Example #1 // … CStringArray arr; arr.Add(_T(“John”)); arr.Add(_T(“Zorro”)); arr.Add(_T(“Sandy”)); arr.Add(_T(“Jimmy”)); // … bool bAscending = true; // … std::sort( arr.GetData(), // position … Read more

C++11: Let’s Write a “Hello Lambda!”

A lambda expression (aka lambda function), introduced in C++11 standard, is a simplified notation for defining an anonymous function object. However, its syntax and using may look weird for many people so let’s try to make a simple program to accommodate with it. // Example #1 #include <iostream> int main() { [] () mutable throw() … Read more

User defined literals

The C++ language defines various built-in literals (numerical, character, string, boolean and pointer) and a series of prefixes and suffixes to specify some of them. The suffix or prefix is part of the literal. auto b = true; // boolean auto s = “sample”; // const char[7] auto i = 128; // int auto d … Read more

Five new algorithms to C++11 that you should know about

C++11 added a bunch of new general purpose algorithms to the <algorithm> header. In this article we will look at five of them that C++ developers should know about. all_of, any_of and none_of These are actually three, not just a single algorithm, all_of, any_of, none_of, but they are very similar. They check if the supplied … Read more

The standard way of converting between numbers and strings in C++11

In C++03 there was no standard way of converting numbers to strings. The best way to do that was using a std::stringstream: int n = 42; std::stringstream ss; ss << n; std::string s = ss.str(); One could put that into a more generic function that looks like this: template <typename T> std::string to_string(T const & … Read more

Farewell to new and delete

C++11 introduced several smart pointers (residing in the <memory> header): std::shared_ptr: retains shared ownership of an object, that is destroyed when the last shared_ptr that holds a reference to it is destroyed or assigned another pointer. std::unique_ptr: retains sole ownership of an object, that is destroyed when the unique_ptr goes out of scope std::weak_ptr: retains … Read more

C++11: non-member begin() and end()

One of the addition to the C++ 2011 standard that is perhaps not so much popularized is the non-member begin() and end(). In STL all containers have a non-static member begin() and end() methods that return an iterator to the beginning and the end of the container. Therefor iterating over a container could look like … Read more