字串是一種常用的資料型別,現在我們將其常用的功能用**實現如下:
class string
; len = 0;
}/*********************************拷貝構造*********************************/
string(const char* s)
/*********************************拷貝構造*********************************/
string(char c, int n) ;
memset(ptr, c, n);
} //const string& 拷貝建構函式的原型,
//1,引用可以改變實參,不應該改變實參的內容
//2,const & 可以引用右值(匿名變數)
/*********************************拷貝構造*********************************/
string(const string& s) ;
memcpy(ptr, s.ptr, len);
} /*********************************字串長度*********************************/
int size()
/*********************************判斷字串是否為空*********************************/
bool empty()
/*********************************字串首位址*********************************/
const char* c_str()
/*********************************字串長度重置*********************************/
void resize(int n) ;
memcpy(p, ptr, n);
delete ptr;
ptr = p;
len = n;
} // ptr = "hello"
// hlo
// p = "000"
// 1, 2
/*********************************字串刪除*********************************/
void erase(int pos, int n)
if (n < 0)
n = n > len - pos ? len - pos : n;
char* p = new char[len-n+1]{};
memcpy(p, ptr, pos);
memcpy(p+pos, ptr+pos+n, len-pos-n);
delete ptr;
ptr = p;
len = len - n;
} /*********************************字串頭插入*********************************/
void insert(int pos, const char* s)
// "he\0llo"
/*********************************字串任意插入*********************************/
void insert(int pos, const string& s)
/* pos+n
| |// ptr = "hello", s = "world"
--// 1, 2
// ptr = "hworldlo";
-----
*//*********************************字串任意插入*********************************/
void replace(int pos, int n, const string& s)
if (n < 0)
n = n > len - pos ? len - pos : n;
int sl = s.len; //string型別的長度不能用strlen測量
char* p = new char[len+sl-n+1]{};
memcpy(p, ptr, pos);
memcpy(p+pos, s.ptr, sl);
memcpy(p+pos+sl, ptr+pos+n, len-pos-n);
len = sl + len - n;
delete ptr;
ptr = p;
} /*********************************字串任意替換*********************************/
void replace(int pos, int n, const char* s)
if (n < 0)
n = n > len - pos ? len - pos : n;
int sl = strlen(s);
char* p = new char[len+sl-n+1]{};
memcpy(p, ptr, pos);
memcpy(p+pos, s, sl);
memcpy(p+pos+sl, ptr+pos+n, len-pos-n);
len = sl + len - n;
delete ptr;
ptr = p;
} /*********************************字串查詢*********************************/
string substr(int pos, int n)
if (n < 0)
n = n > len - pos ? len - pos : n;
char* p = new char[n+1];
memcpy(p, ptr+pos, n);
string t;
delete t.ptr;
t.ptr = p;
t.len = n;
return t;
} void show()
string show(string s)
//析構函式
~string()
};
C C 面試常見題目之字串操作(一)
1,memcpy memmove函式以及二者的區別 原型 extern void memcpy void dest,void src,unsigned int count 用法 include 功能 由src所指記憶體區域複製count個位元組到dest所指記憶體區域。注意 source和desti...
c c 面試45 50之字串
45 使用庫函式將數字轉換為字串,下面是常用庫函式 1 itoa 將整型轉換為字串 2 ltoa 將長整形轉換為字串 3 gcvt 將浮點轉換為字串 46 不使用庫函式將整數轉換為字串 通過把整數的各位上的數字加上 0 轉換為char型別並存到字元陣列 1 1 include 2 using nam...
C C 題目 字元與字串
char a 4 char b 4 答案 abcd efg 分析 由於字串是以 0 為結尾的,所以陣列a不是乙個字串。陣列b列印直到空字元為止有7個字元顯示。而陣列a讀取了 d 字元之後,便在其隨後的位元組繼續解釋要列印的字元,直到遇到空字元為止。const int arsize 5 char na...