{"id":283457,"date":"2017-01-07T03:30:04","date_gmt":"2017-01-07T00:30:04","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=283457"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=283457","title":{"rendered":"\u041e \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u0441\u0442\u0440\u043e\u043a \u0432 \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u043c C++"},"content":{"rendered":"<p>\u0414\u043e\u0431\u0440\u043e\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0441\u0443\u0442\u043e\u043a!<\/p>\n<p>  <\/p>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u0445\u043e\u0442\u0435\u043b \u0431\u044b \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c \u043e \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044f\u0445 \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0441\u0442\u0440\u043e\u043a \u0432 \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u043c C++, \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u0441\u0432\u043e\u0438 \u043d\u0430\u0440\u0430\u0431\u043e\u0442\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u044f \u0443\u0436\u0435 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043b\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e \u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0441\u0440\u0430\u0432\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u043f\u043e\u0434\u0445\u043e\u0434\u043e\u0432 \u043a \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044e \u0441\u0442\u0440\u043e\u043a.<\/p>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<p>\u0421\u0442\u0440\u043e\u043a\u043e\u0432\u043e\u0435 \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u2014 \u044d\u0442\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0449\u0430\u044f \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0438\u0440\u0443\u044e\u0449\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443 \u0438\u0437 \u0441\u0442\u0440\u043e\u043a\u0438-\u0448\u0430\u0431\u043b\u043e\u043d\u0430 \u0438 \u043d\u0430\u0431\u043e\u0440\u0430 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432. \u0421\u0442\u0440\u043e\u043a\u0430-\u0448\u0430\u0431\u043b\u043e\u043d \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0442\u0435\u043a\u0441\u0442, \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043a\u043b\u044e\u0447\u0435\u043d\u044b \u043c\u0435\u0441\u0442\u043e\u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u0438 (placeholders), \u0432\u043c\u0435\u0441\u0442\u043e \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043f\u043e\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b.<\/p>\n<p>  <\/p>\n<p>\u0414\u043b\u044f \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u043e\u0441\u0442\u0438 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">int apples = 5; int oranges = 7; std::string str = format(&quot;I have %d apples and %d oranges, so I have %d fruits&quot;, apples, oranges, apples + oranges); std::cout &lt;&lt; str &lt;&lt; std::endl;<\/code><\/pre>\n<p>  <\/p>\n<p>\u0417\u0434\u0435\u0441\u044c:<br \/>  \u0421\u0442\u0440\u043e\u043a\u0430-\u0448\u0430\u0431\u043b\u043e\u043d: I have %d apples and %d oranges, so I have %d fruits<br \/>  \u041c\u0435\u0441\u0442\u043e\u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u0438: %d, %d, %d<br \/>  \u0410\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b: apples, oranges, apples + oranges<\/p>\n<p>  <\/p>\n<p>\u041f\u0440\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438 \u043f\u0440\u0438\u043c\u0435\u0440\u0430, \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0438\u0440\u0443\u044e\u0449\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443<\/p>\n<p>  <\/p>\n<pre><code>I have 5 apples and 7 oranges, so I have 12 fruits<\/code><\/pre>\n<p>  <\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u0447\u0442\u043e \u0436\u0435 \u043d\u0430\u043c \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 C++ \u0434\u043b\u044f \u0441\u0442\u0440\u043e\u043a\u043e\u0432\u043e\u0433\u043e \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f.<\/p>\n<p>  <\/p>\n<h3 id=\"nasledie-c\">\u041d\u0430\u0441\u043b\u0435\u0434\u0438\u0435 C<\/h3>\n<p>  <\/p>\n<p>\u0421\u0442\u0440\u043e\u043a\u043e\u0432\u043e\u0435 \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0432 C \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0441\u0435\u043c\u0435\u0439\u0441\u0442\u0432\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 Xprintf. \u0421 \u0442\u0435\u043c \u0436\u0435 \u0443\u0441\u043f\u0435\u0445\u043e\u043c, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u044d\u0442\u0438\u043c\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438 \u0438 \u0432 C++:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">char buf[100]; int res = snprintf(buf, sizeof(buf), &quot;I have %d apples and %d oranges, so I have %d fruits&quot;, apples, oranges, apples + oranges); std::string str = &quot;error!&quot;; if (res &gt;= 0 &amp;&amp; res &lt; sizeof(buf))     str = buf; std::cout &lt;&lt; str &lt;&lt; std::endl;<\/code><\/pre>\n<p>  <\/p>\n<p>\u042d\u0442\u043e \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043d\u0435\u043f\u043b\u043e\u0445\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u043d\u0435\u0441\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u043a\u0430\u0436\u0443\u0449\u0443\u044e\u0441\u044f \u043d\u0435\u0443\u043a\u043b\u044e\u0436\u0435\u0441\u0442\u044c:<\/p>\n<p>  <\/p>\n<ul>\n<li>\u044d\u0442\u043e \u0441\u0430\u043c\u044b\u0439 \u0431\u044b\u0441\u0442\u0440\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0441\u0442\u0440\u043e\u043a\u043e\u0432\u043e\u0433\u043e \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f<\/li>\n<li>\u044d\u0442\u043e\u0442 \u0441\u043f\u043e\u0441\u043e\u0431 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043d\u0430 \u0432\u0441\u0435\u0445 \u0432\u0435\u0440\u0441\u0438\u044f\u0445 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u043e\u0432, \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u044f \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438 \u043d\u043e\u0432\u044b\u0445 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043e\u0432<\/li>\n<\/ul>\n<p>  <\/p>\n<p>\u041d\u043e, \u043a\u043e\u043d\u0435\u0447\u043d\u043e, \u043d\u0435 \u043e\u0431\u043e\u0448\u043b\u043e\u0441\u044c \u0438 \u0431\u0435\u0437 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043a\u043e\u0432:<\/p>\n<p>  <\/p>\n<ul>\n<li>\u043d\u0443\u0436\u043d\u043e \u0437\u043d\u0430\u0442\u044c \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u0430\u043c\u044f\u0442\u0438 \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0438\u0440\u0443\u044e\u0449\u0435\u0439 \u0441\u0442\u0440\u043e\u043a\u0438, \u0447\u0442\u043e \u043d\u0435 \u0432\u0441\u0435\u0433\u0434\u0430 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c<\/li>\n<li>\u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u0438 \u0442\u0438\u043f\u0430 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0438 \u043c\u0435\u0441\u0442\u043e\u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u0435\u0439 \u043d\u0438\u043a\u0430\u043a \u043d\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u043a \u043e\u0448\u0438\u0431\u043a\u0430\u043c \u043f\u0440\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b <\/li>\n<\/ul>\n<p>  <\/p>\n<h3 id=\"funkciya-stdto_string\">\u0424\u0443\u043d\u043a\u0446\u0438\u044f std::to_string()<\/h3>\n<p>  <\/p>\n<p>\u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 C++11 \u0432 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435 \u043f\u043e\u044f\u0432\u0438\u043b\u0430\u0441\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044f std::to_string(), \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0435\u043c\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 \u0441\u0442\u0440\u043e\u043a\u0443. \u0424\u0443\u043d\u043a\u0446\u0438\u044f \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043d\u0435 \u0441\u043e \u0432\u0441\u0435\u043c\u0438 \u0442\u0438\u043f\u0430\u043c\u0438 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432, \u0430 \u0442\u043e\u043b\u044c\u043a\u043e \u0441\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c\u0438:<\/p>\n<p>  <\/p>\n<ul>\n<li>int<\/li>\n<li>long<\/li>\n<li>long long<\/li>\n<li>unsinged int<\/li>\n<li>unsinged long<\/li>\n<li>unsigned long long<\/li>\n<li>float<\/li>\n<li>double<\/li>\n<li>long double<\/li>\n<\/ul>\n<p>  <\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">std::string str = &quot;I have &quot; + std::to_string(apples) + &quot; apples and &quot; + std::to_string(oranges) + &quot; oranges, so I have &quot; + std::to_string(apples + oranges) + &quot; fruits&quot;; std::cout &lt;&lt; str &lt;&lt; std::endl;<\/code><\/pre>\n<p>  <\/p>\n<h3 id=\"klass-stdstringstream\">\u041a\u043b\u0430\u0441\u0441 std::stringstream<\/h3>\n<p>  <\/p>\n<p>\u041a\u043b\u0430\u0441\u0441 std::stringstream \u2014 \u044d\u0442\u043e \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0441\u0442\u0440\u043e\u043a\u043e\u0432\u043e\u0433\u043e \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0430\u043c \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 C++:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">std::stringstream ss; ss &lt;&lt; &quot;I have &quot; &lt;&lt; apples &lt;&lt; &quot; apples and &quot; &lt;&lt; oranges &lt;&lt; &quot; oranges, so I have &quot; &lt;&lt; apples + oranges &lt;&lt; &quot; fruits&quot;; std::string str = ss.str(); std::cout &lt;&lt; str &lt;&lt; std::endl;<\/code><\/pre>\n<p>  <\/p>\n<p>\u0421\u0442\u0440\u043e\u0433\u043e \u0433\u043e\u0432\u043e\u0440\u044f, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 std::stringstream \u043d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432 \u043f\u043e\u043b\u043d\u043e\u0439 \u043c\u0435\u0440\u0435 \u0441\u0442\u0440\u043e\u043a\u043e\u0432\u044b\u043c \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u043c, \u0442\u0430\u043a \u043a\u0430\u043a \u0432\u043c\u0435\u0441\u0442\u043e \u043c\u0435\u0441\u0442\u043e\u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u0435\u0439 \u043c\u044b \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u0432 \u0441\u0442\u0440\u043e\u043a\u0443-\u0448\u0430\u0431\u043b\u043e\u043d \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b. \u042d\u0442\u043e \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e \u0432 \u043f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445, \u043d\u043e \u0432 \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u0445 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0443\u0445\u0443\u0434\u0448\u0430\u0435\u0442 \u0447\u0438\u0442\u0430\u0435\u043c\u043e\u0441\u0442\u044c \u043a\u043e\u0434\u0430:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">ss &lt;&lt; &quot;A[&quot; &lt;&lt; i1 &lt;&lt; &quot;, &quot; &lt;&lt; j1 &lt;&lt; &quot;] + A[&quot; &lt;&lt; i2 &lt;&lt; &quot;, &quot; &lt;&lt; j2 &lt;&lt; &quot;] = &quot; &lt;&lt; A[i1][j1] + A[i2][j2];<\/code><\/pre>\n<p>  <\/p>\n<p>\u0441\u0440\u0430\u0432\u043d\u0438\u0442\u0435 \u0441:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">std::string str = format(&quot;A[%d, %d] + A[%d, %d] = %d&quot;, i1, j1, i2, j2, A[i1][j1] + A[i2][j2]);<\/code><\/pre>\n<p>  <\/p>\n<p>\u041e\u0431\u044a\u0435\u043a\u0442 std::sringstream \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0445 \u043e\u0431\u0435\u0440\u0442\u043e\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u0432 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c.<\/p>\n<p>  <\/p>\n<p>\u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 &quot;\u0447\u0435\u0433\u043e \u0443\u0433\u043e\u0434\u043d\u043e&quot; \u0432 \u0441\u0442\u0440\u043e\u043a\u0443:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">template&lt;typename T&gt; std::string to_string(const T &amp;t) {     std::stringstream ss;     ss &lt;&lt; t;     return ss.str(); }<\/code><\/pre>\n<p>  <\/p>\n<pre><code class=\"cpp\">std::string str = to_string(&quot;5&quot;);<\/code><\/pre>\n<p>  <\/p>\n<p>\u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0432\u043e &quot;\u0447\u0442\u043e \u0443\u0433\u043e\u0434\u043d\u043e&quot;:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">template&lt;typename T&gt; T from_string(const std::string &amp;str) {        std::stringstream ss(str);     T t;     ss &gt;&gt; t;     return t; }<\/code><\/pre>\n<p>  <\/p>\n<pre><code class=\"cpp\">int x = from_string&lt;int&gt;(&quot;5&quot;);<\/code><\/pre>\n<p>  <\/p>\n<p>\u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0432\u043e &quot;\u0447\u0442\u043e \u0443\u0433\u043e\u0434\u043d\u043e&quot; \u0441 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u043e\u0439:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">template&lt;typename T&gt; T from_string(const std::string &amp;str, bool &amp;ok) {        std::stringstream ss(str);     T t;     ss &gt;&gt; t;     ok = !ss.fail();     return t; }<\/code><\/pre>\n<p>  <\/p>\n<pre><code class=\"cpp\">bool ok = false; int x = from_string&lt;int&gt;(&quot;x5&quot;, ok); if (!ok) ...<\/code><\/pre>\n<p>  <\/p>\n<p>\u0422\u0430\u043a\u0436\u0435, \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0430\u0440\u0443 \u043e\u0431\u0435\u0440\u0442\u043e\u043a \u0434\u043b\u044f \u0443\u0434\u043e\u0431\u043d\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f std::stringstream \u0432 \u043e\u0434\u043d\u0443 \u0441\u0442\u0440\u043e\u043a\u0443.<\/p>\n<p>  <\/p>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 std::stringstream \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">class fstr final : public std::string { public:     fstr(const std::string &amp;str = &quot;&quot;)     {         *this += str;     }     template&lt;typename T&gt; fstr &amp;operator&lt;&lt;(const T &amp;t)     {         *this += to_string(t);         return *this;     } };<\/code><\/pre>\n<p>  <\/p>\n<pre><code class=\"cpp\">std::string str = fstr() &lt;&lt; &quot;I have &quot; &lt;&lt; apples &lt;&lt; &quot; apples and &quot; &lt;&lt; oranges &lt;&lt; &quot; oranges, so I have &quot; &lt;&lt; apples + oranges &lt;&lt; &quot; fruits&quot;;<\/code><\/pre>\n<p>  <\/p>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043e\u0434\u043d\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 std::stringstream \u0434\u043b\u044f \u0432\u0441\u0435\u0439 \u0441\u0442\u0440\u043e\u043a\u0438:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">class sstr final { public:     sstr(const std::string &amp;str = &quot;&quot;)             : ss_(str)     {     }     template&lt;typename T&gt; sstr &amp;operator&lt;&lt;(const T &amp;t)     {         ss_ &lt;&lt; t;         return *this;     }     operator std::string() const     {         return ss_.str();     } private:     std::stringstream ss_; };<\/code><\/pre>\n<p>  <\/p>\n<pre><code class=\"cpp\">std::string str = sstr() &lt;&lt; &quot;I have &quot; &lt;&lt; apples &lt;&lt; &quot; apples and &quot; &lt;&lt; oranges &lt;&lt; &quot; oranges, so I have &quot; &lt;&lt; apples + oranges &lt;&lt; &quot; fruits&quot;;<\/code><\/pre>\n<p>  <\/p>\n<p>\u0417\u0430\u0431\u0435\u0433\u0430\u044f \u0432\u043f\u0435\u0440\u0435\u0434, \u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c std::to_string \u0432 3-4 \u0440\u0430\u0437\u0430 \u0432\u044b\u0448\u0435, \u0447\u0435\u043c \u0443 to_string, \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u043e\u0439 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e std::stringstream. \u041f\u043e\u044d\u0442\u043e\u043c\u0443, \u043b\u043e\u0433\u0438\u0447\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c std::to_string \u0434\u043b\u044f \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0438\u0445 \u0442\u0438\u043f\u043e\u0432, \u0430 \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0445 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0448\u0430\u0431\u043b\u043e\u043d\u043d\u0443\u044e to_string:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">std::string to_string(int x) { return std::to_string(x); } std::string to_string(unsigned int x) { return std::to_string(x); } std::string to_string(long x) { return std::to_string(x); } std::string to_string(unsigned long x) { return std::to_string(x); } std::string to_string(long long x) { return std::to_string(x); } std::string to_string(unsigned long long x) { return std::to_string(x); } std::string to_string(float x) { return std::to_string(x); } std::string to_string(double x) { return std::to_string(x); } std::string to_string(long double x) { return std::to_string(x); } std::string to_string(const char *x) { return std::string(x); } std::string to_string(const std::string &amp;x) { return x; }  template&lt;typename T&gt; std::string to_string(const T &amp;t) {     std::stringstream ss;     ss &lt;&lt; t;     return ss.str(); }<\/code><\/pre>\n<p>  <\/p>\n<h3 id=\"biblioteka-boostformat\">\u0411\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 boost::format<\/h3>\n<p>  <\/p>\n<p>\u041d\u0430\u0431\u043e\u0440 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a boost \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043c\u043e\u0449\u043d\u044b\u043c \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u043e\u043c, \u043e\u0442\u043b\u0438\u0447\u043d\u043e \u0434\u043e\u043f\u043e\u043b\u043d\u044f\u044e\u0449\u0438\u043c \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430 \u044f\u0437\u044b\u043a\u0430 C++ \u0438 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438. \u0421\u0442\u0440\u043e\u043a\u043e\u0432\u043e\u0435 \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u043e\u0439 boost::format.<\/p>\n<p>  <\/p>\n<p>\u041f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0443\u043a\u0430\u0437\u0430\u043d\u0438\u0435 \u043a\u0430\u043a \u0442\u0438\u043f\u043e\u0432\u044b\u0445 \u043c\u0435\u0441\u0442\u043e\u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u0435\u0439:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">std::string str = (boost::format(&quot;I have %d apples and %d oranges, so I have %d fruits&quot;) % apples % oranges % (apples + oranges)).str();<\/code><\/pre>\n<p>  <\/p>\n<p>\u0442\u0430\u043a \u0438 \u043f\u043e\u0440\u044f\u0434\u043a\u043e\u0432\u044b\u0445:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">std::string str = (boost::format(&quot;I have %1% apples and %2% oranges, so I have %3% fruits&quot;) % apples % oranges % (apples + oranges)).str();<\/code><\/pre>\n<p>  <\/p>\n<p>\u0415\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u043a boost::format \u2014 \u043d\u0438\u0437\u043a\u0430\u044f \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c, \u044d\u0442\u043e \u0441\u0430\u043c\u044b\u0439 \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0441\u0442\u0440\u043e\u043a\u043e\u0432\u043e\u0433\u043e \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f. \u0422\u0430\u043a\u0436\u0435 \u044d\u0442\u043e\u0442 \u0441\u043f\u043e\u0441\u043e\u0431 \u043d\u0435\u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c, \u0435\u0441\u043b\u0438 \u0432 \u043f\u0440\u043e\u0435\u043a\u0442\u0435 \u043d\u0435\u043b\u044c\u0437\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0438\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438.<\/p>\n<p>  <\/p>\n<p>\u0418\u0442\u0430\u043a, \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e C++ \u0438 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u0430\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u043d\u0435 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u043d\u0430\u043c \u0443\u0434\u043e\u0431\u043d\u044b\u0445 \u0441\u0440\u0435\u0434\u0441\u0442\u0432 \u0441\u0442\u0440\u043e\u043a\u043e\u0432\u043e\u0433\u043e \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0431\u0443\u0434\u0435\u043c \u043f\u0438\u0441\u0430\u0442\u044c \u0447\u0442\u043e-\u0442\u043e \u0441\u0432\u043e\u0435.<\/p>\n<p>  <\/p>\n<h3 id=\"obertka-nad-vsnprintf\">\u041e\u0431\u0435\u0440\u0442\u043a\u0430 \u043d\u0430\u0434 vsnprintf<\/h3>\n<p>  <\/p>\n<p>\u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043e\u0431\u0435\u0440\u0442\u043a\u0443 \u043d\u0430\u0434 Xprintf \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439, \u0432\u044b\u0434\u0435\u043b\u044f\u044f \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0430\u043c\u044f\u0442\u0438 \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u044f \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432. <\/p>\n<p>  <\/p>\n<p>\u0414\u043b\u044f \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044e: <\/p>\n<p>  <\/p>\n<ol>\n<li>\u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u044b\u0434\u0435\u043b\u044f\u0435\u043c \u0442\u0430\u043a\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u0430\u043c\u044f\u0442\u0438, \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0432 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0435 \u0441\u043b\u0443\u0447\u0430\u0435\u0432<\/li>\n<li>\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f<\/li>\n<li>\u0435\u0441\u043b\u0438 \u0432\u044b\u0437\u043e\u0432 \u0437\u0430\u043a\u043e\u043d\u0447\u0438\u043b\u0441\u044f \u043d\u0435\u0443\u0434\u0430\u0447\u0435\u0439, \u0432\u044b\u0434\u0435\u043b\u0438\u043c \u0431\u043e\u043b\u044c\u0448\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u0438\u043c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u0448\u0430\u0433<\/li>\n<\/ol>\n<p>  <\/p>\n<p>\u0414\u043b\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c stdarg \u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044e vsnprintf.<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">std::string format(const char *fmt, ...) {     va_list args;     va_start(args, fmt);     std::vector&lt;char&gt; v(1024);     while (true)     {         va_list args2;         va_copy(args2, args);         int res = vsnprintf(v.data(), v.size(), fmt, args2);         if ((res &gt;= 0) &amp;&amp; (res &lt; static_cast&lt;int&gt;(v.size())))         {             va_end(args);             va_end(args2);             return std::string(v.data());         }         size_t size;         if (res &lt; 0)             size = v.size() * 2;         else             size = static_cast&lt;size_t&gt;(res) + 1;         v.clear();         v.resize(size);     } }<\/code><\/pre>\n<p>  <\/p>\n<pre><code class=\"cpp\">std::string str = format(&quot;I have %d apples and %d oranges, so I have %d fruits&quot;, apples, oranges, apples + oranges);<\/code><\/pre>\n<p>  <\/p>\n<p>\u0417\u0434\u0435\u0441\u044c \u0441\u0442\u043e\u0438\u0442 \u0440\u0430\u0437\u044a\u044f\u0441\u043d\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043d\u044e\u0430\u043d\u0441\u043e\u0432. \u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 Xprintf \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u044b, \u043d\u0430 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0430\u0445, \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0435\u0443\u0441\u043f\u0435\u0445\u0430, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f -1, \u0432 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043c\u044b \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u043c \u0431\u0443\u0444\u0435\u0440 \u0432 \u0434\u0432\u0430 \u0440\u0430\u0437\u0430. \u041d\u0430 \u0434\u0440\u0443\u0433\u0438\u0445 \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0430\u0445 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0434\u043b\u0438\u043d\u0430 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0438\u0440\u0443\u044e\u0449\u0435\u0439 \u0441\u0442\u0440\u043e\u043a\u0438 (\u0431\u0435\u0437 \u0443\u0447\u0435\u0442\u0430 \u043d\u0443\u043b\u0435\u0432\u043e\u0433\u043e \u0441\u0438\u043c\u0432\u043e\u043b\u0430), \u0432 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043c\u044b \u0441\u0440\u0430\u0437\u0443 \u043c\u043e\u0436\u0435\u043c \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u0441\u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0430\u043c\u044f\u0442\u0438, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e. \u0411\u043e\u043b\u0435\u0435 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u043e \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 Xprintf \u043d\u0430 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0430\u0445 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0447\u0438\u0442\u0430\u0442\u044c <a href=\"http:\/\/demin.ws\/blog\/russian\/2013\/01\/28\/use-snprintf-on-different-platforms\/\">\u0437\u0434\u0435\u0441\u044c<\/a>. \u0422\u0430\u043a\u0436\u0435, \u043d\u0430 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0430\u0445, vsnprintf() &quot;\u043f\u043e\u0440\u0442\u0438\u0442&quot; \u0441\u043f\u0438\u0441\u043e\u043a \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043a\u043e\u043f\u0438\u0440\u0443\u0435\u043c \u0435\u0433\u043e \u043f\u0435\u0440\u0435\u0434 \u0432\u044b\u0437\u043e\u0432\u043e\u043c.<\/p>\n<p>  <\/p>\n<p>\u042f \u043d\u0430\u0447\u0430\u043b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0435\u0449\u0435 \u0434\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u044f C++11 \u0438 \u0441 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u043c\u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f\u043c\u0438 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u044e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u043e \u0441\u0435\u0433\u043e\u0434\u043d\u044f\u0448\u043d\u0438\u0439 \u0434\u0435\u043d\u044c. \u041e\u0441\u043d\u043e\u0432\u043d\u043e\u0435 \u043d\u0435\u0443\u0434\u043e\u0431\u0441\u0442\u0432\u043e \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u2014 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438 std::string \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0443\u0436\u043d\u043e \u043d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0442\u044c \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c .c_str() \u043a\u043e \u0432\u0441\u0435\u043c \u0441\u0442\u0440\u043e\u043a\u043e\u0432\u044b\u043c \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430\u043c:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">std::string country = &quot;Great Britain&quot;; std::string capital = &quot;London&quot;; std::cout &lt;&lt; format(&quot;%s is a capital of %s&quot;, capital.c_str(), country.c_str()) &lt;&lt; std::endl;<\/code><\/pre>\n<p>  <\/p>\n<h3 id=\"shablon-s-peremennym-kolichestvom-argumentov-variadic-template\">\u0428\u0430\u0431\u043b\u043e\u043d \u0441 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 (Variadic Template)<\/h3>\n<p>  <\/p>\n<p>\u0412 C++ \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 C++11 \u043f\u043e\u044f\u0432\u0438\u043b\u0430\u0441\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0448\u0430\u0431\u043b\u043e\u043d\u044b \u0441 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 (Variadic Templates). <\/p>\n<p>  <\/p>\n<p>\u0422\u0430\u043a\u0438\u0435 \u0448\u0430\u0431\u043b\u043e\u043d\u044b \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0435 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f. \u0422\u0430\u043a\u0436\u0435, \u043d\u0430\u043c \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u0431\u043e\u0442\u0438\u0442\u044c\u0441\u044f \u043e \u0442\u0438\u043f\u0430\u0445 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432, \u0442\u0430\u043a \u043a\u0430\u043a \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0448\u0430\u0431\u043b\u043e\u043d\u043d\u0443\u044e to_string, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u044b\u043b\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u0430 \u0440\u0430\u043d\u0435\u0435. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0440\u044f\u0434\u043a\u043e\u0432\u044b\u0435 \u043c\u0435\u0441\u0442\u043e\u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u0438.<\/p>\n<p>  <\/p>\n<p>\u0414\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u0445 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u043e\u0442\u0434\u0435\u043b\u044f\u0435\u043c \u043f\u0435\u0440\u0432\u044b\u0439 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442, \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u043c \u0435\u0433\u043e \u0432 \u0441\u0442\u0440\u043e\u043a\u0443, \u0437\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u0435\u043c \u0438 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u043c \u044d\u0442\u0443 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e. \u0412 \u0441\u043b\u0443\u0447\u0430\u0435 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u044f \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0438\u043b\u0438 \u043f\u0440\u0438 \u0438\u0445 \u043e\u043a\u043e\u043d\u0447\u0430\u043d\u0438\u0438 (\u043a\u043e\u043d\u0435\u0447\u043d\u0430\u044f \u0442\u043e\u0447\u043a\u0430 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438) \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u0440\u0430\u0437\u0431\u043e\u0440 \u0441\u0442\u0440\u043e\u043a\u0438-\u0448\u0430\u0431\u043b\u043e\u043d\u0430, \u043f\u043e\u0434\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0443 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0438 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0438\u0440\u0443\u044e\u0449\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443.<\/p>\n<p>  <\/p>\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0432\u0441\u0435, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f: \u043f\u0430\u0440\u0441\u0438\u043d\u0433 \u0441\u0442\u0440\u043e\u043a\u0438-\u0448\u0430\u0431\u043b\u043e\u043d\u0430, \u0441\u0431\u043e\u0440 \u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0432 \u0441\u0442\u0440\u043e\u043a\u0443 \u0432\u0441\u0435\u0445 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432, \u043f\u043e\u0434\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0443 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0432 \u0441\u0442\u0440\u043e\u043a\u0443-\u0448\u0430\u0431\u043b\u043e\u043d \u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0438\u0440\u0443\u044e\u0449\u0435\u0439 \u0441\u0442\u0440\u043e\u043a\u0438:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">std::string vtformat_impl(const std::string &amp;fmt, const std::vector&lt;std::string&gt; &amp;strs) {     static const char FORMAT_SYMBOL = '%';     std::string res;     std::string buf;     bool arg = false;     for (int i = 0; i &lt;= static_cast&lt;int&gt;(fmt.size()); ++i)     {         bool last = i == static_cast&lt;int&gt;(fmt.size());         char ch = fmt[i];         if (arg)         {             if (ch &gt;= '0' &amp;&amp; ch &lt;= '9')             {                 buf += ch;             }             else             {                 int num = 0;                 if (!buf.empty() &amp;&amp; buf.length() &lt; 10)                     num = atoi(buf.c_str());                 if (num &gt;= 1 &amp;&amp; num &lt;= static_cast&lt;int&gt;(strs.size()))                     res += strs[num - 1];                 else                     res += FORMAT_SYMBOL + buf;                 buf.clear();                 if (ch != FORMAT_SYMBOL)                 {                     if (!last)                         res += ch;                     arg = false;                 }             }         }         else         {             if (ch == FORMAT_SYMBOL)             {                 arg = true;             }             else             {                 if (!last)                     res += ch;             }         }     }     return res; }  template&lt;typename Arg, typename ... Args&gt; std::string vtformat_impl(const std::string &amp;fmt, std::vector&lt;std::string&gt; &amp;strs, Arg arg, Args ... args) {     strs.push_back(to_string(arg));     return vtformat_impl(fmt, strs, args ...); }  std::string vtformat(const std::string &amp;fmt) {     return fmt; }  template&lt;typename Arg, typename ... Args&gt; std::string vtformat(const std::string &amp;fmt, Arg arg, Args ... args) {     std::vector&lt;std::string&gt; strs;     return vtformat_impl(fmt, strs, arg, args ...); }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0441\u044f \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u044b\u043c, \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0437\u0430 \u043e\u0434\u0438\u043d \u043f\u0440\u043e\u0445\u043e\u0434 \u043f\u043e \u0441\u0442\u0440\u043e\u043a\u0435 \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f. \u0412 \u0441\u043b\u0443\u0447\u0430\u0435, \u0435\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u043e \u043c\u0435\u0441\u0442\u043e\u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044f \u043d\u0435 \u0443\u0434\u0430\u0435\u0442\u0441\u044f \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442, \u043e\u043d \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u0431\u0435\u0437 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439, \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0439 \u043d\u0435 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442\u0441\u044f.<\/p>\n<p>  <\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f:<\/p>\n<p>  <\/p>\n<pre><code class=\"cpp\">std::cout &lt;&lt; vtformat(&quot;I have %1 apples and %2 oranges, so I have %3 fruits&quot;, apples, oranges, apples + oranges) &lt;&lt; std::endl; I have 5 apples and 7 oranges, so I have 12 fruits  std::cout &lt;&lt; vtformat(&quot;%1 + %2 = %3&quot;, 2, 3, 2 + 3) &lt;&lt; std::endl; 2 + 3 = 5  std::cout &lt;&lt; vtformat(&quot;%3 = %2 + %1&quot;, 2, 3, 2 + 3) &lt;&lt; std::endl; 5 = 3 + 2  std::cout &lt;&lt; vtformat(&quot;%2 = %1 + %1 + %1&quot;, 2, 2 + 2 + 2) &lt;&lt; std::endl; 6 = 2 + 2 + 2  std::cout &lt;&lt; vtformat(&quot;%0 %1 %2 %3 %4 %5&quot;, 1, 2, 3, 4) &lt;&lt; std::endl; %0 1 2 3 4 %5  std::cout &lt;&lt; vtformat(&quot;%1 + 1% = %2&quot;, 54, 54 * 1.01) &lt;&lt; std::endl; 54 + 1% = 54.540000  std::string country = &quot;Russia&quot;; const char *capital = &quot;Moscow&quot;; std::cout &lt;&lt; vtformat(&quot;%1 is a capital of %2&quot;, capital, country) &lt;&lt; std::endl; Moscow is a capital of Russia  template&lt;typename T&gt; std::ostream &amp;operator&lt;&lt;(std::ostream &amp;os, const std::vector&lt;T&gt; &amp;v) {     os &lt;&lt; &quot;[&quot;;     bool first = true;     for (const auto &amp;x : v)     {         if (first)             first = false;         else             os &lt;&lt; &quot;, &quot;;         os &lt;&lt; x;     }     os &lt;&lt; &quot;]&quot;;     return os; } std::vector&lt;int&gt; v = {1, 4, 5, 2, 7, 9}; std::cout &lt;&lt; vtformat(&quot;v = %1&quot;, v) &lt;&lt; std::endl; v = [1, 4, 5, 2, 7, 9]<\/code><\/pre>\n<p>  <\/p>\n<h3 id=\"sravnenie-proizvoditelnosti\">\u0421\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<\/h3>\n<p>  <\/p>\n<p>\u0421\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 to_string \u0438 std::to_string, \u043c\u0438\u043b\u043b\u0438\u0441\u0435\u043a\u0443\u043d\u0434 \u043d\u0430 \u043c\u0438\u043b\u043b\u0438\u043e\u043d \u0432\u044b\u0437\u043e\u0432\u043e\u0432 <\/p>\n<p>  <\/p>\n<table>\n<thead>\n<tr>\n<th><\/th>\n<th>int, \u043c\u0441<\/th>\n<th>long long, \u043c\u0441<\/th>\n<th>double, \u043c\u0441<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>to_string<\/td>\n<td>681<\/td>\n<td>704<\/td>\n<td>1109<\/td>\n<\/tr>\n<tr>\n<td>std::to_string<\/td>\n<td>130<\/td>\n<td>201<\/td>\n<td>291<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>  <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/8e5\/de2\/8be\/8e5de28be1774cd2a53b7992bfbf389d.png\" alt=\"image\"\/><\/p>\n<p>  <\/p>\n<p>\u0421\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 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u043c\u0438\u043b\u043b\u0438\u0441\u0435\u043a\u0443\u043d\u0434 \u043d\u0430 \u043c\u0438\u043b\u043b\u0438\u043e\u043d \u0432\u044b\u0437\u043e\u0432\u043e\u0432<\/p>\n<p>  <\/p>\n<table>\n<thead>\n<tr>\n<th><\/th>\n<th>\u043c\u0441<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>fstr<\/td>\n<td>1308<\/td>\n<\/tr>\n<tr>\n<td>sstr<\/td>\n<td>1243<\/td>\n<\/tr>\n<tr>\n<td>format<\/td>\n<td>788<\/td>\n<\/tr>\n<tr>\n<td>boost::format<\/td>\n<td>2554<\/td>\n<\/tr>\n<tr>\n<td>vtformat<\/td>\n<td>2022<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>  <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/321\/694\/748\/321694748ba14caf89e7ea9b5e59019d.png\" alt=\"image\"\/><\/p>\n<p>  <\/p>\n<p>\u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0437\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435.<br \/>  \u0417\u0430\u043c\u0435\u0447\u0430\u043d\u0438\u044f \u0438 \u0434\u043e\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043f\u0440\u0438\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442\u0441\u044f.<\/p>\n<p> \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:\/\/habrahabr.ru\/post\/318962\/\"> https:\/\/habrahabr.ru\/post\/318962\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u0414\u043e\u0431\u0440\u043e\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0441\u0443\u0442\u043e\u043a!<\/p>\n<p>  <\/p>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u0445\u043e\u0442\u0435\u043b \u0431\u044b \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c \u043e \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044f\u0445 \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0441\u0442\u0440\u043e\u043a \u0432 \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u043c C++, \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u0441\u0432\u043e\u0438 \u043d\u0430\u0440\u0430\u0431\u043e\u0442\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u044f \u0443\u0436\u0435 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043b\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e \u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0441\u0440\u0430\u0432\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u043f\u043e\u0434\u0445\u043e\u0434\u043e\u0432 \u043a \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044e \u0441\u0442\u0440\u043e\u043a.<\/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-283457","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/283457","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=283457"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/283457\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=283457"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=283457"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=283457"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}