{"id":345139,"date":"2023-02-08T15:02:39","date_gmt":"2023-02-08T15:02:39","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=345139"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=345139","title":{"rendered":"<span>C++17 \u2014 std::string_view \u0438 \u043d\u0438\u043a\u0430\u043a\u043e\u0433\u043e \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 <code>std::string_view<\/code> \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0443\u0436\u0435 \u0447\u0435\u043c\u0443-\u0442\u043e \u043f\u0440\u0438\u043d\u0430\u0434\u043b\u0435\u0436\u0430\u0442 \u0438 \u0434\u043b\u044f \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043b\u0438\u0448\u044c \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u043e\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435. \u041a\u0430\u043a \u0432\u044b \u0443\u0436\u0435 \u043c\u043e\u0433\u043b\u0438 \u0434\u043e\u0433\u0430\u0434\u0430\u0442\u044c\u0441\u044f, \u044d\u0442\u043e\u0442 \u043f\u043e\u0441\u0442 \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u0441\u0432\u044f\u0449\u0435\u043d \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438.<\/p>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u0440\u0435\u0447\u044c \u043f\u043e\u0439\u0434\u0435\u0442 \u043e\u0431 \u043e\u0434\u043d\u043e\u0439 \u0433\u043b\u0430\u0432\u043d\u044b\u0445 \u0444\u0438\u0447 C++17.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/c90\/a79\/1d2\/c90a791d2ea2741c82ac964ca411c27c.png\" width=\"893\" height=\"431\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c90\/a79\/1d2\/c90a791d2ea2741c82ac964ca411c27c.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u042f \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u044e, \u0447\u0442\u043e \u0432\u044b \u0443\u0436\u0435 \u0438\u043c\u0435\u0435\u0442 \u0431\u0430\u0437\u043e\u0432\u043e\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043e <code>std::string_view<\/code>. \u0415\u0441\u043b\u0438 \u043d\u0435\u0442, \u0442\u043e \u043c\u043e\u0436\u0435\u0442\u0435 \u0441\u043f\u0435\u0440\u0432\u0430 \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u043c\u043e\u0439 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u043f\u043e\u0441\u0442<a href=\"https:\/\/www.modernescpp.com\/index.php\/c-17-what-s-new-in-the-library\"> \u201cC++17 &#8212; \u0427\u0442\u043e \u043d\u043e\u0432\u043e\u0433\u043e \u0432 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435<\/a>\u201d. \u0421\u0442\u0440\u043e\u043a\u0430 C++ \u043f\u043e\u0445\u043e\u0436\u0430 \u043d\u0430 \u0442\u043e\u043d\u043a\u0443\u044e \u043e\u0431\u0435\u0440\u0442\u043a\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0445\u0440\u0430\u043d\u0438\u0442 \u0441\u0432\u043e\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u043a\u0443\u0447\u0435. \u041f\u043e\u044d\u0442\u043e\u043c\u0443, \u043a\u043e\u0433\u0434\u0430 \u0432\u044b \u0438\u043c\u0435\u0435\u0442\u0435 \u0434\u0435\u043b\u043e \u0441\u043e \u0441\u0442\u0440\u043e\u043a\u0430\u043c\u0438 \u0432 C \u0438 C++, \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 &#8212; \u0441\u0430\u043c\u043e\u0435 \u0437\u0430\u0443\u0440\u044f\u0434\u043d\u043e\u0435 \u044f\u0432\u043b\u0435\u043d\u0438\u0435. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0436\u0435 \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c\u0441\u044f \u0441 \u044d\u0442\u0438\u043c.<\/p>\n<h3>\u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u0441\u0442\u0440\u043e\u043a<\/h3>\n<p>\u041e\u0447\u0435\u043d\u044c \u0441\u043a\u043e\u0440\u043e \u0432\u044b \u0443\u0432\u0438\u0434\u0438\u0442\u0435, \u043f\u043e\u0447\u0435\u043c\u0443 \u044f \u043d\u0430\u0437\u0432\u0430\u043b \u044d\u0442\u043e\u0442 \u0430\u0431\u0437\u0430\u0446 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u0441\u0442\u0440\u043e\u043a.<\/p>\n<pre><code class=\"cpp\">\/\/ sso.cpp  #include &lt;iostream> #include &lt;string>  void* operator new(std::size_t count){     std::cout &lt;&lt; \"   \" &lt;&lt; count &lt;&lt; \" bytes\" &lt;&lt; std::endl;     return malloc(count); }  void getString(const std::string&amp; str){}  int main() {      std::cout &lt;&lt; std::endl;      std::cout &lt;&lt; \"std::string\" &lt;&lt; std::endl;      std::string small = \"0123456789\";     std::string substr = small.substr(5);     std::cout &lt;&lt; \"   \" &lt;&lt; substr &lt;&lt; std::endl;      std::cout &lt;&lt; std::endl;      std::cout &lt;&lt; \"getString\" &lt;&lt; std::endl;      getString(small);     getString(\"0123456789\");     const char message []= \"0123456789\";     getString(message);      std::cout &lt;&lt; std::endl;  }<\/code><\/pre>\n<p>\u042f \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u0438\u043b \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0439 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 <code>new<\/code> \u0432 \u0441\u0442\u0440\u043e\u043a\u0430\u0445 6-9. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0432\u044b \u0441\u043c\u043e\u0436\u0435\u0442\u0435 \u0443\u0432\u0438\u0434\u0435\u0442\u044c, \u043a\u0430\u043a\u0430\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438. \u0414\u0430 \u043b\u0430\u0434\u043d\u043e, \u044d\u0442\u043e \u043f\u0440\u043e\u0449\u0435 \u043f\u0430\u0440\u0435\u043d\u043e\u0439 \u0440\u0435\u043f\u044b. \u041a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0442 \u0441\u0442\u0440\u043e\u043a\u0438 19, 20, 28 \u0438 29. \u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u0432\u044b\u0432\u043e\u0434:<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/977\/55b\/219\/97755b219a1b6af1284869d4fa70ef37.png\" width=\"380\" height=\"190\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/977\/55b\/219\/97755b219a1b6af1284869d4fa70ef37.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0427\u0442\u043e \u0437\u0430 &#8230;? \u042f \u0433\u043e\u0432\u043e\u0440\u0438\u043b, \u0447\u0442\u043e \u0441\u0442\u0440\u043e\u043a\u0438 \u0445\u0440\u0430\u043d\u044f\u0442 \u0441\u0432\u043e\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u043a\u0443\u0447\u0435. \u041d\u043e \u044d\u0442\u043e \u0432\u0435\u0440\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u0435\u0441\u043b\u0438 \u0434\u043b\u0438\u043d\u0430 \u0441\u0442\u0440\u043e\u043a\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0430\u0437\u043c\u0435\u0440, \u0437\u0430\u0432\u0438\u0441\u044f\u0449\u0438\u0439 \u043e\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438. \u042d\u0442\u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440 \u0434\u043b\u044f <code>std::string<\/code> \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 15 \u0432 MSVC \u0438 GCC \u0438 23 \u0432 Clang.<\/p>\n<p>\u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0432 \u0441\u0430\u043c\u043e\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u0435 \u0441\u0442\u0440\u043e\u043a\u0438. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f.<\/p>\n<p>\u0425\u043e\u0440\u043e\u0448\u043e, \u043e\u0442\u043d\u044b\u043d\u0435 \u043c\u043e\u0438 \u0441\u0442\u0440\u043e\u043a\u0438 \u0432\u0441\u0435\u0433\u0434\u0430 \u0431\u0443\u0434\u0443\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435 30 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432, \u0447\u0442\u043e\u0431\u044b \u043c\u043d\u0435 \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u043e\u0442\u0432\u043b\u0435\u043a\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044e \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u0441\u0442\u0440\u043e\u043a. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043d\u0430\u0447\u043d\u0435\u043c \u0441 \u043d\u0430\u0447\u0430\u043b\u0430, \u043d\u043e \u043d\u0430 \u044d\u0442\u043e\u0442 \u0440\u0430\u0437 \u0441 \u0431\u043e\u043b\u0435\u0435 \u0434\u043b\u0438\u043d\u043d\u044b\u043c\u0438 \u0441\u0442\u0440\u043e\u043a\u0430\u043c\u0438.<\/p>\n<h3>\u0412\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f<\/h3>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u0437\u0430\u0434 \u043a \u043b\u0443\u0447\u0435\u0437\u0430\u0440\u043d\u043e\u043c\u0443 <code>std::string_view<\/code>. \u0412 \u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u043e\u0442 <code>std::string<\/code>, <code>std::string_view<\/code> \u043d\u0435 \u0432\u044b\u0434\u0435\u043b\u044f\u0435\u0442 \u043f\u0430\u043c\u044f\u0442\u044c. \u0418 \u0432\u043e\u0442 \u0442\u043e\u043c\u0443 \u0434\u043e\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u043e:<\/p>\n<pre><code class=\"cpp\">\/\/ stringView.cpp  #include &lt;cassert> #include &lt;iostream> #include &lt;string>  #include &lt;string_view>  void* operator new(std::size_t count){     std::cout &lt;&lt; \"   \" &lt;&lt; count &lt;&lt; \" bytes\" &lt;&lt; std::endl;     return malloc(count); }  void getString(const std::string&amp; str){}  void getStringView(std::string_view strView){}  int main() {      std::cout &lt;&lt; std::endl;      std::cout &lt;&lt; \"std::string\" &lt;&lt; std::endl;      std::string large = \"0123456789-123456789-123456789-123456789\";     std::string substr = large.substr(10);      std::cout &lt;&lt; std::endl;      std::cout &lt;&lt; \"std::string_view\" &lt;&lt; std::endl;      std::string_view largeStringView{large.c_str(), large.size()};     largeStringView.remove_prefix(10);      assert(substr == largeStringView);      std::cout &lt;&lt; std::endl;      std::cout &lt;&lt; \"getString\" &lt;&lt; std::endl;      getString(large);     getString(\"0123456789-123456789-123456789-123456789\");     const char message []= \"0123456789-123456789-123456789-123456789\";     getString(message);      std::cout &lt;&lt; std::endl;      std::cout &lt;&lt; \"getStringView\" &lt;&lt; std::endl;      getStringView(large);     getStringView(\"0123456789-123456789-123456789-123456789\");     getStringView(message);      std::cout &lt;&lt; std::endl;  }<\/code><\/pre>\n<p>\u0415\u0449\u0435 \u0440\u0430\u0437. \u0412\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0432 \u0441\u0442\u0440\u043e\u043a\u0430\u0445 24, 25, 41 \u0438 43. \u041d\u043e \u0447\u0442\u043e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445 \u0432\u044b\u0437\u043e\u0432\u0430\u0445 \u0432 \u0441\u0442\u0440\u043e\u043a\u0430\u0445 31, 32, 50 \u0438 51? \u041d\u0438\u043a\u0430\u043a\u043e\u0433\u043e \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438 \u043d\u0435 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442!<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/d16\/352\/a68\/d16352a68a69eaf4ba3f2c6b934b7d98.png\" width=\"383\" height=\"293\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/d16\/352\/a68\/d16352a68a69eaf4ba3f2c6b934b7d98.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u042d\u0442\u043e \u0432\u043f\u0435\u0447\u0430\u0442\u043b\u044f\u0435\u0442. \u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0434\u0430\u0436\u0435 \u043d\u0435 \u0441\u043e\u043c\u043d\u0435\u0432\u0430\u0442\u044c\u0441\u044f, \u0447\u0442\u043e \u044d\u0442\u043e \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043f\u0440\u0438\u0440\u043e\u0441\u0442 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u2014 \u043e\u0447\u0435\u043d\u044c \u0437\u0430\u0442\u0440\u0430\u0442\u043d\u0430\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f. \u041e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0445\u043e\u0440\u043e\u0448\u043e \u0437\u0430\u043c\u0435\u0442\u0435\u043d \u044d\u0442\u043e\u0442 \u043f\u0440\u0438\u0440\u043e\u0441\u0442 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438, \u043a\u043e\u0433\u0434\u0430 \u0432\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0435 \u043f\u043e\u0434\u0441\u0442\u0440\u043e\u043a\u0438 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445 \u0441\u0442\u0440\u043e\u043a.<\/p>\n<h3>O(n) vs. O(1)<\/h3>\n<p><code>std::string<\/code> \u0438 <code>std::string_view<\/code> \u043e\u0431\u0430 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442 \u043c\u0435\u0442\u043e\u0434 <code>substr<\/code>. \u041c\u0435\u0442\u043e\u0434 <code>std::string<\/code> \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043f\u043e\u0434\u0441\u0442\u0440\u043e\u043a\u0443, \u0430 \u043c\u0435\u0442\u043e\u0434 <code>std::string_view<\/code> \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043f\u043e\u0434\u0441\u0442\u0440\u043e\u043a\u0438. \u042d\u0442\u043e \u0437\u0432\u0443\u0447\u0438\u0442 \u043d\u0435 \u0442\u0430\u043a \u0437\u0430\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u044e\u0449\u0435, \u043d\u043e \u043c\u0435\u0436\u0434\u0443 \u044d\u0442\u0438\u043c\u0438 \u043c\u0435\u0442\u043e\u0434\u0430\u043c\u0438 \u0435\u0441\u0442\u044c \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0440\u0430\u0437\u043d\u0438\u0446\u0430. <code>std::string::substr<\/code> \u0438\u043c\u0435\u0435\u0442 \u043b\u0438\u043d\u0435\u0439\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c, \u0430 <code>std::string_view::substr<\/code> \u2014 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c. \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043d\u0430\u0434 <code>std::string<\/code> \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u043f\u043e\u0434\u0441\u0442\u0440\u043e\u043a\u0438, \u0430 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043d\u0430\u0434 <code>std::string_view<\/code> \u2014 \u043d\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u0442.<\/p>\n<p>\u0412\u043e\u0442 \u0442\u0435\u043f\u0435\u0440\u044c \u043c\u043d\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043b\u044e\u0431\u043e\u043f\u044b\u0442\u043d\u043e. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u0440\u043e\u0432\u0435\u0434\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e\u0435 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438.<\/p>\n<pre><code class=\"cpp\">\/\/ substr.cpp  #include &lt;chrono> #include &lt;fstream> #include &lt;iostream> #include &lt;random> #include &lt;sstream> #include &lt;string> #include &lt;vector>  #include &lt;string_view>  static const int count = 30; static const int access = 10000000;  int main(){      std::cout &lt;&lt; std::endl;      std::ifstream inFile(\"grimm.txt\");      std::stringstream strStream;     strStream &lt;&lt;  inFile.rdbuf();     std::string grimmsTales = strStream.str();      size_t size = grimmsTales.size();      std::cout &lt;&lt; \"Grimms' Fairy Tales size: \" &lt;&lt; size &lt;&lt; std::endl;     std::cout &lt;&lt; std::endl;      \/\/ \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f     std::random_device seed;     std::mt19937 engine(seed());     std::uniform_int_distribution&lt;> uniformDist(0, size - count - 2);     std::vector&lt;int> randValues;     for (auto i = 0; i &lt;  access; ++i) randValues.push_back(uniformDist(engine));      auto start = std::chrono::steady_clock::now();     for (auto i = 0; i  &lt; access; ++i ) {         grimmsTales.substr(randValues[i], count);     }     std::chrono::duration&lt;double> durString= std::chrono::steady_clock::now() - start;     std::cout &lt;&lt; \"std::string::substr:      \" &lt;&lt; durString.count() &lt;&lt; \" seconds\" &lt;&lt; std::endl;      std::string_view grimmsTalesView{grimmsTales.c_str(), size};     start = std::chrono::steady_clock::now();     for (auto i = 0; i  &lt; access; ++i ) {         grimmsTalesView.substr(randValues[i], count);     }     std::chrono::duration&lt;double> durStringView= std::chrono::steady_clock::now() - start;     std::cout &lt;&lt; \"std::string_view::substr: \" &lt;&lt; durStringView.count() &lt;&lt; \" seconds\" &lt;&lt; std::endl;      std::cout &lt;&lt; std::endl;      std::cout &lt;&lt; \"durString.count()\/durStringView.count(): \" &lt;&lt; durString.count()\/durStringView.count() &lt;&lt; std::endl;      std::cout &lt;&lt; std::endl;  }<\/code><\/pre>\n<p>\u00a0\u041f\u0440\u0435\u0436\u0434\u0435 \u0447\u0435\u043c \u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044e \u0432\u0430\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b, \u043f\u043e\u0437\u0432\u043e\u043b\u044c\u0442\u0435 \u043c\u043d\u0435 \u0441\u043a\u0430\u0437\u0430\u0442\u044c \u043f\u0430\u0440\u0443 \u0441\u043b\u043e\u0432 \u043e \u043c\u043e\u0435\u043c \u0442\u0435\u0441\u0442\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438. \u041e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u0438\u0434\u0435\u044f \u044d\u0442\u043e\u0433\u043e \u0442\u0435\u0441\u0442\u0430 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0444\u0430\u0439\u043b \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 <code>std::string<\/code> \u0438 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043c\u043d\u043e\u0433\u043e \u043f\u043e\u0434\u0441\u0442\u0440\u043e\u043a \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>std::string<\/code> \u0438 <code>std::string_view<\/code>. \u0417\u0434\u0435\u0441\u044c \u043c\u0435\u043d\u044f \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0443\u0435\u0442, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0437\u0430\u0439\u043c\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u044d\u0442\u0438\u0445 \u043f\u043e\u0434\u0441\u0442\u0440\u043e\u043a.<\/p>\n<p>\u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0441\u0432\u043e\u0435\u0433\u043e \u0434\u043b\u0438\u043d\u043d\u043e\u0433\u043e \u0444\u0430\u0439\u043b\u0430 \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b \u201c\u0421\u043a\u0430\u0437\u043a\u0438 \u0431\u0440\u0430\u0442\u044c\u0435\u0432 \u0413\u0440\u0438\u043c\u043c\u201d. \u0410 \u0447\u0442\u043e \u0435\u0449\u0435 \u043c\u043d\u0435 \u0431\u044b\u043b\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c? \u0421\u0442\u0440\u043e\u043a\u0430 <code>grimmTales<\/code> (\u0441\u0442\u0440\u043e\u0447\u043a\u0430 24) \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u0444\u0430\u0439\u043b\u0430. \u042f \u0437\u0430\u043f\u043e\u043b\u043d\u044f\u044e <code>std::vector&lt;int><\/code> \u0432 \u0441\u0442\u0440\u043e\u0447\u043a\u0435 37 \u0441 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c access (10&#8217;000&#8217;000) \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 [0, \u0440\u0430\u0437\u043c\u0435\u0440 &#8212; \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e &#8212; 2] (\u0441\u0442\u0440\u043e\u0447\u043a\u0430 34). \u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u0441\u0430\u043c \u0442\u0435\u0441\u0442 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438. \u042f \u0441\u043e\u0437\u0434\u0430\u044e \u0432 \u0441\u0442\u0440\u043e\u0447\u043a\u0430\u0445 \u0441 39 \u043f\u043e 41 <code>access<\/code> \u043f\u043e\u0434\u0441\u0442\u0440\u043e\u043a \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b <code>count<\/code>. <code>count<\/code> \u0440\u0430\u0432\u043d\u043e 30. \u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u0441\u0442\u0440\u043e\u043a \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u043c\u0435\u0448\u0430\u0442\u044c\u0441\u044f \u043f\u043e\u0434 \u043d\u043e\u0433\u0430\u043c\u0438. \u042f \u0434\u0435\u043b\u0430\u044e \u0442\u043e \u0436\u0435 \u0441\u0430\u043c\u043e\u0435 \u0432 \u0441\u0442\u0440\u043e\u0447\u043a\u0430\u0445 \u0441 47 \u043f\u043e 49 \u0441 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435\u043c <code>std::string_view<\/code>.<\/p>\n<p>\u0412\u043e\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b. \u041d\u0438\u0436\u0435 \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0432\u0438\u0434\u0435\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u0444\u0430\u0439\u043b\u0430, \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438 \u0434\u043b\u044f <code>std::string::substr<\/code> \u0438 <code>std::string_view::substr<\/code>, \u0438 \u0441\u043e\u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u0435 \u043c\u0435\u0436\u0434\u0443 \u043d\u0438\u043c\u0438. \u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430 \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b GCC 6.3.0.<\/p>\n<h4>\u0414\u043b\u044f \u0441\u0442\u0440\u043e\u043a \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u043c \u0432 30 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432<\/h4>\n<p>\u0422\u043e\u043b\u044c\u043a\u043e \u0438\u0437 \u043b\u044e\u0431\u043e\u043f\u044b\u0442\u0441\u0442\u0432\u0430. \u041f\u043e\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438 \u0431\u0435\u0437 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/f5d\/514\/ea7\/f5d514ea72e2b60aa4f335d47c6508f8.png\" width=\"543\" height=\"221\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/f5d\/514\/ea7\/f5d514ea72e2b60aa4f335d47c6508f8.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u041d\u043e \u0442\u0435\u043f\u0435\u0440\u044c \u043a \u0431\u043e\u043b\u0435\u0435 \u0432\u0430\u0436\u043d\u044b\u043c \u043f\u043e\u043a\u0430\u0437\u0435\u0442\u043b\u044f\u043c. GCC \u0441 \u043f\u043e\u043b\u043d\u043e\u0439 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/416\/dcc\/677\/416dcc677f430eaca70e3a4100f2ae1b.png\" width=\"543\" height=\"221\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/416\/dcc\/677\/416dcc677f430eaca70e3a4100f2ae1b.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0435 \u0438\u043c\u0435\u0435\u0442 \u043e\u0441\u043e\u0431\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 <code>std::string<\/code>, \u043d\u043e \u043c\u044b \u043d\u0430\u0431\u043b\u044e\u0434\u0430\u0435\u043c \u0431\u043e\u043b\u044c\u0448\u0443\u044e \u0440\u0430\u0437\u043d\u0438\u0446\u0443 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 <code>std::string_view<\/code>. \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043f\u043e\u0434\u0441\u0442\u0440\u043e\u043a\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>std::string_view<\/code> \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0432 45 \u0440\u0430\u0437 \u0431\u044b\u0441\u0442\u0440\u0435\u0435, \u0447\u0435\u043c \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 <code>std::string<\/code>. \u0427\u0442\u043e \u044d\u0442\u043e, \u0435\u0441\u043b\u0438 \u043d\u0435 \u043f\u043e\u0432\u043e\u0434 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>std::string_view<\/code>?\u00a0<\/p>\n<h4>\u0414\u043b\u044f \u0441\u0442\u0440\u043e\u043a \u0434\u0440\u0443\u0433\u0438\u0445 \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u0432<\/h4>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043d\u0435 \u0441\u0442\u0430\u043b\u043e \u0435\u0449\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0435\u0435. \u0427\u0442\u043e \u0431\u0443\u0434\u0435\u0442, \u0435\u0441\u043b\u0438 \u044f \u043f\u043e\u0438\u0433\u0440\u0430\u044e \u0441 \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u043c <code>count<\/code> \u043f\u043e\u0434\u0441\u0442\u0440\u043e\u043a\u0438? \u0420\u0430\u0437\u0443\u043c\u0435\u0435\u0442\u0441\u044f, \u0432\u0441\u0435 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438 \u0434\u043b\u044f \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439. \u042f \u043e\u043a\u0440\u0443\u0433\u043b\u0438\u043b \u0438\u0445 \u0434\u043e 3-\u0433\u043e \u0437\u043d\u0430\u043a\u0430 \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u043f\u044f\u0442\u043e\u0439.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/a24\/4fd\/4be\/a244fd4bef148bf8e99d0e2ccb044b23.png\" width=\"831\" height=\"325\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/a24\/4fd\/4be\/a244fd4bef148bf8e99d0e2ccb044b23.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u042f \u043d\u0435 \u0443\u0434\u0438\u0432\u043b\u0435\u043d, \u0446\u0438\u0444\u0440\u044b \u043e\u0442\u0440\u0430\u0436\u0430\u044e\u0442 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0438 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 <code>std::string::substr<\/code> \u0432 \u043f\u0440\u043e\u0442\u0438\u0432\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 <code>std::string_view::substr<\/code>. \u0421\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u043b\u0438\u043d\u0435\u0439\u043d\u043e \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u043f\u043e\u0434\u0441\u0442\u0440\u043e\u043a\u0438; \u0432\u0442\u043e\u0440\u043e\u0439 \u043d\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u043f\u043e\u0434\u0441\u0442\u0440\u043e\u043a\u0438. \u0412 \u043a\u043e\u043d\u0435\u0447\u043d\u043e\u043c \u0438\u0442\u043e\u0433\u0435, <code>std::string_view<\/code> \u0440\u0430\u0434\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043f\u0440\u0435\u0432\u043e\u0441\u0445\u043e\u0434\u0438\u0442 <code>std::string<\/code>.<\/p>\n<hr\/>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u0432\u0435\u0447\u0435\u0440\u043e\u043c \u0441\u043e\u0441\u0442\u043e\u0438\u0442\u0441\u044f \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0443\u0440\u043e\u043a, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c\u0441\u044f, \u043a\u0430\u043a\u0438\u0435 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u0432\u043a\u043b\u044e\u0447\u0435\u043d\u044b \u0432 STL. \u0412 \u0445\u043e\u0434\u0435 \u0437\u0430\u043d\u044f\u0442\u0438\u044f \u043f\u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u043c\u0441\u044f \u0441 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430\u043c\u0438 \u043f\u043e\u0438\u0441\u043a\u0430 \u0438 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438. \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c\u0441\u044f \u043c\u043e\u0436\u043d\u043e <a href=\"https:\/\/otus.pw\/4LhR\/\">\u043d\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0435 \u043a\u0443\u0440\u0441\u0430 &#171;C++ Developer. Professional&#187;.<\/a><\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p> <!----> <!----><\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/company\/otus\/blog\/715608\/\"> https:\/\/habr.com\/ru\/company\/otus\/blog\/715608\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 <code>std::string_view<\/code> \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0443\u0436\u0435 \u0447\u0435\u043c\u0443-\u0442\u043e \u043f\u0440\u0438\u043d\u0430\u0434\u043b\u0435\u0436\u0430\u0442 \u0438 \u0434\u043b\u044f \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043b\u0438\u0448\u044c \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u043e\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435. \u041a\u0430\u043a \u0432\u044b \u0443\u0436\u0435 \u043c\u043e\u0433\u043b\u0438 \u0434\u043e\u0433\u0430\u0434\u0430\u0442\u044c\u0441\u044f, \u044d\u0442\u043e\u0442 \u043f\u043e\u0441\u0442 \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u0441\u0432\u044f\u0449\u0435\u043d \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438.<\/p>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u0440\u0435\u0447\u044c \u043f\u043e\u0439\u0434\u0435\u0442 \u043e\u0431 \u043e\u0434\u043d\u043e\u0439 \u0433\u043b\u0430\u0432\u043d\u044b\u0445 \u0444\u0438\u0447 C++17.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u042f \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u044e, \u0447\u0442\u043e \u0432\u044b \u0443\u0436\u0435 \u0438\u043c\u0435\u0435\u0442 \u0431\u0430\u0437\u043e\u0432\u043e\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043e <code>std::string_view<\/code>. \u0415\u0441\u043b\u0438 \u043d\u0435\u0442, \u0442\u043e \u043c\u043e\u0436\u0435\u0442\u0435 \u0441\u043f\u0435\u0440\u0432\u0430 \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u043c\u043e\u0439 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u043f\u043e\u0441\u0442<a href=\"https:\/\/www.modernescpp.com\/index.php\/c-17-what-s-new-in-the-library\"> \u201cC++17 &#8212; \u0427\u0442\u043e \u043d\u043e\u0432\u043e\u0433\u043e \u0432 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435<\/a>\u201d. \u0421\u0442\u0440\u043e\u043a\u0430 C++ \u043f\u043e\u0445\u043e\u0436\u0430 \u043d\u0430 \u0442\u043e\u043d\u043a\u0443\u044e \u043e\u0431\u0435\u0440\u0442\u043a\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0445\u0440\u0430\u043d\u0438\u0442 \u0441\u0432\u043e\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u043a\u0443\u0447\u0435. \u041f\u043e\u044d\u0442\u043e\u043c\u0443, \u043a\u043e\u0433\u0434\u0430 \u0432\u044b \u0438\u043c\u0435\u0435\u0442\u0435 \u0434\u0435\u043b\u043e \u0441\u043e \u0441\u0442\u0440\u043e\u043a\u0430\u043c\u0438 \u0432 C \u0438 C++, \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 &#8212; \u0441\u0430\u043c\u043e\u0435 \u0437\u0430\u0443\u0440\u044f\u0434\u043d\u043e\u0435 \u044f\u0432\u043b\u0435\u043d\u0438\u0435. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0436\u0435 \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c\u0441\u044f \u0441 \u044d\u0442\u0438\u043c.<\/p>\n<h3>\u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u0441\u0442\u0440\u043e\u043a<\/h3>\n<p>\u041e\u0447\u0435\u043d\u044c \u0441\u043a\u043e\u0440\u043e \u0432\u044b \u0443\u0432\u0438\u0434\u0438\u0442\u0435, \u043f\u043e\u0447\u0435\u043c\u0443 \u044f \u043d\u0430\u0437\u0432\u0430\u043b \u044d\u0442\u043e\u0442 \u0430\u0431\u0437\u0430\u0446 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u0441\u0442\u0440\u043e\u043a.<\/p>\n<pre><code class=\"cpp\">\/\/ sso.cpp  #include &lt;iostream> #include &lt;string>  void* operator new(std::size_t count){     std::cout &lt;&lt; \"   \" &lt;&lt; count &lt;&lt; \" bytes\" &lt;&lt; std::endl;     return malloc(count); }  void getString(const std::string&amp; str){}  int main() {      std::cout &lt;&lt; std::endl;      std::cout &lt;&lt; \"std::string\" &lt;&lt; std::endl;      std::string small = \"0123456789\";     std::string substr = small.substr(5);     std::cout &lt;&lt; \"   \" &lt;&lt; substr &lt;&lt; std::endl;      std::cout &lt;&lt; std::endl;      std::cout &lt;&lt; \"getString\" &lt;&lt; std::endl;      getString(small);     getString(\"0123456789\");     const char message []= \"0123456789\";     getString(message);      std::cout &lt;&lt; std::endl;  }<\/code><\/pre>\n<p>\u042f \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u0438\u043b \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0439 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 <code>new<\/code> \u0432 \u0441\u0442\u0440\u043e\u043a\u0430\u0445 6-9. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0432\u044b \u0441\u043c\u043e\u0436\u0435\u0442\u0435 \u0443\u0432\u0438\u0434\u0435\u0442\u044c, \u043a\u0430\u043a\u0430\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438. \u0414\u0430 \u043b\u0430\u0434\u043d\u043e, \u044d\u0442\u043e \u043f\u0440\u043e\u0449\u0435 \u043f\u0430\u0440\u0435\u043d\u043e\u0439 \u0440\u0435\u043f\u044b. \u041a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0442 \u0441\u0442\u0440\u043e\u043a\u0438 19, 20, 28 \u0438 29. \u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u0432\u044b\u0432\u043e\u0434:<\/p>\n<figure class=\"\"><figcaption><\/figcaption><\/figure>\n<p>\u0427\u0442\u043e \u0437\u0430 &#8230;? \u042f \u0433\u043e\u0432\u043e\u0440\u0438\u043b, \u0447\u0442\u043e \u0441\u0442\u0440\u043e\u043a\u0438 \u0445\u0440\u0430\u043d\u044f\u0442 \u0441\u0432\u043e\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u043a\u0443\u0447\u0435. \u041d\u043e \u044d\u0442\u043e \u0432\u0435\u0440\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u0435\u0441\u043b\u0438 \u0434\u043b\u0438\u043d\u0430 \u0441\u0442\u0440\u043e\u043a\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0430\u0437\u043c\u0435\u0440, \u0437\u0430\u0432\u0438\u0441\u044f\u0449\u0438\u0439 \u043e\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438. \u042d\u0442\u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440 \u0434\u043b\u044f <code>std::string<\/code> \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 15 \u0432 MSVC \u0438 GCC \u0438 23 \u0432 Clang.<\/p>\n<p>\u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0432 \u0441\u0430\u043c\u043e\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u0435 \u0441\u0442\u0440\u043e\u043a\u0438. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f.<\/p>\n<p>\u0425\u043e\u0440\u043e\u0448\u043e, \u043e\u0442\u043d\u044b\u043d\u0435 \u043c\u043e\u0438 \u0441\u0442\u0440\u043e\u043a\u0438 \u0432\u0441\u0435\u0433\u0434\u0430 \u0431\u0443\u0434\u0443\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435 30 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432, \u0447\u0442\u043e\u0431\u044b \u043c\u043d\u0435 \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u043e\u0442\u0432\u043b\u0435\u043a\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044e \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u0441\u0442\u0440\u043e\u043a. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043d\u0430\u0447\u043d\u0435\u043c \u0441 \u043d\u0430\u0447\u0430\u043b\u0430, \u043d\u043e \u043d\u0430 \u044d\u0442\u043e\u0442 \u0440\u0430\u0437 \u0441 \u0431\u043e\u043b\u0435\u0435 \u0434\u043b\u0438\u043d\u043d\u044b\u043c\u0438 \u0441\u0442\u0440\u043e\u043a\u0430\u043c\u0438.<\/p>\n<h3>\u0412\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f<\/h3>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u0437\u0430\u0434 \u043a \u043b\u0443\u0447\u0435\u0437\u0430\u0440\u043d\u043e\u043c\u0443 <code>std::string_view<\/code>. \u0412 \u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u043e\u0442 <code>std::string<\/code>, <code>std::string_view<\/code> \u043d\u0435 \u0432\u044b\u0434\u0435\u043b\u044f\u0435\u0442 \u043f\u0430\u043c\u044f\u0442\u044c. \u0418 \u0432\u043e\u0442 \u0442\u043e\u043c\u0443 \u0434\u043e\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u043e:<\/p>\n<pre><code class=\"cpp\">\/\/ stringView.cpp  #include &lt;cassert> #include &lt;iostream> #include &lt;string>  #include &lt;string_view>  void* operator new(std::size_t count){     std::cout &lt;&lt; \"   \" &lt;&lt; count &lt;&lt; \" bytes\" &lt;&lt; std::endl;     return malloc(count); }  void getString(const std::string&amp; str){}  void getStringView(std::string_view strView){}  int main() {      std::cout &lt;&lt; std::endl;      std::cout &lt;&lt; \"std::string\" &lt;&lt; std::endl;      std::string large = \"0123456789-123456789-123456789-123456789\";     std::string substr = large.substr(10);      std::cout &lt;&lt; std::endl;      std::cout &lt;&lt; \"std::string_view\" &lt;&lt; std::endl;      std::string_view largeStringView{large.c_str(), large.size()};     largeStringView.remove_prefix(10);      assert(substr == largeStringView);      std::cout &lt;&lt; std::endl;      std::cout &lt;&lt; \"getString\" &lt;&lt; std::endl;      getString(large);     getString(\"0123456789-123456789-123456789-123456789\");     const char message []= \"0123456789-123456789-123456789-123456789\";     getString(message);      std::cout &lt;&lt; std::endl;      std::cout &lt;&lt; \"getStringView\" &lt;&lt; std::endl;      getStringView(large);     getStringView(\"0123456789-123456789-123456789-123456789\");     getStringView(message);      std::cout &lt;&lt; std::endl;  }<\/code><\/pre>\n<p>\u0415\u0449\u0435 \u0440\u0430\u0437. \u0412\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0432 \u0441\u0442\u0440\u043e\u043a\u0430\u0445 24, 25, 41 \u0438 43. \u041d\u043e \u0447\u0442\u043e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445 \u0432\u044b\u0437\u043e\u0432\u0430\u0445 \u0432 \u0441\u0442\u0440\u043e\u043a\u0430\u0445 31, 32, 50 \u0438 51? \u041d\u0438\u043a\u0430\u043a\u043e\u0433\u043e \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438 \u043d\u0435 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442!<\/p>\n<figure class=\"\"><figcaption><\/figcaption><\/figure>\n<p>\u042d\u0442\u043e \u0432\u043f\u0435\u0447\u0430\u0442\u043b\u044f\u0435\u0442. \u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0434\u0430\u0436\u0435 \u043d\u0435 \u0441\u043e\u043c\u043d\u0435\u0432\u0430\u0442\u044c\u0441\u044f, \u0447\u0442\u043e \u044d\u0442\u043e \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043f\u0440\u0438\u0440\u043e\u0441\u0442 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u2014 \u043e\u0447\u0435\u043d\u044c \u0437\u0430\u0442\u0440\u0430\u0442\u043d\u0430\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f. \u041e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0445\u043e\u0440\u043e\u0448\u043e \u0437\u0430\u043c\u0435\u0442\u0435\u043d \u044d\u0442\u043e\u0442 \u043f\u0440\u0438\u0440\u043e\u0441\u0442 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438, \u043a\u043e\u0433\u0434\u0430 \u0432\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0435 \u043f\u043e\u0434\u0441\u0442\u0440\u043e\u043a\u0438 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445 \u0441\u0442\u0440\u043e\u043a.<\/p>\n<h3>O(n) vs. O(1)<\/h3>\n<p><code>std::string<\/code> \u0438 <code>std::string_view<\/code> \u043e\u0431\u0430 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442 \u043c\u0435\u0442\u043e\u0434 <code>substr<\/code>. \u041c\u0435\u0442\u043e\u0434 <code>std::string<\/code> \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043f\u043e\u0434\u0441\u0442\u0440\u043e\u043a\u0443, \u0430 \u043c\u0435\u0442\u043e\u0434 <code>std::string_view<\/code> \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043f\u043e\u0434\u0441\u0442\u0440\u043e\u043a\u0438. \u042d\u0442\u043e \u0437\u0432\u0443\u0447\u0438\u0442 \u043d\u0435 \u0442\u0430\u043a \u0437\u0430\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u044e\u0449\u0435, \u043d\u043e \u043c\u0435\u0436\u0434\u0443 \u044d\u0442\u0438\u043c\u0438 \u043c\u0435\u0442\u043e\u0434\u0430\u043c\u0438 \u0435\u0441\u0442\u044c \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0440\u0430\u0437\u043d\u0438\u0446\u0430. <code>std::string::substr<\/code> \u0438\u043c\u0435\u0435\u0442 \u043b\u0438\u043d\u0435\u0439\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c, \u0430 <code>std::string_view::substr<\/code> \u2014 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c. \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043d\u0430\u0434 <code>std::string<\/code> \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u043f\u043e\u0434\u0441\u0442\u0440\u043e\u043a\u0438, \u0430 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043d\u0430\u0434 <code>std::string_view<\/code> \u2014 \u043d\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u0442.<\/p>\n<p>\u0412\u043e\u0442 \u0442\u0435\u043f\u0435\u0440\u044c \u043c\u043d\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043b\u044e\u0431\u043e\u043f\u044b\u0442\u043d\u043e. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u0440\u043e\u0432\u0435\u0434\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e\u0435 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438.<\/p>\n<pre><code class=\"cpp\">\/\/ substr.cpp  #include &lt;chrono> #include &lt;fstream> #include &lt;iostream> #include &lt;random> #include &lt;sstream> #include &lt;string> #include &lt;vector>  #include &lt;string_view>  static const int count = 30; static const int access = 10000000;  int main(){      std::cout &lt;&lt; std::endl;      std::ifstream inFile(\"grimm.txt\");      std::stringstream strStream;     strStream &lt;&lt;  inFile.rdbuf();     std::string grimmsTales = strStream.str();      size_t size = grimmsTales.size();      std::cout &lt;&lt; \"Grimms' Fairy Tales size: \" &lt;&lt; size &lt;&lt; std::endl;     std::cout &lt;&lt; std::endl;      \/\/ \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f     std::random_device seed;     std::mt19937 engine(seed());     std::uniform_int_distribution&lt;> uniformDist(0, size - count - 2);     std::vector&lt;int> randValues;     for (auto i = 0; i &lt;  access; ++i) randValues.push_back(uniformDist(engine));      auto start = std::chrono::steady_clock::now();     for (auto i = 0; i  &lt; access; ++i ) {         grimmsTales.substr(randValues[i], count);     }     std::chrono::duration&lt;double> durString= std::chrono::steady_clock::now() - start;     std::cout &lt;&lt; \"std::string::substr:      \" &lt;&lt; durString.count() &lt;&lt; \" seconds\" &lt;&lt; std::endl;      std::string_view grimmsTalesView{grimmsTales.c_str(), size};     start = std::chrono::steady_clock::now();     for (auto i = 0; i  &lt; access; ++i ) {         grimmsTalesView.substr(randValues[i], count);     }     std::chrono::duration&lt;double> durStringView= std::chrono::steady_clock::now() - start;     std::cout &lt;&lt; \"std::string_view::substr: \" &lt;&lt; durStringView.count() &lt;&lt; \" seconds\" &lt;&lt; std::endl;      std::cout &lt;&lt; std::endl;      std::cout &lt;&lt; \"durString.count()\/durStringView.count(): \" &lt;&lt; durString.count()\/durStringView.count() &lt;&lt; std::endl;      std::cout &lt;&lt; std::endl;  }<\/code><\/pre>\n<p>\u00a0\u041f\u0440\u0435\u0436\u0434\u0435 \u0447\u0435\u043c \u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044e \u0432\u0430\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b, \u043f\u043e\u0437\u0432\u043e\u043b\u044c\u0442\u0435 \u043c\u043d\u0435 \u0441\u043a\u0430\u0437\u0430\u0442\u044c \u043f\u0430\u0440\u0443 \u0441\u043b\u043e\u0432 \u043e \u043c\u043e\u0435\u043c \u0442\u0435\u0441\u0442\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438. \u041e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u0438\u0434\u0435\u044f \u044d\u0442\u043e\u0433\u043e \u0442\u0435\u0441\u0442\u0430 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0444\u0430\u0439\u043b \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 <code>std::string<\/code> \u0438 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043c\u043d\u043e\u0433\u043e \u043f\u043e\u0434\u0441\u0442\u0440\u043e\u043a \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>std::string<\/code> \u0438 <code>std::string_view<\/code>. \u0417\u0434\u0435\u0441\u044c \u043c\u0435\u043d\u044f \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0443\u0435\u0442, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0437\u0430\u0439\u043c\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u044d\u0442\u0438\u0445 \u043f\u043e\u0434\u0441\u0442\u0440\u043e\u043a.<\/p>\n<p>\u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0441\u0432\u043e\u0435\u0433\u043e \u0434\u043b\u0438\u043d\u043d\u043e\u0433\u043e \u0444\u0430\u0439\u043b\u0430 \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b \u201c\u0421\u043a\u0430\u0437\u043a\u0438 \u0431\u0440\u0430\u0442\u044c\u0435\u0432 \u0413\u0440\u0438\u043c\u043c\u201d. \u0410 \u0447\u0442\u043e \u0435\u0449\u0435 \u043c\u043d\u0435 \u0431\u044b\u043b\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c? \u0421\u0442\u0440\u043e\u043a\u0430 <code>grimmTales<\/code> (\u0441\u0442\u0440\u043e\u0447\u043a\u0430 24) \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u0444\u0430\u0439\u043b\u0430. \u042f \u0437\u0430\u043f\u043e\u043b\u043d\u044f\u044e <code>std::vector&lt;int><\/code> \u0432 \u0441\u0442\u0440\u043e\u0447\u043a\u0435 37 \u0441 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c access (10&#8217;000&#8217;000) \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 [0, \u0440\u0430\u0437\u043c\u0435\u0440 &#8212; \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e &#8212; 2] (\u0441\u0442\u0440\u043e\u0447\u043a\u0430 34). \u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u0441\u0430\u043c \u0442\u0435\u0441\u0442 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438. \u042f \u0441\u043e\u0437\u0434\u0430\u044e \u0432 \u0441\u0442\u0440\u043e\u0447\u043a\u0430\u0445 \u0441 39 \u043f\u043e 41 <code>access<\/code> \u043f\u043e\u0434\u0441\u0442\u0440\u043e\u043a \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b <code>count<\/code>. <code>count<\/code> \u0440\u0430\u0432\u043d\u043e 30. \u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u0441\u0442\u0440\u043e\u043a \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u043c\u0435\u0448\u0430\u0442\u044c\u0441\u044f \u043f\u043e\u0434 \u043d\u043e\u0433\u0430\u043c\u0438. \u042f \u0434\u0435\u043b\u0430\u044e \u0442\u043e \u0436\u0435 \u0441\u0430\u043c\u043e\u0435 \u0432 \u0441\u0442\u0440\u043e\u0447\u043a\u0430\u0445 \u0441 47 \u043f\u043e 49 \u0441 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435\u043c <code>std::string_view<\/code>.<\/p>\n<p>\u0412\u043e\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b. \u041d\u0438\u0436\u0435 \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0432\u0438\u0434\u0435\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u0444\u0430\u0439\u043b\u0430, \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438 \u0434\u043b\u044f <code>std::string::substr<\/code> \u0438 <code>std::string_view::substr<\/code>, \u0438 \u0441\u043e\u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u0435 \u043c\u0435\u0436\u0434\u0443 \u043d\u0438\u043c\u0438. \u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430 \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b GCC 6.3.0.<\/p>\n<h4>\u0414\u043b\u044f \u0441\u0442\u0440\u043e\u043a \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u043c \u0432 30 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432<\/h4>\n<p>\u0422\u043e\u043b\u044c\u043a\u043e \u0438\u0437 \u043b\u044e\u0431\u043e\u043f\u044b\u0442\u0441\u0442\u0432\u0430. \u041f\u043e\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438 \u0431\u0435\u0437 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u041d\u043e \u0442\u0435\u043f\u0435\u0440\u044c \u043a \u0431\u043e\u043b\u0435\u0435 \u0432\u0430\u0436\u043d\u044b\u043c \u043f\u043e\u043a\u0430\u0437\u0435\u0442\u043b\u044f\u043c. GCC \u0441 \u043f\u043e\u043b\u043d\u043e\u0439 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0435 \u0438\u043c\u0435\u0435\u0442 \u043e\u0441\u043e\u0431\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 <code>std::string<\/code>, \u043d\u043e \u043c\u044b \u043d\u0430\u0431\u043b\u044e\u0434\u0430\u0435\u043c \u0431\u043e\u043b\u044c\u0448\u0443\u044e \u0440\u0430\u0437\u043d\u0438\u0446\u0443 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 <code>std::string_view<\/code>. \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043f\u043e\u0434\u0441\u0442\u0440\u043e\u043a\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>std::string_view<\/code> \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0432 45 \u0440\u0430\u0437 \u0431\u044b\u0441\u0442\u0440\u0435\u0435, \u0447\u0435\u043c \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 <code>std::string<\/code>. \u0427\u0442\u043e \u044d\u0442\u043e, \u0435\u0441\u043b\u0438 \u043d\u0435 \u043f\u043e\u0432\u043e\u0434 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>std::string_view<\/code>?\u00a0<\/p>\n<h4>\u0414\u043b\u044f \u0441\u0442\u0440\u043e\u043a \u0434\u0440\u0443\u0433\u0438\u0445 \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u0432<\/h4>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043d\u0435 \u0441\u0442\u0430\u043b\u043e \u0435\u0449\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0435\u0435. \u0427\u0442\u043e \u0431\u0443\u0434\u0435\u0442, \u0435\u0441\u043b\u0438 \u044f \u043f\u043e\u0438\u0433\u0440\u0430\u044e \u0441 \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u043c <code>count<\/code> \u043f\u043e\u0434\u0441\u0442\u0440\u043e\u043a\u0438? \u0420\u0430\u0437\u0443\u043c\u0435\u0435\u0442\u0441\u044f, \u0432\u0441\u0435 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438 \u0434\u043b\u044f \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439. \u042f \u043e\u043a\u0440\u0443\u0433\u043b\u0438\u043b \u0438\u0445 \u0434\u043e 3-\u0433\u043e \u0437\u043d\u0430\u043a\u0430 \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u043f\u044f\u0442\u043e\u0439.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u042f \u043d\u0435 \u0443\u0434\u0438\u0432\u043b\u0435\u043d, \u0446\u0438\u0444\u0440\u044b \u043e\u0442\u0440\u0430\u0436\u0430\u044e\u0442 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0438 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 <code>std::string::substr<\/code> \u0432 \u043f\u0440\u043e\u0442\u0438\u0432\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 <code>std::string_view::substr<\/code>. \u0421\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u043b\u0438\u043d\u0435\u0439\u043d\u043e \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u043f\u043e\u0434\u0441\u0442\u0440\u043e\u043a\u0438; \u0432\u0442\u043e\u0440\u043e\u0439 \u043d\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u043f\u043e\u0434\u0441\u0442\u0440\u043e\u043a\u0438. \u0412 \u043a\u043e\u043d\u0435\u0447\u043d\u043e\u043c \u0438\u0442\u043e\u0433\u0435, <code>std::string_view<\/code> \u0440\u0430\u0434\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043f\u0440\u0435\u0432\u043e\u0441\u0445\u043e\u0434\u0438\u0442 <code>std::string<\/code>.<\/p>\n<hr\/>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u0432\u0435\u0447\u0435\u0440\u043e\u043c \u0441\u043e\u0441\u0442\u043e\u0438\u0442\u0441\u044f \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0443\u0440\u043e\u043a, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c\u0441\u044f, \u043a\u0430\u043a\u0438\u0435 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u0432\u043a\u043b\u044e\u0447\u0435\u043d\u044b \u0432 STL. \u0412 \u0445\u043e\u0434\u0435 \u0437\u0430\u043d\u044f\u0442\u0438\u044f \u043f\u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u043c\u0441\u044f \u0441 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430\u043c\u0438 \u043f\u043e\u0438\u0441\u043a\u0430 \u0438 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438. \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c\u0441\u044f \u043c\u043e\u0436\u043d\u043e <a href=\"https:\/\/otus.pw\/4LhR\/\">\u043d\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0435 \u043a\u0443\u0440\u0441\u0430 &#171;C++ Developer. Professional&#187;.<\/a><\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p> <!----> <!----><\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/company\/otus\/blog\/715608\/\"> https:\/\/habr.com\/ru\/company\/otus\/blog\/715608\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-345139","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/345139","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=345139"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/345139\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=345139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=345139"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=345139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}