c++ 使用擦除成员函数来截断字符串

From , 4 Years ago, written in C++, viewed 52 times.
URL https://pastebin.vip/view/eb1e7832
  1. //使用擦除成员函数来截断字符串
  2. #include <string>
  3. #include <algorithm>
  4. #include <iostream>
  5.  
  6. int main() {
  7.         using namespace std;
  8.  
  9.         string strSample("Hello String! Wake up to a beautiful day!");
  10.         cout << "The original sample string is: " << endl;
  11.         cout << strSample << endl << endl;
  12.  
  13.         // Delete characters from the string given position and count
  14.         cout << "Truncating the second sentence: " << endl;
  15.         strSample.erase(13, 28);
  16.         cout << strSample << endl << endl;
  17.  
  18.         // Find a character 'S' in the string using STL find algorithm
  19.         string::iterator iCharS = find(strSample.begin(), strSample.end(), 'S');
  20.  
  21.         // If character found, 'erase' to deletes a character
  22.         cout << "Erasing character 'S' from the sample string:" << endl;
  23.         if (iCharS != strSample.end())
  24.                 strSample.erase(iCharS);
  25.  
  26.         cout << strSample << endl << endl;
  27.  
  28.         // Erase a range of characters using an overloaded version of erase()
  29.         cout << "Erasing a range between begin() and end(): " << endl;
  30.         strSample.erase(strSample.begin(), strSample.end());
  31.  
  32.         // Verify the length after the erase() operation above
  33.         if (strSample.length() == 0)
  34.                 cout << "The string is empty" << endl;
  35.  
  36.         return 0;
  37. }
  38.  

Reply to "c++ 使用擦除成员函数来截断字符串"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website