C++--STL----string

string是STL的字符串类型,通常用来表示字符串。而在使用string之前,字符串通常是用char*表示的。string与char*都可以用来表示字符串,那么二者有什么区别呢。
string和char*的比较
string是一个类, char*是一个指向字符的指针。
         string封装了char*,管理这个字符串,是一个char*型的容器。
string不用考虑内存释放和越界。
         string管理char*所分配的内存。每一次string的复制,取值都由string类负责维护,不用担心复制越界和取值越界等。
string提供了一系列的字符串操作函数(这个等下会详讲)

         查找find,拷贝copy,删除erase,替换replace,插入insert

string在STL中一般有十几种方式。

一,string的构造函数

        默认构造函数:
             string();    //构造一个空的字符串string s1。
        拷贝构造函数:
             string(const string &str);     //构造一个与str一样的string。如string s1(s2)。
         带参数的构造函数
            string(const char *s);    //  用字符串s初始化

            string(int n,char c);        //  用n个字符c初始化

void StringInit()   //字符串构造函数,4种方式
{
    string s1;
    string s2("helloworld");
    string s3(s2);
    string s4(10,'a');

    cout << s1 << endl;
    cout << s2 << endl;
    cout << s3 << endl;
    cout << s4 << endl;
}

二,string的赋值

            string &operator=(const string &s);     //把字符串s赋给当前的字符串
            string &assign(const char *s);               //把字符串s赋给当前的字符串
            string &assign(const char *s, int n);    //把字符串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个字符赋给当前字符串

void StringAssign()  //给字符串赋值
{
    string s1("helloworld");
    string s2("test");
    char *s = "wangsheng";
    s2 = s1;
    cout << s2 << endl; 

    s2.assign(s);  //完全拷贝
    cout << s2 << endl;

    s2.assign(s,5); // 拷贝前五个
    cout << s2 << endl;

    s2.assign(s1);
    cout << s2 << endl;

    s2.assign(10,'a');  //赋值10 个’a‘
    cout << s2 << endl;

    s2.assign(s1,5,2);  //从s1的第五个字符往后复制2个给s2
    cout << s2 << endl;
}

三,string中的查找,替换

                查找
                    int find(char c,int pos=0) const;  //从pos开始查找字符c在当前字符串的位置 
                    int find(const char *s, int pos=0) const;  //从pos开始查找字符串s在当前字符串的位置
                    int find(const string &s, int pos=0) const;  //从pos开始查找字符串s在当前字符串中的位置
                    find函数如果查找不到,就返回-1
                    int rfind(char c, int pos=npos) const;   //从pos开始从后向前查找字符c在当前字符串中的位置 
                    int rfind(const char *s, int pos=npos) const;
                    int rfind(const string &s, int pos=npos) const;
                    //rfind是反向查找的意思,如果查找不到, 返回-1
                替换
                    string &replace(int pos, int n, const char *s);//删除从pos开始的n个字符,然后在pos处插入串s
                     string &replace(int pos, int n, const string &s);  //删除从pos开始的n个字符,然后在pos处插入串s

                    void swap(string &s2);    //交换当前字符串与s2的值

void StringFind()  //字符串查找,找到后返回字符串位置;可定义从什么位置开始寻找,可以替换
{
    string s1("helloworldhelloworld");
    string s2("world");

    int index = s1.find('h',1);
    cout << index << endl;

    index = s1.find("hello",0);
    cout << index << endl;

    index = s1.find(s2,0);
    cout << index << endl;

    index = s1.find("hello",0);
    while(-1 != index)
    {
        s1.replace(0,5,"xx");
        index = s1.find("hello",index + strlen("xx"));
    }
    cout << s1 << endl;
}

三,sting的拷贝

            int copy(char *s, int n, int pos=0) const;  

              把当前串中以pos开始的n个字符拷贝到以s为起始位置的字符数组中,返回实际拷贝的数目。注意要保证s所指向的空                   间足够大以容纳当前字符串,不然会越界。

void StringCopy()  //字符串拷贝
{
    string s1("helloworld");
    char buf[64] = {0};
    s1.copy(buf,5,2);
    cout << buf << endl;
}


四,string的删除之后插入

            string &insert(int pos, const char *s);
            string &insert(int pos, const string &s);
            //前两个函数在pos位置插入字符串s
            string &insert(int pos, int n, char c);  //在pos位置 插入n个字符c
            string &erase(int pos=0, int n=npos);  //删除pos开始的n个字符,返回修改后的字符串

void StringInsert()  //字符串删除插入,可定义插入的位置
{
    string s1("helloworld");
    string s2("test");
    s1.insert(5,"wang");
    cout << s1 << endl;

    s1.insert(7,s2);
    cout << s1 << endl;

    s2.insert(2,3,'c');
    cout << s2 << endl;

    s1.erase(5,5);
    cout << "s1: " << s1 << endl;

}

五,sting的查找替换

            查找
            int find(char c,int pos=0) const;  //从pos开始查找字符c在当前字符串的位置 
            int find(const char *s, int pos=0) const;  //从pos开始查找字符串s在当前字符串的位置
            int find(const string &s, int pos=0) const;  //从pos开始查找字符串s在当前字符串中的位置
            find函数如果查找不到,就返回-1
            int rfind(char c, int pos=npos) const;   //从pos开始从后向前查找字符c在当前字符串中的位置 
            int rfind(const char *s, int pos=npos) const;
            int rfind(const string &s, int pos=npos) const;
            //rfind是反向查找的意思,如果查找不到, 返回-1
        替换
                string &replace(int pos, int n, const char *s);//删除从pos开始的n个字符,然后在pos处插入串s
                string &replace(int pos, int n, const string &s);  //删除从pos开始的n个字符,然后在pos处插入串s
                void swap(string &s2);    //交换当前字符串与s2的值
 

void StringFind()  //字符串查找,找到后返回字符串位置;可定义从什么位置开始寻找
{
    string s1("helloworldhelloworld");
    string s2("world");

    int index = s1.find('h',1);
    cout << index << endl;

    index = s1.find("hello",0);
    cout << index << endl;

    index = s1.find(s2,0);
    cout << index << endl;

    index = s1.find("hello",0);
    while(-1 != index)
    {
        s1.replace(0,5,"xx");  //字符串替换
        index = s1.find("hello",index + strlen("xx"));
    }
    cout << s1 << endl;
}

六,string的取字符操作

            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()在越界时会抛出异常,[]在刚好越界时会返回(char)0,再继续越界时,编译器直接出错。如果你的程序希望可以通过try,catch捕获异常,建议采用at()。   

void StringAt()
{
    string s1("helloworld");
    //cout << s1[500000] << endl;  //越界访问,段错误
    try
    {
        cout << s1.at(15) << endl;         // 越界访问,抛出异常
    }
    catch(exception &e)
    {
        cout << "catch a exception:" << e.what() << endl;
    }
}

七,string的从string取得const char*的操作

           const char *c_str() const;   //返回一个以'\0'结尾的字符串的首地址

void StringStr()
{
    string s2("helloworld");
    const char* str;
    str = s2.c_str();    // 从str中取得const char*的操作
    cout << str << endl;

}

八,string的长度

int length() const;         //返回当前字符串的长度。长度不包括字符串结尾的'\0'。
bool empty() const;     //当前字符串是否为空
 

void StringLength()  //求字符串长度
{
    string s2("helloworld");
    cout << s2.length() << endl;
    if(s2.empty())
    {
        cout << "null" << endl;
    }
    else
    {
        cout << "not null" << endl;
    }
}

九,string的链接

        string &operator+=(const string &s);  //把字符串s连接到当前字符串结尾
        string &operator+=(const char *s);//把字符串s连接到当前字符串结尾
        string &append(const char *s);    //把字符串s连接到当前字符串结尾
        string &append(const char *s,int n);  //把字符串s的前n个字符连接到当前字符串结尾
        string &append(const string &s);   //同operator+=()
        string &append(const string &s,int pos, int n);//把字符串s中从pos开始的n个字符连接到当前字符串结尾
        string &append(int n, char c);   //在当前字符串结尾添加n个字符c
 

 
void StringAppend()  //字符串链接
{
    string s1("helloworld");
    string s2("123456");
    char *s = "wangsheng";
     
    s1 = s1 + s2;
    s1 = s1 + s;
    cout << s1 << endl;

    s1.append(s);
    cout << s1 << endl;

    s1.append(s,5);
    cout << s1 << endl;

    s1.append(s2);
    cout << s1 << endl;

    s1.append(s2,3,4);
    cout << s1 << endl;

    s1.append(10,'a');
    cout << s1 << endl;

}

十,string的比较

            int compare(const string &s) const;  //与字符串s比较
            int compare(const char *s) const;   //与字符串s比较
            compare函数在>时返回 1,<时返回 -1,==时返回 0。比较区分大小写,比较时参考字典顺序,排越前面的越小大写的A比小写的a小。
 

void StringCompare()   // 字符串比较
{
    string s1("hello");
    string s2("wang");
    if(-1 == s1.compare(s2))
    {
        cout << "s1 < s2" << endl;
    }
    else if(1 == s1.compare(s2))
    {
        cout << "s1 > s2" << endl;
    }
    else
    {
        cout << "s1 = s2" << endl;
    }
}

十一,string的子串

            string substr(int pos=0, int n=npos) const;    //返回由pos开始的n个字符组成的子字符串
 

void StringSub()  //返回需要的字符串
{
    string s1("helloworldhelloworldhelloworld");
    string s2;
    s2 = s1.substr(5,4);  //把从s1的第五个字符开始往后复制器四个给s2
    cout << s2 << endl;
}
十二,string的算法相关
 
string s2 = "AAAbbb";
    transform(s2.begin(), s2.end(), s2.begin(), toupper);
    cout << s2 << endl;
 
    string s3 = "AAAbbb";
    transform(s3.begin(), s3.end(), s3.begin(), tolower);
    cout << s3 << endl;
这里总结了string的十二中相关。

        

猜你喜欢

转载自blog.csdn.net/wsqfly06/article/details/80440804