Skip to main content

Posts

Showing posts with the label STL

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...

STL - Map keys difference

Today, I would like to show how to quickly find difference between two maps of STL library basing on keys only. We need to compare two maps of STL library and find third map which will contain only those elements of first map which keys are not being placed within second map. Take a look on code: Output of this code is: In point I we are defining struct type having overloaded of operator() . This structure will be used as function object to compare two elements of maps ( std::pair ) basing on keys only. In point II we are defining two maps which will be compared. We are going to receive all std::pair s from firstMap which keys are not being placed within  secondMap . In point II we are declaring diffMap map which will contain result of our differentiation. Point IV is key point of our function. In that point we are using std::set_difference algorithm. This algorithm returns difference of two ranges of STL containers. First 4 arguments are begins and ends of two rang...

STL - sort() and is_sorted() algorithms

Today I would like to show first article about some example of usage STL ( Standard Template Library ) library and its algorithms. First thing which I would like to present is usage of std::sort() and std::is_sorted() algorithms. Function std::sort() sorts selected range of elements of container in ascending order. Function std::is_sorted() checks whether selected range of elements of container is sorted in ascending order. Take a look at following example of usage those functions: Output of below example is: At the beginning of that example we declare vector of int-typed elements using initializer_list  and prints it using range-based for loop (URL). In point (I) we are using std::is_sorted () function to check whether vector is sorted according to ascending order. Notice that we are iterating through whole vector (from testVector.begin() to testVector.end() ). Function returns false because our vector is not sorted yet. We can see results in first line of output. ...