{"id":13560,"date":"2024-03-15T07:22:36","date_gmt":"2024-03-15T07:22:36","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/what-does-the-erase-function-in-cs-vector-do\/"},"modified":"2025-08-05T20:14:24","modified_gmt":"2025-08-05T20:14:24","slug":"what-does-the-erase-function-in-cs-vector-do","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/what-does-the-erase-function-in-cs-vector-do\/","title":{"rendered":"C++ Vector erase() Function Guide"},"content":{"rendered":"<p>The erase function in C++ vectors is used to remove one or more elements from the vector. It removes the element at a specified position or a specified range of elements from the vector and updates the size of the vector.<\/p>\n<p>There are two ways to use the erase function.<\/p>\n<ol>\n<li>Remove a single element: pass an iterator parameter pointing to the position of the element to be deleted, the function deletes the element, and returns an iterator pointing to the next element after the deleted element.<\/li>\n<li>Remove multiple elements: pass two iterator parameters specifying the range of elements to be deleted, the function removes all elements within that range, and returns an iterator pointing to the element after the deleted ones.<\/li>\n<\/ol>\n<p>For example, the following code demonstrates the usage of the erase function:<\/p>\n<pre class=\"post-pre\"><code><span class=\"hljs-meta\">#<span class=\"hljs-keyword\">include<\/span> <span class=\"hljs-string\">&lt;iostream&gt;<\/span><\/span>\r\n<span class=\"hljs-meta\">#<span class=\"hljs-keyword\">include<\/span> <span class=\"hljs-string\">&lt;vector&gt;<\/span><\/span>\r\n\r\n<span class=\"hljs-function\"><span class=\"hljs-type\">int<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\r\n    std::vector&lt;<span class=\"hljs-type\">int<\/span>&gt; numbers = {<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>};\r\n\r\n    <span class=\"hljs-comment\">\/\/ \u5220\u9664\u5355\u4e2a\u5143\u7d20<\/span>\r\n    numbers.<span class=\"hljs-built_in\">erase<\/span>(numbers.<span class=\"hljs-built_in\">begin<\/span>() + <span class=\"hljs-number\">2<\/span>); <span class=\"hljs-comment\">\/\/ \u5220\u9664\u7d22\u5f15\u4e3a2\u7684\u5143\u7d20<\/span>\r\n\r\n    <span class=\"hljs-comment\">\/\/ \u5220\u9664\u591a\u4e2a\u5143\u7d20<\/span>\r\n    numbers.<span class=\"hljs-built_in\">erase<\/span>(numbers.<span class=\"hljs-built_in\">begin<\/span>(), numbers.<span class=\"hljs-built_in\">begin<\/span>() + <span class=\"hljs-number\">2<\/span>); <span class=\"hljs-comment\">\/\/ \u5220\u9664\u524d\u4e24\u4e2a\u5143\u7d20<\/span>\r\n\r\n    <span class=\"hljs-comment\">\/\/ \u8f93\u51fa\u5411\u91cf\u4e2d\u7684\u5143\u7d20<\/span>\r\n    <span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-type\">int<\/span> num : numbers) {\r\n        std::cout &lt;&lt; num &lt;&lt; <span class=\"hljs-string\">\" \"<\/span>;\r\n    }\r\n    std::cout &lt;&lt; std::endl;\r\n\r\n    <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-number\">0<\/span>;\r\n}\r\n<\/code><\/pre>\n<p>The output result is:<\/p>\n<pre class=\"post-pre\"><code>4 5\r\n<\/code><\/pre>\n<p>In the example above, the vector begins with elements 1, 2, 3, 4, 5. Initially, the erase function is used to remove the element at index 2, which is the number 3. Then the erase function is used again to delete the elements at index 0 and 1, which are the numbers 1 and 2. Finally, only the numbers 4 and 5 remain in the vector, which are then output.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The erase function in C++ vectors is used to remove one or more elements from the vector. It removes the element at a specified position or a specified range of elements from the vector and updates the size of the vector. There are two ways to use the erase function. Remove a single element: pass [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[3933,7317,7356,18081,18082],"class_list":["post-13560","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-c-iterators","tag-c-vector","tag-erase-function","tag-stl-tutorial","tag-vector-elements"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C++ Vector erase() Function Guide - Blog - Silicon Cloud<\/title>\n<meta name=\"description\" content=\"Learn C++ vector erase() usage: remove single\/multiple elements, iterator behavior &amp; examples. Complete STL vector tutorial.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/blog\/what-does-the-erase-function-in-cs-vector-do\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++ Vector erase() Function Guide\" \/>\n<meta property=\"og:description\" content=\"Learn C++ vector erase() usage: remove single\/multiple elements, iterator behavior &amp; examples. Complete STL vector tutorial.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/what-does-the-erase-function-in-cs-vector-do\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-15T07:22:36+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-08-05T20:14:24+00:00\" \/>\n<meta name=\"author\" content=\"Sophia Anderson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:site\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Sophia Anderson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/what-does-the-erase-function-in-cs-vector-do\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/what-does-the-erase-function-in-cs-vector-do\/\"},\"author\":{\"name\":\"Sophia Anderson\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/19a24313de9c988db3d69226b4a40a30\"},\"headline\":\"C++ Vector erase() Function Guide\",\"datePublished\":\"2024-03-15T07:22:36+00:00\",\"dateModified\":\"2025-08-05T20:14:24+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/what-does-the-erase-function-in-cs-vector-do\/\"},\"wordCount\":199,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"keywords\":[\"C# iterators\",\"C++ vector\",\"erase function\",\"STL tutorial\",\"vector elements\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/what-does-the-erase-function-in-cs-vector-do\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/what-does-the-erase-function-in-cs-vector-do\/\",\"name\":\"C++ Vector erase() Function Guide - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2024-03-15T07:22:36+00:00\",\"dateModified\":\"2025-08-05T20:14:24+00:00\",\"description\":\"Learn C++ vector erase() usage: remove single\/multiple elements, iterator behavior & examples. Complete STL vector tutorial.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/what-does-the-erase-function-in-cs-vector-do\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/what-does-the-erase-function-in-cs-vector-do\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/what-does-the-erase-function-in-cs-vector-do\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ Vector erase() Function Guide\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"name\":\"Silicon Cloud Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\",\"name\":\"Silicon Cloud Blog\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"contentUrl\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"width\":1024,\"height\":1024,\"caption\":\"Silicon Cloud Blog\"},\"image\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\",\"https:\/\/twitter.com\/SiliCloudGlobal\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/19a24313de9c988db3d69226b4a40a30\",\"name\":\"Sophia Anderson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c726c09aa40e37115fb5c62d0c3ed62c16ca255d3763e2e3ae83a70ddf8c2175?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c726c09aa40e37115fb5c62d0c3ed62c16ca255d3763e2e3ae83a70ddf8c2175?s=96&d=mm&r=g\",\"caption\":\"Sophia Anderson\"},\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/sophiaanderson\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"C++ Vector erase() Function Guide - Blog - Silicon Cloud","description":"Learn C++ vector erase() usage: remove single\/multiple elements, iterator behavior & examples. Complete STL vector tutorial.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/blog\/what-does-the-erase-function-in-cs-vector-do\/","og_locale":"en_US","og_type":"article","og_title":"C++ Vector erase() Function Guide","og_description":"Learn C++ vector erase() usage: remove single\/multiple elements, iterator behavior & examples. Complete STL vector tutorial.","og_url":"https:\/\/www.silicloud.com\/blog\/what-does-the-erase-function-in-cs-vector-do\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2024-03-15T07:22:36+00:00","article_modified_time":"2025-08-05T20:14:24+00:00","author":"Sophia Anderson","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"Sophia Anderson","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/what-does-the-erase-function-in-cs-vector-do\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/what-does-the-erase-function-in-cs-vector-do\/"},"author":{"name":"Sophia Anderson","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/19a24313de9c988db3d69226b4a40a30"},"headline":"C++ Vector erase() Function Guide","datePublished":"2024-03-15T07:22:36+00:00","dateModified":"2025-08-05T20:14:24+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/what-does-the-erase-function-in-cs-vector-do\/"},"wordCount":199,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"keywords":["C# iterators","C++ vector","erase function","STL tutorial","vector elements"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/what-does-the-erase-function-in-cs-vector-do\/","url":"https:\/\/www.silicloud.com\/blog\/what-does-the-erase-function-in-cs-vector-do\/","name":"C++ Vector erase() Function Guide - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2024-03-15T07:22:36+00:00","dateModified":"2025-08-05T20:14:24+00:00","description":"Learn C++ vector erase() usage: remove single\/multiple elements, iterator behavior & examples. Complete STL vector tutorial.","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/what-does-the-erase-function-in-cs-vector-do\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/what-does-the-erase-function-in-cs-vector-do\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/what-does-the-erase-function-in-cs-vector-do\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"C++ Vector erase() Function Guide"}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/blog\/#website","url":"https:\/\/www.silicloud.com\/blog\/","name":"Silicon Cloud Blog","description":"","publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.silicloud.com\/blog\/#organization","name":"Silicon Cloud Blog","url":"https:\/\/www.silicloud.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","contentUrl":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","width":1024,"height":1024,"caption":"Silicon Cloud Blog"},"image":{"@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/SiliCloudGlobal\/","https:\/\/twitter.com\/SiliCloudGlobal"]},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/19a24313de9c988db3d69226b4a40a30","name":"Sophia Anderson","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c726c09aa40e37115fb5c62d0c3ed62c16ca255d3763e2e3ae83a70ddf8c2175?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c726c09aa40e37115fb5c62d0c3ed62c16ca255d3763e2e3ae83a70ddf8c2175?s=96&d=mm&r=g","caption":"Sophia Anderson"},"url":"https:\/\/www.silicloud.com\/blog\/author\/sophiaanderson\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/13560","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=13560"}],"version-history":[{"count":2,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/13560\/revisions"}],"predecessor-version":[{"id":157553,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/13560\/revisions\/157553"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=13560"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=13560"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=13560"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}