C++(STL)學習(二)—string容器

基本概念

string類,定義在頭文件<string>中

String和C風格字符串相比:
  • Char *是一個指針,String是一個類
    String封裝了char* ,管理這個字符串,是個 char*類型的容器
  • String封裝了很多使用的成員方法
    查找、拷貝、刪除、替換、插入
  • 不用考慮內存釋放和越界
    String管理Char*所分配的內存,每一次string的複製,取值都由string類負責維護,不用擔心複製越界和取值越界等

string容器常用操作

string構造函數

string();	//  創建一個空的字符串
string(const string & str);  //  使用一個string對象來初始化另一個string對象
string(const char *s);  //	使用字符串s初始化
string(int n,char c);  //  使用n個字符初始化

string基本賦值操作

string &operator=(const char *s);  //  把char *類型字符串賦值給當前的字符串
string &operator=(const string &s);  //		把字符串s賦值給當前的字符串
string &operator=(char c);	// 字符賦值給當前的字符串
string &assign(const char *s);//	把字符串s賦值給當前的字符串
string &assign(const char *s,int n);
string &assign(const string &s);//	把字符串s賦值給當前的字符串
string &assign(int n,char c);
string &assign(const string &s,int start,int n);

#include <iostream>
#include <string>
#include <stdexcept>
#include <vector>
#include <algorithm>
using namespace std;


//  String案例
void test01()
{
    string s1;
    string s2(s1);
    string s3("aaa");
    string s4(10,'c');

    cout<<s3<<endl;
    cout<<s4<<endl;

    string s5;
    s5 = s4;
    cout<<s5<<endl;
    s5.assign("abcdefg",3);

    cout<<s5<<endl;

    string s6;
    s6.assign("abcdefg",3,4);
    cout<<s6<<endl;



}

//  字符存取

void  test02()
{
    string s = "hello,world!";

    for(int i=0;i<s.size();i++)
    {
        //  cout<<s[i]<<endl;
        cout<<s.at(i)<<endl;    //  at和[]區別  []訪問越界會直接掛掉  而at訪問越界  會拋出一個異常
    }

    try
    {
        //  s[100];
        s.at(100);
    }
    catch(out_of_range &e)
    {
        cout<<e.what()<<endl;
    }


}

void test03()
{
    //  字符串拼接
    string s1 = "我";
    string s2 = "愛學習";
    s1 +=s2;
    cout<<s1<<endl;

    string s3 = "我";
    s3 +="愛學習";
    cout<<s3<<endl;

    s3.append("愛學習");
    cout<<s3<<endl;

    //  字符串查找  第二個參數默認查找起始座標爲0
     string s4 = "abcdefghide";

     cout<<s4.find("def")<<endl;
     cout<<s4.find("den")<<endl;  //  如果找不到返回-1  找到的話返回第一次出現的位置

     cout<<s4.rfind("de")<<endl;    //  rfind從右往左查找

     //  字符串替換
     s4.replace(1,3,"111111111");
     cout<<s4<<endl;

}


void test04()
{
    string s1 = "abcde";
    string s2 = "abcd";

    if(!s1.compare(s2))
    {
        cout<<"s1=s2"<<endl;
    }
    else if(s1.compare(s2)>0)
    {
        cout<<"s1>s2"<<endl;
    }
    else
    {
        cout<<"s1<s2"<<endl;
    }
}

void test05()
{
    string s1 = "abcde";
    cout<<s1.substr(1,3)<<endl;

    string s2 = "[email protected]";
    cout<<s2.substr(0,s2.find("@"))<<endl;
}

void test06()
{
    string s1 = "www.baidu.com.cn";
    vector<string>v;
    int i = 0;
    while(true)
    {
        if(s1.find(".",i)==-1)
        {
            v.push_back(s1.substr(i,s1.size()));
            break;
        }
        v.push_back(s1.substr(i, s1.find(".",i)-i));
        i = s1.find(".",i)+1;
    }
    for(vector<string>::iterator it = v.begin();it!=v.end();it++)
    {
        cout<<*it<<endl;
    }
}


//  string插入和刪除操作

/*
    string& insert(int pos,const char *s);  //  插入字符串
    string& insert(int pos,const string &s);  //  插入字符串
    string& insert(int pos,int n,char c);  //  在指定位置插入n個字符
    string& erase(int pos,int n = npos);  //  刪除從Pos開始的n個字符
*/
void test07()
{
    string s1 = "abcdefg";
    cout<<s1.insert(2,"112233")<<endl;

    cout<<s1.insert(2,5,'n')<<endl;

    cout<<s1.erase(2,11)<<endl;
}

//  string和C風格字符串轉換
/*
 * string 轉 char*
 * string str = "abcdef";
 * const char * cstr = str.c_str();
 * char* 轉 string
 * char *s = "abcdef";
 * string str(s)
 * */

void test09()
{
    string s = "abcdefg";
    char &a = s[2];
    char &b = s[3];

    a = '1';
    b = '2';

    cout<<s<<endl;
    cout<<(int *)s.c_str()<<endl;

    s = "ppppppppppppppppp";
    a = '1';
    b = '2';
    cout<<s<<endl;
    cout<<(int *)s.c_str()<<endl;
}

void test10()
{
    string s = "abdEsad";
    for(int i=0;i<s.size();i++)
    {
        s[i] = toupper(s[i]);

        s[i] = tolower(s[i]);
    }

    cout<<s<<endl;
}

int main()
{
    //  test01();
    //  test02();
    //  test03();
    //  test04();
    //  test05();
    //  test06();
    //  test07();
    //  test09();
    test10();
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章