Skip to main content

Posts

Showing posts from August, 2013

C++11 - Initializer lists

In previous version of C++ standard (C++03) we was able to do following array elements assignment: But we have not been able to do similar assignments to vector : In order to assign above three elements to vector we had to invoke push_back () method three times: C++11 present feature which help with above inconvenience. This feature can be achieved thanks to initializer_list template. In C++11, vector implementation has assignment operator taking initializer_list template as parameter: Thanks to that we can use following assignments to vector : The same way we can overload assignement operator of any of own class, as well as constructor or any function. Such definition can can take initializer_list template as parameter. Then we will be able to pass initialization list having syntax: { element1, element2, element3,...} as parameter of such function. Of course STL vector container has implemented constructor having initializer_list as parameter, too. So you can also

C++11 - Range-based for loops

Another interesting new feature of C++11 standard is range-based for loop. This feature is simplification of usage of for loop going through each element of some container (ex. vector ). For other programming languages we can use following syntax when we would like to iterate through all container elements: Those constructions are very simple and rather self-understandable. However in C++ until now (C++03 standard) we had to use following syntax when we would like to go through each element of container: Complicated, right? Especially for beginner user who is just starting learning of C++ and STL library. So, how to simplity it? C++11 helps us here :) It defines new syntax for dealing with 'for each' iteration called "range-based for loop". Instead of above vector iteration we can do something like this: Definitely simpler, more understandable and easier to read, I think. What is more when we do not know exact type of data in our container we can use auto

C++11 - Auto type

At the beginning I would like to explain you one small new feature of C++11 standard. This feature is auto type. auto is new C++11 type keyword which automatically deducts type of variable. See example below: What is type of variable b ?  int of course. C++11 can automatically deduct type of variables using auto type. However remember - there is not desirable to use auto too often. It can make your code less readable. Try to use defined types as often as possible. Nevertheless, there is little more useful example of usage auto type. Let say that we have been declared following map containing two strings as key-value instances: Now if we would like to iterate through elements of above map in previous version of C++ (C++03) we had to declare iterator following way for example: Thanks to auto type C++11 allow us to declare it that way: Shorter and more comfortable. That's it for the beginning. I hope you understand usage auto type in C++11 right now. Write your pro

Welcome C++ lovers

Welcome all C++ lovers. I am starting this blog to share my C++ adventure with you. I will try to explain here usage of some C++ features as well as I will describe my experience with working as C++ developer. I hope you will find a lot of useful informations here. All comments are welcome. Let's start our common C++ adventure. Hello world :)