1. 程式人生 > 其它 >關於string你可能不知道的一些用法

關於string你可能不知道的一些用法

技術標籤:程式設計

今天我們來康康string的一些奇妙用法:

  1. a.erase(x,y) 這個函式是用來刪除字串,x是指刪除的起始下標,y是刪除多少個
  2. a.insert(x,“y”) 它是用來新增字串,x是指起始下標,y是你想要新增的字串
  3. a.replace(x,y,“k”) 替換函式可以說是包含了上面兩個函式,x是起始下標,y是刪除多少個字元,k是你想要新增的字串

示範程式碼:

#include <iostream>
#include <cstring>
#include <string>
#include <fstream>
#include
<algorithm>
#include <queue> #include <stack> #include <iomanip> #include <math.h> #include <cmath> #include <ctime> #include <cstdio> #include <map> #define inf 0x7ffffff #define ll long long using namespace std; string a="abcde"; int main
() { a.erase(2,2); a.insert(2,"cd"); a.replace(2,2,"xxx"); return 0; }