{"id":2221,"date":"2021-11-02T13:52:10","date_gmt":"2021-11-02T13:52:10","guid":{"rendered":"https:\/\/blog.embold.io\/?p=2221"},"modified":"2021-11-03T06:31:18","modified_gmt":"2021-11-03T06:31:18","slug":"optimizing-the-use-of-std-vector","status":"publish","type":"post","link":"https:\/\/blog.embold.io\/fr\/optimizing-the-use-of-std-vector\/","title":{"rendered":"Optimizing the use of std vector"},"content":{"rendered":"\n<h1>Overview<\/h1>\n\n\n\n<p>Optimizing in C++ means knowing your environment. One should know how vector works, and vector works with pushing back elements into an array.&nbsp;<\/p>\n\n\n\n<h1>Advantages of <code>std::vector<\/code><\/h1>\n\n\n\n<p>Dynamic arrays in C++ are implemented using <code>std::vector<\/code>.<\/p>\n\n\n\n<p>When a new element is inserted, and the existing capacity of the vector is exhausted, the <code>std::vector<\/code> resizes itself. The way it does that is by allocating memory at a different location that is large enough to fit the element we are inserting as well as the existing items. Then it copies all the existing items into the new location and adds the element we are trying to insert.<\/p>\n\n\n\n<p>The standard vector resizes itself very frequently when elements get pushed into it. It results in multiple memory reallocations and slower, inefficient code. To avoid resizing and copying, we can use the <code>reserve <\/code>function. If we know how many elements the vector will hold, we can reserve that size at the beginning. <code>Reserve <\/code>will make sure that we have enough memory. Thus, multiple memory reallocations will be avoided whenever new elements are inserted into the vector.<\/p>\n\n\n\n<h1>How is this feature integrated with Embold? <\/h1>\n\n\n\n<p>Embold has integrated a checker in C++.&nbsp;<\/p>\n\n\n\n<ul><li><strong><code>performance-inefficient-vector-operation<\/code><\/strong>: It finds possible inefficient <code>std::vector<\/code> operations (e.g.: <strong><code>push_back<\/code><\/strong>) that may cause unnecessary memory reallocations.<\/li><\/ul>\n\n\n\n<h1>Example:&nbsp;<\/h1>\n\n\n\n<p>              <code>std::vector v;<br>         for (int i = 0; i &lt; n; ++i) {<br>           v.push_back(n);<br>             }<\/code><\/p>\n\n\n\n<h2>An explanation for the above example<\/h2>\n\n\n\n<p>The <code>push_back<\/code> function may cause multiple memory reallocations in v. Avoid by inserting a &#8216;<code>reserve(n)<\/code>&#8216; statement before the &#8216;for&#8217; statement.<\/p>\n\n\n\n<p><code>std::vector&lt;int&gt; v;<br>         v.reserve(n); <br>           for (int i = 0; i &lt; n; ++i) { <br>             v.push_back(n); <br>              }<\/code><\/p>\n\n\n\n<h1>Conclusion<\/h1>\n\n\n\n<p>We are spending too much time on reallocating and deallocating memory. However, by using the C++ function <code>std::vector::reserve<\/code>, it helps to reserve the vector capacity that may contain at least n elements.  <\/p>\n\n\n\n<p>Also, it is recommended the usage of <code>std::vector<\/code> in most cases where dynamic arrays are needed.&nbsp; And this optimizing by using a standard vector will lead to easily resizing the length. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Overview Optimizing in C++ means knowing your environment. One should know how vector works, and vector works with pushing back elements into an array.&nbsp; Advantages of std::vector Dynamic arrays in&hellip;<\/p>\n","protected":false},"author":10,"featured_media":2226,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[224,221,222,216,218,225,220,223,219,217,215],"better_featured_image":{"id":2226,"alt_text":"","caption":"","description":"","media_type":"image","media_details":{"width":1280,"height":853,"file":"2021\/11\/Optimizing-the-use-of-std-vector.jpg","sizes":{"thumbnail":{"file":"Optimizing-the-use-of-std-vector-150x150.jpg","width":150,"height":150,"mime-type":"image\/jpeg","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/11\/Optimizing-the-use-of-std-vector-150x150.jpg"},"medium_large":{"file":"Optimizing-the-use-of-std-vector-768x512.jpg","width":768,"height":512,"mime-type":"image\/jpeg","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/11\/Optimizing-the-use-of-std-vector-768x512.jpg"},"penci-full-thumb":{"file":"Optimizing-the-use-of-std-vector-1170x780.jpg","width":1170,"height":780,"mime-type":"image\/jpeg","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/11\/Optimizing-the-use-of-std-vector-1170x780.jpg"},"penci-slider-thumb":{"file":"Optimizing-the-use-of-std-vector-1170x663.jpg","width":1170,"height":663,"mime-type":"image\/jpeg","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/11\/Optimizing-the-use-of-std-vector-1170x663.jpg"},"penci-magazine-slider":{"file":"Optimizing-the-use-of-std-vector-780x516.jpg","width":780,"height":516,"mime-type":"image\/jpeg","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/11\/Optimizing-the-use-of-std-vector-780x516.jpg"},"penci-slider-full-thumb":{"file":"Optimizing-the-use-of-std-vector-1280x800.jpg","width":1280,"height":800,"mime-type":"image\/jpeg","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/11\/Optimizing-the-use-of-std-vector-1280x800.jpg"},"penci-thumb":{"file":"Optimizing-the-use-of-std-vector-585x390.jpg","width":585,"height":390,"mime-type":"image\/jpeg","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/11\/Optimizing-the-use-of-std-vector-585x390.jpg"},"penci-masonry-thumb":{"file":"Optimizing-the-use-of-std-vector-585x390.jpg","width":585,"height":390,"mime-type":"image\/jpeg","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/11\/Optimizing-the-use-of-std-vector-585x390.jpg"},"penci-thumb-square":{"file":"Optimizing-the-use-of-std-vector-585x585.jpg","width":585,"height":585,"mime-type":"image\/jpeg","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/11\/Optimizing-the-use-of-std-vector-585x585.jpg"},"penci-thumb-vertical":{"file":"Optimizing-the-use-of-std-vector-480x650.jpg","width":480,"height":650,"mime-type":"image\/jpeg","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/11\/Optimizing-the-use-of-std-vector-480x650.jpg"},"penci-thumb-small":{"file":"Optimizing-the-use-of-std-vector-263x175.jpg","width":263,"height":175,"mime-type":"image\/jpeg","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/11\/Optimizing-the-use-of-std-vector-263x175.jpg"},"jr_insta_square":{"file":"Optimizing-the-use-of-std-vector-640x640.jpg","width":640,"height":640,"mime-type":"image\/jpeg","source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/11\/Optimizing-the-use-of-std-vector-640x640.jpg"}},"image_meta":{"aperture":"0","credit":"","camera":"","caption":"","created_timestamp":"0","copyright":"","focal_length":"0","iso":"0","shutter_speed":"0","title":"","orientation":"0","keywords":[]}},"post":2221,"source_url":"https:\/\/blog.embold.io\/wp-content\/uploads\/sites\/2\/2021\/11\/Optimizing-the-use-of-std-vector.jpg"},"translation":{"provider":"WPGlobus","version":"2.10.8","language":"fr","enabled_languages":["en","es","de","fr","ru"],"languages":{"en":{"title":true,"content":true,"excerpt":false},"es":{"title":false,"content":false,"excerpt":false},"de":{"title":false,"content":false,"excerpt":false},"fr":{"title":false,"content":false,"excerpt":false},"ru":{"title":false,"content":false,"excerpt":false}}},"_links":{"self":[{"href":"https:\/\/blog.embold.io\/fr\/wp-json\/wp\/v2\/posts\/2221"}],"collection":[{"href":"https:\/\/blog.embold.io\/fr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.embold.io\/fr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.embold.io\/fr\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.embold.io\/fr\/wp-json\/wp\/v2\/comments?post=2221"}],"version-history":[{"count":12,"href":"https:\/\/blog.embold.io\/fr\/wp-json\/wp\/v2\/posts\/2221\/revisions"}],"predecessor-version":[{"id":2235,"href":"https:\/\/blog.embold.io\/fr\/wp-json\/wp\/v2\/posts\/2221\/revisions\/2235"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.embold.io\/fr\/wp-json\/wp\/v2\/media\/2226"}],"wp:attachment":[{"href":"https:\/\/blog.embold.io\/fr\/wp-json\/wp\/v2\/media?parent=2221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embold.io\/fr\/wp-json\/wp\/v2\/categories?post=2221"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embold.io\/fr\/wp-json\/wp\/v2\/tags?post=2221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}