[:tr]Haftalık C++ 13 – std::find_if ve auto code Örnek Kod Parçası[:en]Weekly C++ 13 – std::find_if and auto code snippet[:]

[:tr]Merhaba sevgili yazılımperver dostlarım. Bu yazımda, kendi kodlarınızda da sıklıkla kullanabileceğiniz std::find_if API’sine ilişkin (ki kendisini STL algorithm kütüphanesi ile sunulur) örnek bir kod parçasına göz atacağız.

İlgili API’nin tanımlamasına bakacak olursak:

API’nin resmi tanımı şöyle: tanımlanan [first, second] sınırları içerisinde, geçirilen pred metodu doğru dönen ilk elemanı gösteren iteratorü döner.

Şimdi de std::find_if API’si ile C++ 11/14 ile sunulan “uniform initialization, auto” ve C++ 17 ile sunulan if statement with initializer” kullanılan örnek kod parçasına bakalım. Bu kabiliyetler, genel olarak hep birlikte kullanılırlar, bu noktaya lütfen dikkat.

Peki yukarıda gerçekleştirdiğimiz işleri, C++ 11 öncesinde nasıl yapardık, farkı görmek adına isterseniz bir de o koda bakalım. Eminim, farkı fark edeceksiniz 🙂

Ekstra yapıcı, iterator ve “predicate” metot tanımlamaları ile kodun ne kadar büyüdüğünü görebilirsiniz. Modern C++ sayesinde, daha temiz ve okunabilir C++ kodu geliştirebilirsiniz (kendi fikrim 🙂

Ben yazılımperver, kendinize iyi bakın![:en]Hello my friends. In this post, we are going to have a brief look at std::find_if API (provided via algorithm library) snippet which you can use frequently in your codes.

You can find the prototype of API as follow:

Let us look at the official definition which says that it finds the first element in a range defined by [first, end] for which the predicate function pred returns true and return iterator to that element.

Now let us look at the snippet which make use of std::find_if API with uniform initialization, auto that are introduced with C++ 11/C++ 14 and if statement with initializer that introduced in C++ 17.

So the snippet is as follow:

So, how all this was done in pre-c++ 11. Let us also look at that code snippet to see the difference 🙂

You can see how code is get bloated with extra constructor, iterator and predicate definitions. Thanks to modern C++, now it is possible to develop more cleaner C++ code.

This is yazilimperver, take care of yourselves![:]