{"id":6774,"date":"2020-05-17T14:34:20","date_gmt":"2020-05-17T21:34:20","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6774"},"modified":"2020-05-17T14:51:31","modified_gmt":"2020-05-17T21:51:31","slug":"python-like-enumerate-in-c17","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/c\/python-like-enumerate-in-c17\/","title":{"rendered":"Python-Like enumerate() In C++17 &#8211; C++ Weekly EP1"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Python-Like enumerate() In C++17 - C++ Weekly EP1\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/ENpgbTrrebo?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\n#include <iostream>\n#include <string>\n#include <vector>\n#include <list>\n#include <tuple>\nusing namespace std;\n\n\/\/ Python-Like enumerate() In C++17\n\/\/ http:\/\/reedbeta.com\/blog\/python-like-enumerate-in-cpp17\/\ntemplate <typename T,\n          typename TIter = decltype(std::begin(std::declval<T>())),\n          typename = decltype(std::end(std::declval<T>()))>\nconstexpr auto enumerate(T && iterable) {\n  struct iterator {\n    int i;\n    TIter iter;\n    bool operator != (const iterator & other) const { return iter != other.iter; }\n    void operator ++ () { ++i; ++iter; }\n    auto operator * () const { return std::tie(i, *iter); }\n  };\n  struct iterable_wrapper {\n    T iterable;\n    auto begin() { return iterator{ 0, std::begin(iterable) }; }\n    auto end() { return iterator{ 0, std::end(iterable) }; }\n  };\n  \/\/ return iterable_wrapper{ iterable }; \/\/ this makes a copy if iterable is a rvalue.\n  return iterable_wrapper{ std::forward<T>(iterable) };  \n}\n\nstruct A {\n  int val;\n  A(int val): val(val) { cout << \"A(int)\" << endl; }\n  A(const A&#038; a): val(a.val) { cout << \"A(A&#038;)\" << endl; }\n  A(A&#038;&#038; a): val(a.val) { cout << \"A(A&#038;&#038;)\" << endl; }    \n};\n\nint main(int argc, char** argv) {\n  vector<int> arr{1, 2, 4};  \n  \n  for (size_t i = 0; i < arr.size(); ++i)\n    cout << i << \" \" << arr[i] << endl;\n\n  size_t idx = 0;\n  for (int v : arr)\n    cout << idx++ << \" \" << v << endl;\n\n  for (auto it = begin(arr); it != end(arr); ++it)\n    cout << distance(begin(arr), it) << \" \" << *it << endl;\n  \n\n  for (const auto&#038; [i, v] : enumerate(arr))\n    cout << i << \" \" << v << endl;\n\n  list<string> lst{\"hello\", \"world!\"};  \n\n  for (const auto& [i, v] : enumerate(lst))\n    cout << i << \" \" << v << endl;\n\n  for (const auto&#038; [i, v] : enumerate(string(\"abcde\")))\n    cout << i << \" \" << v << endl;\n\n  vector<A> vec;\n  vec.reserve(3);\n  vec.emplace_back(1);\n  vec.emplace_back(2);\n  vec.emplace_back(4);\n\n  for (const auto& [i, v] : enumerate(vec))\n    cout << i << \" \" << v.val << endl;\n\n  for (const auto&#038; [i, v] : enumerate(vector<A>{A(1), A(2), A(4)}))\n    cout << i << \" \" << v.val << endl;\n\n  return 0;\n}\n\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\/\/ Author: Huahua #include #include #include #include #include using namespace std; \/\/ Python-Like enumerate() In C++17 \/\/ http:\/\/reedbeta.com\/blog\/python-like-enumerate-in-cpp17\/ template constexpr auto enumerate(T &#038;&#038; iterable) {&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[600],"tags":[603,38,602,604,601],"class_list":["post-6774","post","type-post","status-publish","format-standard","hentry","category-c","tag-auto","tag-c","tag-c17","tag-constexpr","tag-enumerate","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6774","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=6774"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6774\/revisions"}],"predecessor-version":[{"id":6778,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6774\/revisions\/6778"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6774"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6774"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6774"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}