{"id":1099,"date":"2019-04-01T19:35:05","date_gmt":"2019-04-01T19:35:05","guid":{"rendered":"https:\/\/yazilimperver.com\/?p=1099"},"modified":"2019-04-02T05:36:30","modified_gmt":"2019-04-02T05:36:30","slug":"haftalik-c-13-stdfind_if-ve-auto-code-ornek-kod-parcasi","status":"publish","type":"post","link":"https:\/\/yazilimperver.com\/index.php\/2019\/04\/01\/haftalik-c-13-stdfind_if-ve-auto-code-ornek-kod-parcasi\/","title":{"rendered":"[:tr]Haftal\u0131k C++ 13 &#8211; std::find_if ve auto code \u00d6rnek Kod Par\u00e7as\u0131[:en]Weekly C++ 13 &#8211; std::find_if and auto code snippet[:]"},"content":{"rendered":"<p>[:tr]Merhaba sevgili yaz\u0131l\u0131mperver dostlar\u0131m. Bu yaz\u0131mda, kendi kodlar\u0131n\u0131zda da s\u0131kl\u0131kla kullanabilece\u011finiz\u00a0<em>std::find_if\u00a0<\/em>API&#8217;sine ili\u015fkin (ki kendisini STL algorithm k\u00fct\u00fcphanesi ile sunulur) \u00f6rnek bir kod par\u00e7as\u0131na g\u00f6z ataca\u011f\u0131z.<\/p>\n<p>\u0130lgili API&#8217;nin tan\u0131mlamas\u0131na bakacak olursak:<\/p>\n<pre class=\"\">template &lt;class InputIterator, class UnaryPredicate&gt;\r\n   InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred);<\/pre>\n<p>API&#8217;nin resmi tan\u0131m\u0131 \u015f\u00f6yle: tan\u0131mlanan [first, second] s\u0131n\u0131rlar\u0131 i\u00e7erisinde, ge\u00e7irilen\u00a0<code>pred<\/code>\u00a0metodu do\u011fru d\u00f6nen ilk eleman\u0131 g\u00f6steren iterator\u00fc d\u00f6ner.<\/p>\n<p>\u015eimdi de\u00a0<strong><em>std::find_if<\/em> <\/strong>API&#8217;si ile C++ 11\/14 ile sunulan &#8220;<em><strong>uniform initialization,<\/strong><\/em>\u00a0<em><strong>auto&#8221;<\/strong> <\/em>ve C++ 17 ile sunulan <span style=\"text-decoration: underline;\"><em>&#8220;<strong>if statement with initializer&#8221;<\/strong><\/em><\/span>\u00a0kullan\u0131lan \u00f6rnek kod par\u00e7as\u0131na bakal\u0131m. Bu kabiliyetler, genel olarak hep birlikte kullan\u0131l\u0131rlar, bu noktaya l\u00fctfen dikkat.<\/p>\n<pre class=\"lang:c++ decode:true\">#include &lt;algorithm&gt;\r\n#include &lt;iostream&gt;\r\n#include &lt;string&gt;\r\n#include &lt;vector&gt;\r\n\r\nstruct Person\r\n{\r\n    int mId;\r\n    std::string mName;\r\n};\r\n\r\nint main()\r\n{\r\n    std::vector&lt;Person&gt; personList =\r\n    {{0, \"Ahmet\"}, {1, \"Mehmet\"}, {2, \"Begum\"}, {3, \"Fatma\"}};\r\n\r\n    \/\/ Burada kullanilan auto C++ 11 ile kullanima sunuldu\r\n    if (auto item = std::find_if (personList.begin(), personList.end(),\r\n    \t\t\t     \/\/ Burada kullanilan auto ise C++ 14 ile kullanima sunuldu\r\n                             [] (const auto&amp; arg)\r\n                             {\r\n                                 return arg.mName == \"Begum\";\r\n                             }) \r\n    \t; item != personList.end())\r\n    {\r\n        std::cout &lt;&lt; \"The Begum is found. Its id is: \" &lt;&lt; item-&gt;mId &lt;&lt; '\\n';\r\n    }\r\n    else\r\n    {\r\n        std::cout &lt;&lt; \"The Begum is not found!\" &lt;&lt; '\\n';        \r\n    }\r\n\r\n    return 0;\r\n}<\/pre>\n<p>Peki yukar\u0131da ger\u00e7ekle\u015ftirdi\u011fimiz i\u015fleri, C++ 11 \u00f6ncesinde nas\u0131l yapard\u0131k, fark\u0131 g\u00f6rmek ad\u0131na isterseniz bir de o koda bakal\u0131m. Eminim, fark\u0131 fark edeceksiniz \ud83d\ude42<\/p>\n<pre class=\"lang:c++ decode:true\">#include &lt;algorithm&gt;\r\n#include &lt;iostream&gt;\r\n#include &lt;string&gt;\r\n#include &lt;vector&gt;\r\n\r\nstruct Person\r\n{\r\n    int mId;\r\n    std::string mName;\r\n\r\n    \/\/ Ekstra yapicimiz\r\n    Person(int id, const std::string&amp; name)\r\n    {\r\n    \tmId = id;\r\n    \tmName = name;\r\n    }\r\n};\r\n\r\n\/\/ \"Predicate\" metodumuz i\u00e7in kullanacagimiz fonksiyon nesneleri (std::find_if icin)\r\nstruct isPersonPredicate\r\n{\r\n\tstd::string mPersonToFind;\r\n\tisPersonPredicate(const std::string&amp; personToLook)\r\n\t  : mPersonToFind(personToLook)\r\n\t{\r\n\r\n\t}\r\n\r\n    inline bool operator() (const Person&amp; arg)\r\n    {\r\n        return (arg.mName == mPersonToFind);\r\n    }\r\n};\r\n\r\n\r\nint main () \r\n{ \r\n\tstd::vector&lt;Person&gt; personList;\r\n\r\n\tpersonList.push_back(Person(0, std::string(\"Ahmet\")));\r\n\tpersonList.push_back(Person(1, std::string(\"Mehmet\")));\r\n\tpersonList.push_back(Person(2, std::string(\"Begum\")));\r\n\tpersonList.push_back(Person(3, std::string(\"Fatma\")));\r\n\r\n    std::vector&lt;Person&gt;::iterator item = std::find_if(personList.begin(), personList.end(),\r\n    \tisPersonPredicate(\"Begum\"));\r\n\r\n    if (item != personList.end())\r\n    {\r\n        std::cout &lt;&lt; \"The Begum is found. Its id is: \" &lt;&lt; item-&gt;mId &lt;&lt; '\\n';\r\n    }\r\n    else\r\n    {\r\n        std::cout &lt;&lt; \"The Begum is not found!\" &lt;&lt; '\\n';        \r\n    }\r\n\r\n     return 0;\r\n}<\/pre>\n<p>Ekstra yap\u0131c\u0131, iterator ve &#8220;predicate&#8221; metot tan\u0131mlamalar\u0131 ile kodun ne kadar b\u00fcy\u00fcd\u00fc\u011f\u00fcn\u00fc g\u00f6rebilirsiniz. Modern C++ sayesinde, daha temiz ve okunabilir C++ kodu geli\u015ftirebilirsiniz (kendi fikrim \ud83d\ude42<\/p>\n<p>Ben yaz\u0131l\u0131mperver, kendinize iyi bak\u0131n![:en]Hello my friends. In this post, we are going to have a brief look at <em>std::find_if<\/em> API (provided via algorithm library) snippet which you can use frequently in your codes.<\/p>\n<p>You can find the prototype of API as follow:<\/p>\n<pre class=\"\">template &lt;class InputIterator, class UnaryPredicate&gt;\r\n   InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred);<\/pre>\n<p>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\u00a0<code>pred<\/code>\u00a0returns true and return iterator to that element.<\/p>\n<p>Now let us look at the snippet which make use of <strong><em>std::find_if<\/em> <\/strong>API with <em><strong>uniform initialization,<\/strong><\/em>\u00a0<em><strong>auto<\/strong> <\/em>that are introduced with C++ 11\/C++ 14 and <em><strong>if statement with initializer<\/strong><\/em> that introduced in C++ 17.<\/p>\n<p>So the snippet is as follow:<\/p>\n<pre class=\"lang:c++ decode:true\">#include &lt;algorithm&gt;\r\n#include &lt;iostream&gt;\r\n#include &lt;string&gt;\r\n#include &lt;vector&gt;\r\n\r\nstruct Person\r\n{\r\n    int mId;\r\n    std::string mName;\r\n};\r\n\r\nint main()\r\n{\r\n    std::vector&lt;Person&gt; personList =\r\n    {{0, \"Ahmet\"}, {1, \"Mehmet\"}, {2, \"Begum\"}, {3, \"Fatma\"}};\r\n\r\n    \/\/ First auto is introduced in C++ 11\r\n    if (auto item = std::find_if (personList.begin(), personList.end(),\r\n    \t\t\t    \/\/ This auto is introduced in C++ 14\r\n                             [] (const auto&amp; arg)\r\n                             {\r\n                                 return arg.mName == \"Begum\";\r\n                             }) \r\n    \t; item != personList.end())\r\n    {\r\n        std::cout &lt;&lt; \"The Begum is found. Its id is: \" &lt;&lt; item-&gt;mId &lt;&lt; '\\n';\r\n    }\r\n    else\r\n    {\r\n        std::cout &lt;&lt; \"The Begum is not found!\" &lt;&lt; '\\n';        \r\n    }\r\n\r\n    return 0;\r\n}<\/pre>\n<p>So, how all this was done in pre-c++ 11. Let us also look at that code snippet to see the difference \ud83d\ude42<\/p>\n<pre class=\"lang:c++ decode:true\">#include &lt;algorithm&gt;\r\n#include &lt;iostream&gt;\r\n#include &lt;string&gt;\r\n#include &lt;vector&gt;\r\n\r\nstruct Person\r\n{\r\n    int mId;\r\n    std::string mName;\r\n\r\n    Person(int id, const std::string&amp; name)\r\n    {\r\n    \tmId = id;\r\n    \tmName = name;\r\n    }\r\n};\r\n\r\n\/\/ Predicate\r\nstruct isPersonPredicate\r\n{\r\n\tstd::string mPersonToFind;\r\n\tisPersonPredicate(const std::string&amp; personToLook)\r\n\t  : mPersonToFind(personToLook)\r\n\t{\r\n\r\n\t}\r\n\r\n    inline bool operator() (const Person&amp; arg)\r\n    {\r\n        return (arg.mName == mPersonToFind);\r\n    }\r\n};\r\n\r\n\r\nint main () \r\n{ \r\n\tstd::vector&lt;Person&gt; personList;\r\n\r\n\tpersonList.push_back(Person(0, std::string(\"Ahmet\")));\r\n\tpersonList.push_back(Person(1, std::string(\"Mehmet\")));\r\n\tpersonList.push_back(Person(2, std::string(\"Begum\")));\r\n\tpersonList.push_back(Person(3, std::string(\"Fatma\")));\r\n\r\n    std::vector&lt;Person&gt;::iterator item = std::find_if(personList.begin(), personList.end(),\r\n    \tisPersonPredicate(\"Begum\"));\r\n\r\n    if (item != personList.end())\r\n    {\r\n        std::cout &lt;&lt; \"The Begum is found. Its id is: \" &lt;&lt; item-&gt;mId &lt;&lt; '\\n';\r\n    }\r\n    else\r\n    {\r\n        std::cout &lt;&lt; \"The Begum is not found!\" &lt;&lt; '\\n';        \r\n    }\r\n\r\n     return 0;\r\n}<\/pre>\n<p>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.<\/p>\n<p>This is yazilimperver, take care of yourselves![:]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[:tr]Merhaba sevgili yaz\u0131l\u0131mperver dostlar\u0131m. Bu yaz\u0131mda, kendi kodlar\u0131n\u0131zda da s\u0131kl\u0131kla kullanabilece\u011finiz\u00a0std::find_if\u00a0API&#8217;sine ili\u015fkin (ki kendisini STL algorithm k\u00fct\u00fcphanesi ile sunulur) \u00f6rnek bir kod par\u00e7as\u0131na g\u00f6z ataca\u011f\u0131z. \u0130lgili API&#8217;nin tan\u0131mlamas\u0131na bakacak olursak: template &lt;class InputIterator, class UnaryPredicate&gt; InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred); API&#8217;nin resmi tan\u0131m\u0131 \u015f\u00f6yle: tan\u0131mlanan [first, second] s\u0131n\u0131rlar\u0131 i\u00e7erisinde, ge\u00e7irilen\u00a0pred\u00a0metodu do\u011fru d\u00f6nen&#8230; <a class=\"more-link\" href=\"https:\/\/yazilimperver.com\/index.php\/2019\/04\/01\/haftalik-c-13-stdfind_if-ve-auto-code-ornek-kod-parcasi\/\">Continue reading <span class=\"meta-nav\">&#8594;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":174,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_seopress_titles_title":"","_seopress_titles_desc":"","_seopress_robots_index":"","_seopress_robots_follow":"","_seopress_robots_imageindex":"","_seopress_robots_snippet":"","_seopress_robots_primary_cat":"0","_seopress_robots_breadcrumbs":"","_seopress_robots_freeze_modified_date":"","_seopress_robots_custom_modified_date":"","_seopress_robots_canonical":"","_seopress_social_fb_title":"","_seopress_social_fb_desc":"","_seopress_social_fb_img":"","_seopress_social_fb_img_attachment_id":0,"_seopress_social_fb_img_width":0,"_seopress_social_fb_img_height":0,"_seopress_social_twitter_title":"","_seopress_social_twitter_desc":"","_seopress_social_twitter_img":"","_seopress_social_twitter_img_attachment_id":0,"_seopress_social_twitter_img_width":0,"_seopress_social_twitter_img_height":0,"_seopress_redirections_value":"","_seopress_redirections_enabled":"","_seopress_redirections_enabled_regex":"","_seopress_redirections_logged_status":"","_seopress_redirections_param":"","_seopress_redirections_type":0,"_seopress_analysis_target_kw":"","dsgo_overlay_header":false,"dsgo_overlay_header_text_color":"","dsgo_overlay_skip_top_bar":false,"_designsetgo_exclude_llms":false,"footnotes":""},"categories":[10,41],"tags":[222,43,13,46,398,401,399,400,402,42,366,64],"class_list":["post-1099","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c","category-modern-c","tag-algorithm","tag-auto","tag-c","tag-c-11","tag-c-14","tag-code-snippet","tag-find_if","tag-if-init-expressions","tag-iterator","tag-modern-c","tag-snippet","tag-uniform-initialization"],"_links":{"self":[{"href":"https:\/\/yazilimperver.com\/index.php\/wp-json\/wp\/v2\/posts\/1099","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yazilimperver.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yazilimperver.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yazilimperver.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/yazilimperver.com\/index.php\/wp-json\/wp\/v2\/comments?post=1099"}],"version-history":[{"count":0,"href":"https:\/\/yazilimperver.com\/index.php\/wp-json\/wp\/v2\/posts\/1099\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/yazilimperver.com\/index.php\/wp-json\/wp\/v2\/media\/174"}],"wp:attachment":[{"href":"https:\/\/yazilimperver.com\/index.php\/wp-json\/wp\/v2\/media?parent=1099"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yazilimperver.com\/index.php\/wp-json\/wp\/v2\/categories?post=1099"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yazilimperver.com\/index.php\/wp-json\/wp\/v2\/tags?post=1099"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}