程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> C++ string類型詳解

C++ string類型詳解

編輯:關於C++

string是非常強大的類型,很好的封裝了字符串的操作,有些時候我們可以把string當做字符的容器,string也支持大多數容器操作,下面就列出string類型所支持的所有操作,本文並不是為了講解string的用法和應用,而是希望作為string類型的參考文檔,每個函數皆在注釋後有詳細說明,需要用時查閱即可。

1.構造函數

 

string();//空串

string(size_type length,char ch);//以length為長度的ch的拷貝(即length個ch)

string(const char *str);//以str為初值 (長度任意)

string(const char *str,size_type length);//同上,長度不限,但注意不要越界,以免發生不可預知問題

string(string &str, size_type index, size_type length);
//以index為索引開始的子串,長度為length, 或者小於length

string(input_iterator begin, input_iterator end);//以從start到end的元素為初值
2.支持的操作符
==
>
<
>=
<=
!=
+
+=
[ ]

 

3.追加文本(append)

 

basic_string &append(const basic_string &str);//在字符串的末尾添加str

basic_string &append(const char *str);//在字符串末尾添加str所指向的c風格字符串

basic_string &append(const basic_string &str,size_type index,size_type len);
//在字符串的末尾添加str的子串,子串以index索引開始,長度為len

basic_string &append(const char *str,size_type num);//在字符串的末尾添加str中的num個字符

basic_string &append(size_type num,char ch);//在字符串的末尾添加num個字符ch

basic_string &append(input_iterator start,input_iterator end);
//在字符串的末尾添加以迭代器start和end表示的字符序列

push_back('k');//把一個字符連接到當前字符串的結尾

 

4.賦值(assign)

 

basic_string &assign(const basic_string &str);//用str為字符串賦值

basic_string &assign(const char *str);//用str c風格為字符串賦值

basic_string &assign(const char *str,size_type num);//用str的開始num個字符為字符串賦值

basic_string &assign(const basic_string &str,size_type index,size_type len);
//用str的子串為字符串賦值,子串以index索引開始,長度為len

basic_string &assign(size_type num,char ch);//用num個字符ch為字符串賦值

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

 

5.比較(compare)

 

int compare(const basic_string &str);//比較自己和str

int compare(size_type index,size_type length,const basic_string &str);
//比較自己的子串和str,子串以index索引開始,長度為length

int compare(size_type index,size_type length,const basic_string &str,size_type 
            index2,size_type length2);
//比較自己的子串和str的子串,其中index2和length2引用str,index和length引用自己

int compare(const char *str);//比較自己和str

int compare(int pos, int n,const char *s)
//比較自己的子串,從pos開始,n個字符,和s進行比較

int compare(size_type index,size_type length,const char *str,size_type length2);
//比較自己的子串和str的子串,其中str的子串以索引0開始,長度為length2,自己的子串
//以index開始,長度為length

 

返回值 情況

小於零 this < str
零 this == str
大於零 this > str

6.刪除(erase)

 

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

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

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

 

7.插入(insert)

 

iterator insert(iterator i,const char &ch);//在迭代器i表示的位置前面插入一個字符ch

basic_string &insert(size_type index,const basic_string &str);//在字符串的位置index插入字符串str

basic_string &insert(size_type index,const char *str);//在字符串的位置index插入字符串str

basic_string &insert(size_type index1,const basic_string &str,size_type index2,size_type num);
//在字符串的位置index插入字符串str的子串(從index2開始,長num個字符)

basic_string &insert(size_type index,const char *str,size_type num);
//在字符串的位置index插入字符串str的num個字符

basic_string &insert(size_type index,size_type num,char ch );
//在字符串的位置index插入num個字符ch的拷貝

void insert(iterator i,size_type num,const char &ch);
//在迭代器i表示的位置前面插入num個字符ch的拷貝

void insert(iterator i,iterator begin,iterator end );
//在迭代器i表示的位置前面插入一段字符,從start開始,以end結束

 

8.替換(replace)

 

basic_string &replace(size_type index,size_type num,const basic_string &str);
//用str中的num個字符替換本字符串中的字符,從index開始

replace(size_type index1,size_type num1,const basic_string &str,size_type index2,size_type num2);
//用str中的num2個字符(從index2開始)替換本字符串中的字符,從index1開始,最多num1個字符

basic_string &replace(size_type index,size_type num,const char *str);
//用str中的num個字符(從index開始)替換本字符串中的字符

basic_string &replace(size_type index,size_type num1,const char *str,size_type num2);
//用str中的num2個字符(從index2開始)替換本字符串中的字符,從index1開始,num1個字符

basic_string &replace(size_type index,size_type num1,size_type num2,char ch);
//用num2個ch字符替換本字符串中的字符,從index開始,num1個字符

basic_string &replace(iterator start,iterator end,const basic_string &str);
//用str中的字符替換本字符串中的字符,迭代器start和end指示范圍

basic_string &replace(iterator start,iterator end,const char *str);
//用str替換本字符串中的內容,迭代器start和end指示范圍

basic_string &replace(iterator start,iterator end,const char *str,size_type num );
//用str中的num個字符替換本字符串中的內容,迭代器start和end指示范圍

basic_string &replace(iterator start,iterator end,size_type num,char ch );
//用num個ch字符替換本字符串中的內容,迭代器start和end指示范圍

 

9.查找

 

函數 find:

size_type find( const basic_string &str, size_type index );
//返回str在字符串中第一次出現的位置(從index開始查找)

size_type find( const char *str, size_type index );
//返回str在字符串中第一次出現的位置(從index開始查找)

size_type find( const char *str, size_type index, size_type length );
//返回str在字符串中第一次出現的位置(從index開始查找,長度為length)

size_type find( char ch, size_type index );
//返回字符ch在字符串中第一次出現的位置(從index開始查找)


函數 find_first_of:查找在字符串中第一個與str中的某個字符匹配的字符

    size_type find_first_of( const basic_string &str, size_type index = 0);

    size_type find_first_of( const char *str, size_type index = 0 );

    size_type find_first_of( const char *str, size_type index, size_type num );

    size_type find_first_of( char ch, size_type index = 0 );

函數 find_first_not_of:在字符串中查找第一個與str中的字符都不匹配的字符

    size_type find_first_not_of( const basic_string &str, size_type index = 0 );

    size_type find_first_not_of( const char *str, size_type index = 0 );

    size_type find_first_not_of( const char *str, size_type index, size_type num );

    size_type find_first_not_of( char ch, size_type index = 0 );

函數 find_last_of:在字符串中查找最後一個與str中的某個字符匹配的字符

  size_type find_last_of( const basic_string &str, size_type index = npos );

  size_type find_last_of( const char *str, size_type index = npos );

  size_type find_last_of( const char *str, size_type index, size_type num );

  size_type find_last_of( char ch, size_type index = npos );

函數 find_last_not_of:在字符串中查找最後一個與str中的字符都不匹配的字符

    size_type find_last_not_of( const basic_string &str, size_type index = npos );

    size_type find_last_not_of( const char *str, size_type index = npos);

    size_type find_last_not_of( const char *str, size_type index, size_type num );

    size_type find_last_not_of( char ch, size_type index = npos );

rfind函數

  size_type rfind( const basic_string &str, size_type index );
  //返回最後一個與str中的某個字符匹配的字符,從index開始查找

  size_type rfind( const char *str, size_type index );
  //返回最後一個與str中的某個字符匹配的字符,從index開始查找

  size_type rfind( const char *str, size_type index, size_type num );
  //返回最後一個與str中的某個字符匹配的字符,從index開始查找,最多查找num個字符

  size_type rfind( char ch, size_type index );
  //返回最後一個與ch匹配的字符,從index開始查找

 

10.其他函數

 

at函數
     reference at( size_type index );
     //at()函數返回一個引用,指向在index位置的字符. 如果index
     //不在字符串范圍內, at() 將報告"out of range"錯誤,並拋出out_of_range異常

begin函數
    iterator begin();//begin()函數返回一個迭代器,指向字符串的第一個元素

end函數
    iterator end();//返回一個迭代器,指向字符串的末尾(最後一個字符的下一個位置)

c_str函數
    const char *c_str();//返回一個指向正規C字符串的指針, 內容與本字符串相同

capacity函數
    size_type capacity();//返回在重新申請更多的空間前字符串可以
    //容納的字符數. 這個數字至少與 size()一樣大

copy函數
    size_type copy( char *str, size_type num, size_type index );
    //拷貝自己的num個字符到str中(從索引index開始)。返回值是拷貝的字符數

data函數
    const char *data();//返回指向自己的第一個字符的指針

empty函數
    bool empty();//如果字符串為空則empty()返回真(true),否則返回假(false)

get_allocator函數
    allocator_type get_allocator();//返回本字符串的配置器

length函數
    size_type length();//返回字符串的長度. 這個數字應該和size()返回的數字相同

max_size
    size_type max_size();//返回字符串能保存的最大字符數

rbegin函數
    rbegin();//返回一個逆向迭代器,指向最後一個字符

rend函數
    rend();//返回一個逆向迭代器,指向第一個元素的前一個位置

reserve函數
    reserve( size_type num );//保留一定容量以容納字符串(設置capacity值)

resize函數
  void resize( size_type num );//改變本字符串的大小到num, 新空間的內容不確定

  void resize( size_type num, char ch );//也可以指定用ch填充

size函數
    size();//返回字符串中字符的數量

substr函數
     basic_string substr( size_type index, size_type num = npos );
     //返回本字符串的一個子串,從index開始,長num個字符。如果沒有指定,
     //將是默認值 string::npos。這樣,substr()函數將簡單的返回從index開始的剩余的字符串

swap函數
    void swap( basic_string &str );//把str和本字符串交換

11.示例

 

#include 
#include 
#include 
using namespace std;
int main(){
    //1.string類重載運算符operator>>用於輸入,同樣重載運算符operator<<用於輸出操作
    string str1;
    cin >> str1;//當用cin>>進行字符串的輸入的時候,遇到空格的地方就停止字符串的讀取輸入
    cout << str1 << endl;
    cin.get();//這個的作用就是讀取cin>>輸入的結束符,不用對getline的輸入產生影響!
    getline(cin, str1);//字符串的行輸入
    cout << str1 << endl;

    //2.string類的構造函數
    string str2 = "aaaaa";//最簡單的字符串初始化
    cout << str2 << endl;

    char *s = "bbbbb";
    string str3(s);//用c字符串s初始化
    cout << str3 << endl;

    char ch = 'c';
    string str4(5, ch);//用n個字符ch初始化
    cout << str4 << endl;

    //3.string類的字符操作
    string str5 = "abcde";
    ch = str5[3];//operator[]返回當前字符串中第n個字符的位置
    cout << ch << endl;

    string str6 = "abcde";
    ch = str6.at(4);//at()返回當前字符串中第n個字符的位置,並且提供范圍檢查,當越界時會拋出異常!
    cout << ch << endl;

    //4.string的特性描述
    string str7 = "abcdefgh";
    int size;
    size = str7.capacity();//返回當前容量
    cout << size << endl;
    size = str7.max_size();//返回string對象中可存放的最大字符串的長度
    cout << size << endl;
    size = str7.size();//返回當前字符串的大小
    cout << size << endl;
    size = str7.length();//返回當前字符串的長度
    cout << size << endl;
    bool flag;
    flag = str7.empty();//判斷當前字符串是否為空
    cout << flag << endl;
    int len = 10;
    str7.resize(len, ch);//把字符串當前大小置為len,並用字符ch填充不足的部分
    cout << str7 << endl;

    //5.string的賦值
    string str8;
    str8 = str7;//把字符串str7賦給當前字符串
    cout << str8 << endl;
    str8.assign(str7);//把字符串str7賦給當前字符串
    cout << str8 << endl;
    str8.assign(s);//用c類型字符串s賦值
    cout << str8 << endl;
    str8.assign(s, 2);//用c類型字符串s開始的n個字符賦值
    cout << str8 << endl;
    str8.assign(len, ch);//用len個字符ch賦值給當前字符串
    cout << str8 << endl;
    str8.assign(str7, 0, 3);//把字符串str7中從0開始的3個字符賦給當前字符串
    cout << str8 << endl;
    string str9 = "0123456789";
    str8.assign(str9.begin(), str9.end());//把迭代器之間的字符賦給字符串
    cout << str8 << endl;

    //6.string的連接
    string str10;
    str10 += str9;//把字符串str9連接到當前字符串的結尾
    cout << str10 << endl;
    str10.append(s);//把c類型字符串s連接到當前字符串的結尾
    cout << str10 << endl;
    str10.append(s, 2);//把c類型字符串s的前2個字符連接到當前字符串的結尾
    cout << str10 << endl;
    str10.append(str9.begin(), str9.end());//把迭代器之間的一段字符連接到當前字符串的結尾
    cout << str10 << endl;
    str10.push_back('k');//把一個字符連接到當前字符串的結尾
    cout << str10 << endl;

    //7.string的比較
    flag = (str9 == str10);//判斷兩個字符串是否相等
    cout << flag << endl;
    flag = (str9 != str10);//判斷兩個字符串是否不相等
    cout << flag << endl;
    flag = (str9 > str10);//判斷兩個字符串是否大於關系
    cout << flag << endl;
    flag = (str9 < str10);//判斷兩個字符串是否為小於關系
    cout << flag << endl;
    flag = (str9 >= str10);//判斷兩個字符串是否為大於等於關系
    cout << flag << endl;
    flag = (str9 <= str10);//判斷兩個字符串否為小於等於關系
    cout << flag << endl;

    //以下的3個函數同樣適用於c類型的字符串,在compare函數中>時返回1,<時返回-1,=時返回0
    flag = str10.compare(str9);//比較兩個字符串的大小,通過ASCII的相減得出!
    cout << flag << endl;
    flag = str10.compare(6, 12, str9);//比較str10字符串從6開始的12個字符組成的字符串與str9的大小
    cout << flag << endl;
    flag = str10.compare(6, 12, str9, 3, 5);//比較str10字符串從6開始的12個字符組成的字符串與str9字符串從3開始的5個字符組成的字符串的大小
    cout << flag << endl;

    //8.string的字串
    string str11;
    str11 = str10.substr(10, 15);//返回從下標10開始的15個字符組成的字符串
    cout << str11 << endl;

    //9.string的交換
    str11.swap(str10);//交換str11與str10的值
    cout << str11 << endl;

    //10.string的查找,查找成功時返回所在位置,失敗時返回string::npos的值,即是-1
    string str12 = "abcdefghijklmnopqrstuvwxyz";
    int pos;
    pos = str12.find('i', 0);//從位置0開始查找字符i在當前字符串的位置
    cout << pos << endl;
    pos = str12.find("ghijk", 0);//從位置0開始查找字符串“ghijk”在當前字符串的位置
    cout << pos << endl;
    pos = str12.find("opqrstuvw", 0, 4);//從位置0開始查找字符串“opqrstuvw”前4個字符組成的字符串在當前字符串中的位置
    cout << pos << endl;
    pos = str12.rfind('s', string::npos);//從字符串str12反向開始查找字符s在字符串中的位置
    cout << pos << endl;
    pos = str12.rfind("klmn", string::npos);//從字符串str12反向開始查找字符串“klmn”在字符串中的位置
    cout << pos << endl;
    pos = str12.rfind("opqrstuvw", string::npos, 3);//從string::pos開始從後向前查找字符串s中前n個字符組成的字符串在當前串中的位置
    cout << pos << endl;

    string str13 = "aaaabbbbccccdddeeefffggghhhiiijjjkkllmmmandjfaklsdfpopdtwptioczx";
    pos = str13.find_first_of('d', 0);//從位置0開始查找字符d在當前字符串第一次出現的位置
    cout << pos << endl;
    pos = str13.find_first_of("eefff", 0);//從位置0開始查找字符串“eeefff“在當前字符串中第一次出現的位置
    cout << pos << endl;
    pos = str13.find_first_of("efff", 0, 3);//從位置0開始查找當前串中第一個在字符串”efff“的前3個字符組成的數組裡的字符的位置
    cout << pos << endl;
    pos = str13.find_first_not_of('b', 0);//從當前串中查找第一個不在串s中的字符出現的位置
    cout << pos << endl;
    pos = str13.find_first_not_of("abcdefghij", 0);//從當前串中查找第一個不在串s中的字符出現的位置
    cout << pos << endl;
    pos = str13.find_first_not_of("abcdefghij", 0, 3);//從當前串中查找第一個不在由字符串”abcdefghij”的前3個字符所組成的字符串中的字符出現的位置
    cout << pos << endl;
    //下面的last的格式和first的一致,只是它從後面檢索!
    pos = str13.find_last_of('b', string::npos);
    cout << pos << endl;
    pos = str13.find_last_of("abcdef", string::npos);
    cout << pos << endl;
    pos = str13.find_last_of("abcdef", string::npos, 2);
    cout << pos << endl;
    pos = str13.find_last_not_of('a', string::npos);
    cout << pos << endl;
    pos = str13.find_last_not_of("abcdef", string::npos);
    cout << pos << endl;
    pos = str13.find_last_not_of("abcdef", string::npos, 3);
    cout << pos << endl;

    //11.string的替換
    string str14 = "abcdefghijklmn";
    str14.replace(0, 3, "qqqq");//刪除從0開始的3個字符,然後在0處插入字符串“qqqq”
    cout << str14 << endl;
    str14.replace(0, 3, "vvvv", 2);//刪除從0開始的3個字符,然後在0處插入字符串“vvvv”的前2個字符
    cout << str14 << endl;
    str14.replace(0, 3, "opqrstuvw", 2, 4);//刪除從0開始的3個字符,然後在0處插入字符串“opqrstuvw”從位置2開始的4個字符
    cout << str14 << endl;
    str14.replace(0, 3, 8, 'c');//刪除從0開始的3個字符,然後在0處插入8個字符 c
    cout << str14 << endl;
    //上面的位置可以換為迭代器的位置,操作是一樣的,在這裡就不再重復了!

    //12.string的插入,下面的位置處亦可以用迭代器的指針表示,操作是一樣的
    string str15 = "abcdefg";
    str15.insert(0, "mnop");//在字符串的0位置開始處,插入字符串“mnop”
    cout << str15 << endl;
    str15.insert(0, 2, 'm');//在字符串的0位置開始處,插入2個字符m
    cout << str15 << endl;
    str15.insert(0, "uvwxy", 3);//在字符串的0位置開始處,插入字符串“uvwxy”中的前3個字符
    cout << str15 << endl;
    str15.insert(0, "uvwxy", 1, 2);//在字符串的0位置開始處,插入從字符串“uvwxy”的1位置開始的2個字符
    cout << str15 << endl;

    //13.string的刪除
    string str16 = "gfedcba";
    string::iterator it;
    it = str16.begin();
    it++;
    str16.erase(it);//刪除it指向的字符,返回刪除後迭代器的位置
    cout << str16 << endl;
    str16.erase(it, it+3);//刪除it和it+3之間的所有字符,返回刪除後迭代器的位置
    cout << str16 << endl;
    str16.erase(2);//刪除從字符串位置3以後的所有字符,返回位置3前面的字符
    cout << str16 << endl;

    //14.字符串的流處理
    string str17("hello,this is a test");
    istringstream is(str17);
    string s1,s2,s3,s4;
    is>>s1>>s2>>s3>>s4;//s1="hello,this",s2="is",s3="a",s4="test"
    ostringstream os;
    cout << str17 << endl;

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved