"Erase usage in STL string"

Source: Internet
Author: User

http://blog.csdn.net/leolin_/article/details/7315975

The erase function is prototyped as follows:
(1) string& erase (size_t pos = 0, size_t n = NPOs);
(2) Iterator erase (iterator position);
(3) Iterator erase (iterator first, iterator last);
That means there are three ways to use:
(1) Erase (pos,n); Deleting n characters starting from POS, such as erase (0,1), deletes the first character
(2) Erase (position); Deletes a character at position (position is a string type of iterator)
(3) Erase (first,last); Delete the characters from first to last (both first and last are iterators)
Let me give you an example:
[CPP] View plain copy// string::erase   #include  <iostream>   #include  < string>   using namespace std;      int main  ()    {      string str  ("this is an example phrase.");      string::iterator it;        //  (1) Usage       str.erase  (10,8);     cout << str <<  endl;        //  "This is an phrase."         //  Section (2) Usage      it=str.begin () +9;      str.erase  (IT);     cout << str << endl;         //  "This is a phrase."         //  (3) typeLaw      str.erase  (Str.begin () +5, str.end () -7);     cout  << str << endl;        //  "this  Phrase. "      return 0;  }  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.