C++ 字串庫 - erase



描述

它擦除字串的一部分,縮短其長度。

宣告

以下是 std::string::erase 的宣告。

string& insert (size_t pos, const string& str);

C++11

 string& erase (size_t pos = 0, size_t len = npos);

C++14

string& erase (size_t pos = 0, size_t len = npos);

引數

  • pos − 它是一個插入點。

  • str − 它是一個字串物件。

  • len − 它包含要擦除的字元數的資訊。

返回值

它返回 *this。

異常

如果丟擲異常,字串不會發生任何更改。

示例

在下面的 std::string::erase 示例中。

#include <iostream>
#include <string>

int main () {
   std::string str ("sairamkrishna Mammahe is a one of the tech person.");
   std::cout << str << '\n';

   str.erase (10,8);
   std::cout << str << '\n';

   str.erase (str.begin()+9);
   std::cout << str << '\n';

   str.erase (str.begin()+5, str.end()-9);
   std::cout << str << '\n';

   return 0;
}

示例輸出應如下所示:

sairamkrishna Mammahe is a one of the tech person.
sairamkrisahe is a one of the tech person.
sairamkriahe is a one of the tech person.
sairah person.
string.htm
廣告