Skip to main content

Posts

Showing posts from December, 2013

Boost - shared pointer

Shared pointer is another kind of Boost library's pointer. It is similar to scoped pointer but shared pointer allows share ownership of object. We are able to assign two or more pointers pointing to the same object (it was impossible for scoped pointer). Take a look to the example below: Output of this example is: In point I we are initializing two shared pointers pointing to two separate instances of TestClass class, then we are printing its values in point II. Point III shows use difference between shared pointer and scoped pointer. We are assigning two pointers to the same object of class TestClass . In this point object having testValue==5 is being destroyed (because we remove the only one pointer to that object). Also, we have two pointers pointing to objects having testValue==6 now. Note that that object will be destroyed when last shared pointer pointing to them will be destroyed ( int our case it is destroyed when we are reaching end of main() function)

C++11 - Lambda functions

C++11 standard introduces new type of functions for C++ programming - lambda functions. Lambda function is a anonymous (unnamed) function which you can write directly inline in the code. Lambda function allows do some operations visible like a function without declaring it as a function (saving space and time). C++ lambda function has following syntax: [capture](arguments){body} capture - value which should be captured outside of body class arguments - list of arguments of function (the same syntax as for normal C++ functions) body - body of function - statements which should be exectued when function is invoked For better understanding take a look fo below example: Output of this example is: In point I we are initializing vector using C++11 initializer_list Point II is key point of our example. It demonstrate using lambda function (3rd argument of for_each function) together with with for_each algorithm of STL library in order to calculate sum of vector element

C++14 - Tuple addressing via type

Today I would like to introduce one of new features which will arrive to C++ with new language standard (C++14) which is going to be release in 2014. In order to compile example from this article you need to have compiler supporting C++14 standard. The newest version of GCC supports it. I would like to introduce you features called Tuple addressing via type which allows us to get tuple element value using type name instead of tuple parameter number. Of course it is possible only for type names which are not ambiguous. Let's take a look on below example for better understanding: In point I we are declaring our tuple type containng of 2 int elements and on string element. In point II we are using std::get function to get values of our tuple typed variable using tuple parameters numbers. This feature is well known from C++11 standard. Point III shows new (introduced in C++14 standard) way of getting values of tuple elements. We are getting string type element using

Interview with Bjarne Stroustrup - father of C++

Today I would like to recommend you very interesting interview with Bjarne Stroustrup - the inventor of C++ programming language. You can find out what Bjarne thing about increasing growing of impact of web technologies and languages with comparison to C++ applications. Bjarne also give an advices about technologies which young programmers should learn to be better developers. You will also be able to find out why Bjarne think that software development can be noble profession in the future. All those information and many more you can find here: Interview with Bjarne Stroustrup Enjoy!

STL - count and count_if algorithms

One of the basic and most useful STL algorithms is algorithm which can be used to count number of elements within selected container according to specified criteria. In order to do that we can use std::count or std::count_if algorithm. std::count (firstElementIterator, lastElementIterator, elementForSearch) - is function which will go through container using firstElementIterator and lastElementIterator and return number of container elements which value is equal elementForSearch std::count_if (firstElementIterator, lastElementIterator, UnaryPredicateFunction) - is function which examine range from firstElementIterator to lastElementIterator and return number of container elements which fulfill UnaryPredicateFunction criteria. UnaryPredicateFunction is function having following signature: bool functionName(const Type& a) . So, count_if returns number of elements where UnaryPredicateFunction returns true for. For better understanding let's take a

STL - iterators mechanism

Iterators are a generalization of pointers which allow a programmer to work with different data structures in the uniform manner .  In STL we are using iterators to go through STL container for getting and setting values of its elements. In order to better understand working with iterators, take a look on below example: Output of this example is: In point I we are defining two vector containers using C++11 initializer lists . Point II shows as definition of iterator which we will use to go through our vector structures to examine their elements. We are setting our iterator to first element of our myVector container. Because iterators are generalized pointers we can imagine this operation as setting up pointer to the first element of our structure. This operation can be illustrated like Step 1 on the picture below. In C++11 we can simplify declaration of STL iterators using auto type as shown in point IIa. In point III, IV and V we can see process of going through con