關於字串 2

2021-07-06 04:07:31 字數 4900 閱讀 1589

上節我們講了字元陣列,這是c處理字元的方法, 被c++保留了下來。c++為我們提供了一種新的資料型別:字串型別。注意,這裡面string並不是c++提供的基本資料型別(基本資料型別有:int,char,float,double等),而是乙個字串類。在用之前,包含的標頭檔案為:
#include
和其他變數一樣,字串先定義後使用,用類名string。
string string1;

string string2="hello";

string1="china";

注意:在字元陣列中是不可以這麼做的,比如char str[10]; str="hello"這種寫法是錯誤的。因為str是陣列名而不是變數名。

string類還可以直接用乙個字串給另乙個字串賦值。

string string1="canada";

string string2="china"

string2=string1;

賦值結束後string2從長度為5變成長度為6的「canada」。

注意:在之前講的字元陣列中,是要用strcpy函式來進行這一操作的。

除此之外,還可以對字串變數的某一字元操作,如:

word="then";

word[2]='a';

執行結束後word就變成了「than」。

可以直接在輸入輸出用語句中用字串變數名進行輸入輸出。
cin>>string1;

cout

string1="c++ "

;string

string2="language"

;string1=string1+string2;

最後string1的內容是「c++ language」.

注意:在字元陣列中,是用 strcat(char,const char) 函式的。

3.字串比較直接用關係運算子
可以直接用 ==,>,<,!=,>=,<=來對字串進行比較。這是因為string類對這些運算子進行了過載。

注意:在字元陣列中是要用strcmp(const char,const char)函式的。

1.建構函式

tring(const char *s); //用c字串s初始化

string(int n,char c); //用n個字元c初始化也可以像之前講的一樣初始化。

2.string類的字元操作:

const

char &operator(int n)const;

const

char &at(int n)const;

char &operator(int n);

char &at(int n);

operator和at()均返回當前字串中第n個字元的位置,但at函式提供範圍檢    查,當越界時會丟擲out_of_range異常,下標運算子不提供檢查訪問。
const

char *data()const;//返回乙個非null終止的c字元陣列

const

char *c_str()const;//返回乙個以null終止的c字串

intcopy(char *s, int n, int pos = 0) const;//把當前串中以pos開始的n個字元拷貝到以s為起始位置的字元陣列中,返回實際拷貝的數目

3.string的特性描述:

int capacity()const;    //返回當前容量(即string中不必增加記憶體即可存放的元素個數)

int max_size()const; //返回string物件中可存放的最大字串的長度

int size()const; //返回當前字串的大小

int length()const; //返回當前字串的長度(大小是記憶體,長度是個數)

bool empty()const; //當前字串是否為空

void resize(int len,char c);//把字串當前大小置為len,並用字元c填充不足的部分

4.string類的輸入輸出操作:

string類過載運算子operator>>用於輸入,同樣過載運算子operator

《用於輸出操作。

函式getline(istream &in,string &s);用於從輸入流in中讀取字串到s中,以換行符'\n'分開。

5.string的賦值:

string &operator=(const

string &s);//把字串s賦給當前字串

string &assign(const

char *s);//用c型別字串s賦值

string &assign(const

char *s,int n);//用c字串s開始的n個字元賦值

string &assign(const

string &s);//把字串s賦給當前字串

string &assign(int n,char c);//用n個字元c賦值給當前字串

string &assign(const

string &s,int start,int n);//把字串s中從start開始的n個字元賦給當前字串

string &assign(const_iterator first,const_itertor last);//把first和last迭代器之間的部分賦給字串

6.string的連線:

string &operator+=(const

string &s);//把字串s連線到當前字串的結尾

string &s); //同operator+=()

string &s,int pos,int n);//把字串s中從pos開始的n個字元連線到當前字串的結尾

7.string的比較:

bool

operator==(const

string &s1,const

string &s2)const;//比較兩個字串是否相等

運算子">","<",">=","<=","!="均被過載用於字串的比較;

int compare(const

string &s) const;//比較當前字串和s的大小

int compare(int pos, int n,const

string &s)const;//比較當前字串從pos開始的n個字元組成的字串與s的大小

int compare(int pos, int n,const

string &s,int pos2,int n2)const;//比較當前字串從pos開始的n個字元組成的字串與s中pos2開始的n2個字元組成的字串的大小

int compare(const

char *s) const;

int compare(int pos, int n,const

char *s) const;

int compare(int pos, int n,const

char *s, int pos2) const;

compare函式在》時返回1,《時返回-1,==時返回0

8.string的子串:

string substr(int pos = 0,int n = npos) const;//返回pos開始的n個字元組成的字串
9.string的交換:

void swap(string &s2);    //交換當前字串與s2的值
10.string類的插入函式:

string &insert(int p0, const

char *s);

string &insert(int p0, const

char *s, int n);

string &insert(int p0,const

string &s);

string &insert(int p0,const

string &s, int pos, int n);

//前4個函式在p0位置插入字串s中pos開始的前n個字元

string &insert(int p0, int n, char c);//此函式在p0處插入n個字元c

iterator insert(iterator it, char c);//在it處插入字元c,返回插入後迭代器的位置

void insert(iterator it, const_iterator first, const_iterator last);//在it處插入[first,last)之間的字元

void insert(iterator it, int n, char c);//在it處插入n個字元c

11.string類的刪除函式

iterator erase(iterator first, iterator last);//刪除[first,last)之間的所有字元,返回刪除後迭代器的位置

iterator erase(iterator it);//刪除it指向的字元,返回刪除後迭代器的位置

string &erase(int pos = 0, int n = npos);//刪除pos開始的n個字元,返回修改後的字串

關於字串

一些基本概念 用字元陣列儲存這麼乙個字串 char str hello world 然後我們應該知道,1.陣列的大小是12 字串字元個數 1 最後乙個元素為 0 用於標記字串的結束。0 不是數字0,它是非列印字元,其ascii碼值為0 2.若使用如下定義 char str 100 hello wor...

關於字串

關於string最重要的幾點 string為引用型別體現於其在棧記憶體和堆記憶體中的結構 堆記憶體中有字串常量池。有幾種new字串的方式 string str ass 方式1 string str1 new string ass 方式2char cha string str2 new string ...

關於MYSQL字串

字串是多個字元組成的乙個字串行,由單引號 或雙引號 字元包圍。但在 ansi 模式中執行時只能用單引號 例如 a string another string 在乙個字串中,如果某個序列具有特殊的含義,每個序列以反斜線符號 開頭,稱為轉義字元。mysql 識別下列轉義字元 0 乙個 ascii 0 n...